Set and Retrieve a Lookup Id and Value using Xrm.WebApi

Leave a comment

We can use JavaScript to connect to Dynamics 365 and set and retrieve a lookup id and value.

Let’s say on an Account, we have the Owner field, which is a lookup:

We would like to set the value of this lookup field through JavaScript. Let’s say we want to set it to another system user, Alan Steiner. To set the value, we will need to know Guid of Alan’s record, and the name of the record. We already know the name is Alan, but let’s retrieve it anyway through code, as we may need this in other scenarios.

We will run this on changing of the website field, just to invoke our code. The code below will grab the owner of 3M accounts:

Xrm.WebApi.retrieveMultipleRecords("account", "?$select=_ownerid_value&$filter=name eq '3M'").then(
    function success(result) {
        for (var i = 0; i < result.entities.length; i++) {
            alert(result.entities[i]._ownerid_value);
            break; // We will grab the first one
        }
    },
        function(error) {  
            alert("Error: " + error.message);  
        }  
    );

In running the code above, we get the Id, Value, logical name etc:

In previous releases of the Web Api, we would need to pass (“Prefer”, “odata.include-annotations=OData.Community.Display.V1.FormattedValue”) in our request header in order to get the value of the lookup and not just the Id. This is not required when using the Xrm.WebApi.

Setting the Field

Now let’s set the value of the owner field by creating a new array.

In the above example we could do something like this, using the FormattedValue and other fields:

        for (var i = 0; i < result.entities.length; i++) {
            var p = result.entities[0];
            if (p != null) {
                var lookup = new Array();
                lookup[0] = new object;
                lookup[0].id = p["_ownerid_value"];
                lookup[0].name = p["_ownerid_value@OData.Community.Display.V1.FormattedValue"];
                lookup[0].entityType = p["_ownerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                Xrm.Page.getAttribute("ownerid").setValue(lookup);
            }

If we look up Alan’s systemuser record to get the Guid and name, we can set it on our field on the form like below:

Xrm.WebApi.retrieveMultipleRecords("systemuser", "?$select=fullname,ownerid&amp;amp;amp;$filter=fullname eq 'Alan Steiner'").then(
function success(result) {
for (var i = 0; i < result.entities.length; i++) {
var p = result.entities[0];
if (p != null) {
var lookup = new Array();
lookup[0] = new object;
lookup[0].id = p["_ownerid_value"];
lookup[0].name = p["fullname"];
lookup[0].entityType = p["_ownerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
Xrm.Page.getAttribute("ownerid").setValue(lookup);
}

break; // We will grab the first one
}
},
function(error) {
alert("Error: " + error.message);
}
);

In running this code, our owner is now set to Alan:

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *