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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

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

    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans