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&amp;$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;$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:
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
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!
Hi Graham, good catch! I’ve updated the code now. Thanks for letting me know! – Carl
Very useful .this post solved my all doubts
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.
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.
Thanks Carl, very valuable blog!
BTW – _ownerid_value@OData.Community.Display.V1.FormattedValue this seems not working whereas other parts working nicely. Any idea why?
I am working with single record fetch instead of multiple.
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!
How can you do similar in the portal, I mean retrieve the lookup field selected value?