Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Updating an Option Set field in Projects based on change of option set in Entity Items

Posted on by Microsoft Employee

I have an option set in Projects and Entity Items that share the same name, new_2vpnapplicable (fields have been double checked for syntax and var type) and duplicate value set.   The script in question below, I borrowed from Rawish Kumar - https://community.dynamics.com/crm/b/passiondynamics/archive/2018/01/13/get-lookup-value-from-other-entity-and-set-it-on-the-form-using-web-api-in-microsoft-dynamics-crm.

The script below works until line 45.  I verified that the Projects entity has focus and that the value of the field fieldValueToCopy is 100000000 (as expected).  I hard coded this value in place of the var, fieldValueToCopy, to verify there was no trouble with the variable.  The field new_2vpnapplicable in Projects is not being updated, though I can modify it manually.

line 45: Xrm.Page.getAttribute("new_2vpnapplicable").setValue(fieldValueToCopy);

******* Script start ******

function VPNApplicable_Opt() {
debugger;
    
    var fieldValueToCopy  = Xrm.Page.getAttribute("new_2vpnapplicable").getValue();
    var lookupObj = Xrm.Page.getAttribute("objectid").getValue();
    var newid = lookupObj[0].id.slice(1, -1); // you will get perfect id like "EDCJDKDJDKJDJDKJDJKD" here.

    var req = new XMLHttpRequest();
    /*
    var primaryProjectId;
    EntityReference  primaryProjectId = new EntityReference ("msdyn_projects", newid);
    console.log(primaryProjectId);
    
    queueitem["primaryProjectId"] = primaryProjectId;    
    */
    // req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/msdyn_projects(" + newid + ")?$select=new_2vpnapplicable", true);
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/msdyn_projects(" + newid + ")?$select=new_2vpnapplicable", true);
    /*
    var url = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/msdyn_projects(" + newid + ")?$select=new_2vpnapplicable";
    console.log(url);
    req.open("GET", url, true);
    */
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                 var result = JSON.parse(this.response); // you will get the retrieved value in object we stored in result var.

                var retrivedvalue = result.new_2vpnapplicable; //get the id of the field
                var retrivedformatedvalue = result["new_2vpnapplicable@OData.Community.Display.V1.FormattedValue"]; //get the formatted name of the field
                if (retrivedvalue != null) {
                    var value = new Array();
                    value[0] = new Object();
                    value[0].id = retrivedvalue;
                    value[0].name = retrivedformatedvalue;
                    value[0].entityType = "msdyn_projects";
                    Xrm.Page.getAttribute("new_2vpnapplicable").setValue(fieldValueToCopy);   //set the lookup value finally
                } else
                    alert("some textt!!!!!!") // optional
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
}

***** End of script

Tracing the code, I can observed the first pass through, the value of this.readyState is not 4.  After hitting req.send(), the code loops back a few times before this.readyState is equal to 4 and proceeds.  Any ideas as to why the update on line 45 is not occurring?

*This post is locked for comments

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updating an Option Set field in Projects based on change of option set in Entity Items

    Problem resolved..

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans