Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Unable to set the value of a lookup using java script and Business rule

Posted on by 222

If I set the value of a lookup field using Javascript, it appears correctly on the form (name, icon, etc) but does not save when the user hits the save button. The record does save, just not that field. If I do it "manually" (ie - use the lookup control) it will save the field value. The field is not read only, but I have tried with submitmode = 'always' as well with the same results. I have also tried variations on "typename" (e.g. used "entityType") The field is even mandatory and the record still saves. This issue is in CRM 2015 update 1 .Any ideas?

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to set the value of a lookup using java script and Business rule

    I got the same issue but i setup the lookup as follows so its working... Please find below for your reference.

    function setLookupValue(LookupId, Type, Id, Name) {

       var lookupReference = [];

       lookupReference[0] = {};

       lookupReference[0].id = Id;

       lookupReference[0].entityType = Type;

       lookupReference[0].name = Name;

       Xrm.Page.getAttribute(LookupId).setValue(lookupReference);

    }

             var lookup = 'new_service'

             var  lookuptype = 'product'; // Lookup Entity

             var  lookupID = '4C6EEB49-5EAA-E611-80FE-005056A82748'; // Guid of lookup

              var lookupname = 'recordname';

           }

           setLookupValue(lookup, lookuptype, lookupID, lookupname);

  • Abhiram Thulasi Profile Picture
    Abhiram Thulasi 222 on at
    RE: Unable to set the value of a lookup using java script and Business rule

    Hi Aileen Gusni,

       There is record with the primary field and if clicked on that lookup name which is going into the corresponding record.

  • Abhiram Thulasi Profile Picture
    Abhiram Thulasi 222 on at
    RE: Unable to set the value of a lookup using java script and Business rule

    Hi Divyesh,

    I got the value of dirty flag as " true" .... but still I can not stop the execution. If I put some alerts inside my code the alerts are coming twice.

  • Aileen Gusni Profile Picture
    Aileen Gusni 44,522 on at
    RE: Unable to set the value of a lookup using java script and Business rule

    Abhiram,

    Just curious, the lookup field name is having value or not?

    Because somehow, it looks not saving because its primary field name is blank.

    For example: You have lookup to Bank Account, but bank Account that you use has no Name (primary field in CRM entity) so that it left looks like blank.

    THanks.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to set the value of a lookup using java script and Business rule

    Hi Abhiram,

    Please debug the above code and check that are you able to go inside this line of code.

    if (dirtyflag == "true")

    ...

    And if you are getting error than please check the functionality with the minimum code and then after implement all of your business logic.

    Thanks & Regards,

    Divyesh Sapovadiya

  • Abhiram Thulasi Profile Picture
    Abhiram Thulasi 222 on at
    RE: Unable to set the value of a lookup using java script and Business rule

    Here is my code :

    // JavaScript source code

    function setproblemtype(econtext) {

       var subject;

       var problemcodename;

       if (Xrm.Page.data.entity.attributes.get("subjectid").getValue() != "" && Xrm.Page.data.entity.attributes.get("subjectid").getValue() != null)

               {

                   subject = Xrm.Page.data.entity.attributes.get("subjectid").getValue()[0].name;

               }

               var Severity = Xrm.Page.data.entity.attributes.get("severitycode");

               var ServiceType = Xrm.Page.data.entity.attributes.get("md_service");

               var id = "";

               if (Xrm.Page.data.entity.attributes.get("subjectid").getValue() != "" && Xrm.Page.data.entity.attributes.get("subjectid").getValue() != null)

               {

                   var ClientUrl = Xrm.Page.context.getClientUrl();

                   var ODataPath = ClientUrl + "/XRMServices/2011/OrganizationData.svc";

                   var retrieveReq = new XMLHttpRequest();

                   var filter = "/md_problemcodeSet?" +

                                     "$select=md_problemcodeId,md_name" + "&$filter=md_name eq '" + subject + "'";

                   retrieveReq.open("GET", ODataPath + filter, false);

                   //alert(ODataPath + filter);

                   retrieveReq.setRequestHeader("Accept", "application/json");

                   retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");

                   retrieveReq.send(null);

                   var retrievedRecords = JSON.parse(retrieveReq.responseText).d;

                   if (retrievedRecords.results.length > 0) {

                       var f = retrievedRecords.results[0];

                       if (f.md_problemcodeId != null) {

                           id = f.md_problemcodeId;

                       }

                       if (f.md_name != null) {

                           problemcodename = f.md_name;

                       }

                   }

                   if (id != "") {

                       if (subject == "Extension of Payment") {

                           Severity.setValue(100000001);

                           ServiceType.setValue(100000000);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if ((subject == "Deferring of Payment") || (subject == "Refund") || (subject == "Waiver") || (subject == "Discount Request") || (subject == "Price Adjustment")) {

                           Severity.setValue(100000003);

                           ServiceType.setValue(100000000);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Plot Size Discrepancy") {

                           Severity.setValue(100000004);

                           ServiceType.setValue(100000001);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Floor Plan Request") {

                           Severity.setValue(100000000);

                           ServiceType.setValue(100000001);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Plot Plan Request") {

                           Severity.setValue(100000001);

                           ServiceType.setValue(100000001);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Name Rectification") {

                           Severity.setValue(100000002);

                           ServiceType.setValue(100000002);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Name Change") {

                           Severity.setValue(100000003);

                           ServiceType.setValue(100000002);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Customer Information Update") {

                           Severity.setValue(100000001);

                           ServiceType.setValue(100000002);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Reinstatement") {

                           Severity.setValue(100000005);

                           ServiceType.setValue(100000003);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Consolidation") {

                           Severity.setValue(100000005);

                           ServiceType.setValue(100000004);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Primary Market Transfer") {

                           Severity.setValue(100000001);

                           ServiceType.setValue(100000005);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Mortgage Discharger") {

                           Severity.setValue(100000001);

                           ServiceType.setValue(100000006);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Mortgage Registration") {

                           Severity.setValue(100000003);

                           ServiceType.setValue(100000006);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Upgrade") {

                           Severity.setValue(100000002);

                           ServiceType.setValue(100000007);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Downgrade") {

                           Severity.setValue(100000002);

                           ServiceType.setValue(100000007);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Merger") {

                           Severity.setValue(100000005);

                           ServiceType.setValue(100000007);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else if (subject == "Swap") {

                           Severity.setValue(100000005);

                           ServiceType.setValue(100000007);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                           //Xrm.Page.getAttribute("md_problemcodeid").setSubmitMode("always");

                       }

                       else if (subject == "Sale Cancellation") {

                           Severity.setValue(100000001);

                           ServiceType.setValue(100000008);

                           SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");

                       }

                       else {

                           Severity.setValue("");

                           ServiceType.setValue("");

                           Xrm.Page.getAttribute("md_problemcodeid").setValue(null);

                       }

                   }

                   else {

                       Severity.setValue("");

                       ServiceType.setValue("");

                       Xrm.Page.getAttribute("md_problemcodeid").setValue(null);

                   }

               }

               else {

                   Severity.setValue("");

                   ServiceType.setValue("");

                   Xrm.Page.getAttribute("md_problemcodeid").setValue(null);

               }

               var dirtyflag = "false";

               var attributes = Xrm.Page.data.entity.attributes.get();

               var formType = Xrm.Page.ui.getFormType();

               if (formType != 1) {

                   for (var i in attributes) {

                       var attribute = attributes[i];

                       if (attribute.getIsDirty()) {

                           dirtyflag = "true";

                           //alert(attribute.getName());

                           Xrm.Page.getAttribute(attribute.getName()).setSubmitMode("always"); //force save flag

                       }

                       if (dirtyflag == "true") {

                           Xrm.Page.data.entity.save();

                       }

                   }

               }

    }

    function SetLookupValue(fieldName, id, name, entityType) {

       if (fieldName != null) {

           var lookupValue = new Array();

           lookupValue[0] = new Object();

           lookupValue[0].id = id;

           lookupValue[0].name = name;

           lookupValue[0].entityType = entityType;

           Xrm.Page.getAttribute(fieldName).setValue(lookupValue);

       }

    }

  • Abhiram Thulasi Profile Picture
    Abhiram Thulasi 222 on at
    RE: Unable to set the value of a lookup using java script and Business rule

    Hi Divyesh,

    I have only one script for this form and i am triggering the script on onchnage of a field, but it is getting called on Onsave event also. I don't know how....and the form is not getting saved.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to set the value of a lookup using java script and Business rule

    Hey Abhiram,

    Can you please attach the code which you are using. Some times, you are getting this issue because may be you are updating the value of the any other field using another java script library.

    So, Please make sure you are not calling another library from the form.

    And Please, also attach the snapshot of the Form Properties with the calling function detail so i can check if there is any other issue.]

    Thanks & Regards,

    Divyesh Sapovadiya

  • Abhiram Thulasi Profile Picture
    Abhiram Thulasi 222 on at
    RE: Unable to set the value of a lookup using java script and Business rule

    I tried this  but still the unsaved changes are there. The same script is automatically calling on save of that record. But actually there is no script on load

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans