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

8 Comments

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:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

8 Responses to Set and Retrieve a Lookup Id and Value using Xrm.WebApi

  1. Thanks, I have returned to, and used the code on this page a few times, however:

    lookup[0] = new object;
    needs to be:
    lookup[0] = new Object;

    The lowercase o gets me every time!

  2. Very useful. I couldn’t find how to get the guid of the lookup anywhere else – spent all afternoon looking!

    Syntax seems odd though, why does one have to use _fieldname_value rather than just fieldname?

    Is there a more ‘standard’ way to get this?

    Thanks again.

  3. Hi Carl,
    Need to know that if we retrieve a data from CRM rest builder in (retrieving multiple record) how to pass the data from their to other function.

  4. Hello Carl, I wrote you in another post, about how to do somthing like this but ForEach line or element. How the code would be. thanks!

Leave a Reply

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