locked
Populate Lookup on Service Activity with Lookup on Case Form - Web API RRS feed

  • Question

  • Hi

    I am trying to use the new Web Api to populate the resource lookup on the service activity form with a value that is stored within a Lookup attribute on the Case form.

    I have managed to write the JavaScript which displays the value in an alert. I just need to use this value to populate the field 'resource' on the Service Activity form.

    Please find herewith the script used to display the value in an alert:

    function getPropertysetResource() {
           debugger;
    	var serverURL = Xrm.Page.context.getClientUrl();
    	var queryEntity = "incidents";
    	var Lookup = Xrm.Page.getAttribute("regardingobjectid").getValue();
        var Guid = Lookup[0].id.substring(1, 37);
    	var selectOptions = "$select=_new_propertyid_value";
        var req = new XMLHttpRequest();
    	req.open("GET", encodeURI(serverURL + "/api/data/v8.1/" + queryEntity + "(" + Guid + ")?" + selectOptions), 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=OData.Community.Display.V1.FormattedValue");
    	req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var data = JSON.parse(this.response);{
                	alert(data["_new_propertyid_value@OData.Community.Display.V1.FormattedValue"]);
    				//Xrm.Page.getAttribute("new_propertyid_value").setValue(results["resources"]);
                }
    			}
            else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }

    It would seem that a need to use:
    @odata.bind

    I would really appreciate it if someone could point me in the right direction.

    Thanks

    Tony

    Thursday, March 30, 2017 1:42 PM

Answers

  • Hi

    I realised the error with the above script, I was trying to populate a field with a different entitytype that is available on the lookup.

    I created a business rule to populate a resource lookup on the case form based on the value selected for property.

    I then used the following script to populate the resource on the service activity form:

    function getPropertysetResource() {
        debugger;
        var serverURL = Xrm.Page.context.getClientUrl();
        var queryEntity = "incidents";
        var Lookup = Xrm.Page.getAttribute("regardingobjectid").getValue();
        var Guid = Lookup[0].id.substring(1, 37);
        var selectOptions = "$select=_new_facilityid_value";
        var req = new XMLHttpRequest();
        req.open("GET", encodeURI(serverURL + "/api/data/v8.1/" + queryEntity + "(" + Guid + ")?" + selectOptions), 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 data = JSON.parse(this.response);{
                    	 Xrm.Page.getAttribute('resources').setSubmitMode("always");
                        Xrm.Page.getAttribute('resources').setValue([{
                            id:data["_new_facilityid_value"],                                                                                                                                                                             
                            name:data["_new_facilityid_value@OData.Community.Display.V1.FormattedValue"],
                            entityType:data[ "_new_facilityid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]
                    }]);
                }
            }
            else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }

    Working 100% now

    Thanks
    Tony

    • Marked as answer by TonySim Friday, March 31, 2017 8:17 AM
    Friday, March 31, 2017 8:17 AM

All replies

  • Hi Tony,

    To set a look up value in javascript you need to follow the below procedure;

    var lookup = new Array();
    lookup[0] = new Object();
    lookup[0].id = recorid;
    lookup[0].name = recordname;
    lookup[0].entityType = entityname;
    Xrm.Page.getAttribute("attributename").setValue(lookup);

    Thanks


    Sachith Chandrasiri

    Thursday, March 30, 2017 9:56 PM
  • Hi Sachith

    Thanks for your reply, I am aware of those methods however I seem to think there is more preferred way of populating the lookup using the Web API

    I am now using the following code:

    function getPropertysetResource() {
        debugger;
        var serverURL = Xrm.Page.context.getClientUrl();
        var queryEntity = "incidents";
        var Lookup = Xrm.Page.getAttribute("regardingobjectid").getValue();
        var Guid = Lookup[0].id.substring(1, 37);
        var selectOptions = "$select=_new_propertyid_value";
        var req = new XMLHttpRequest();
        req.open("GET", encodeURI(serverURL + "/api/data/v8.1/" + queryEntity + "(" + Guid + ")?" + selectOptions), 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 data = JSON.parse(this.response);{
                    	 Xrm.Page.getAttribute('resources').setSubmitMode("always");
                        Xrm.Page.getAttribute('resources').setValue([{
                            id:data["_new_propertyid_value"],                                                                                                                                                                             
                            name:data["_new_propertyid_value@OData.Community.Display.V1.FormattedValue"],
                            entityType:data[ "_new_propertyid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]
                    }]);
                }
            }
            else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    

    This code almost works, the field is populated with the correct value however the value does not appear 100%.

    Any suggestions would be greatly appreciated.

    Thanks
    Tony

    Friday, March 31, 2017 7:24 AM
  • Hi

    I realised the error with the above script, I was trying to populate a field with a different entitytype that is available on the lookup.

    I created a business rule to populate a resource lookup on the case form based on the value selected for property.

    I then used the following script to populate the resource on the service activity form:

    function getPropertysetResource() {
        debugger;
        var serverURL = Xrm.Page.context.getClientUrl();
        var queryEntity = "incidents";
        var Lookup = Xrm.Page.getAttribute("regardingobjectid").getValue();
        var Guid = Lookup[0].id.substring(1, 37);
        var selectOptions = "$select=_new_facilityid_value";
        var req = new XMLHttpRequest();
        req.open("GET", encodeURI(serverURL + "/api/data/v8.1/" + queryEntity + "(" + Guid + ")?" + selectOptions), 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 data = JSON.parse(this.response);{
                    	 Xrm.Page.getAttribute('resources').setSubmitMode("always");
                        Xrm.Page.getAttribute('resources').setValue([{
                            id:data["_new_facilityid_value"],                                                                                                                                                                             
                            name:data["_new_facilityid_value@OData.Community.Display.V1.FormattedValue"],
                            entityType:data[ "_new_facilityid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]
                    }]);
                }
            }
            else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    }

    Working 100% now

    Thanks
    Tony

    • Marked as answer by TonySim Friday, March 31, 2017 8:17 AM
    Friday, March 31, 2017 8:17 AM