web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by 2

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

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    hey Abhiram,

    You need to save the form using java script

    Please add below line at the end of your code to set look up value in java script and after the setsubmitmode = 'always' ;

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

    Thanks & Regards,

    Divyesh Sapovadiya

  • Abhiram Thulasi Profile Picture
    2 on at

    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

  • Community Member Profile Picture
    on at

    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
    2 on at

    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.

  • Abhiram Thulasi Profile Picture
    2 on at

    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);

       }

    }

  • Community Member Profile Picture
    on at

    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

  • Aileen Gusni Profile Picture
    44,524 on at

    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.

  • Abhiram Thulasi Profile Picture
    2 on at

    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.

  • Abhiram Thulasi Profile Picture
    2 on at

    Hi Aileen Gusni,

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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans