Updating Lookup Fields in JavaScript using Xrm.WebApi

2 Comments

In this post, we will look at how to update Lookup Fields in a Dynamics 365 Power Apps record using the Xrm.WebApi.

Let’s say we have an Opportunity record. This record has 3 lookup fields:

  • Contact
  • Account
  • Currency

For simplicity, let’s hardcode the Ids of the new records we will be associating to our Opportunity:

  • Our new Contact called Maria Campbell is c0993617-c85d-ea11-a811-000d3a579ca1
  • Our new Account called Blue Yonder Airlines is 5c993617-c85d-ea11-a811-000d3a579ca1
  • Our new Currency Euro is 1C2A5F41-0D5C-EA11-A811-000D3A5694CA

And the Opportunity we’re updating is 8a9a3617-c85d-ea11-a811-000d3a579ca1.

Note on the Opportunity, our fields have the following names:

  • Contact = parentcontactid
  • Account = parentaccountid
  • Currency = transactioncurrencyid

These fields are of entity types Contact, Account and Transaction Currency, respectively.

Now the code. We will use the notation fieldname@data.bind: “/entitypluralname(Id)” in our object to update our record:

var ContactId = "c0993617-c85d-ea11-a811-000d3a579ca1";
var AccountId = "5c993617-c85d-ea11-a811-000d3a579ca1";
var CurrencyId = "1C2A5F41-0D5C-EA11-A811-000D3A5694CA";
var OpportunityId = "8a9a3617-c85d-ea11-a811-000d3a579ca1";

var UpdateObject = {
    "parentcontactid@odata.bind": "/contacts(" + ContactId + ")",
    "parentaccountid@odata.bind": "/accounts(" + AccountId + ")",
    "transactioncurrencyid@odata.bind": "/transactioncurrencies(" + CurrencyId + ")" 
};

Xrm.WebApi.updateRecord("opportunity", OpportunityId, UpdateObject).then(
    function success(result) {
        console.log("success"); 
    },
    function error(result) {
        console.log(result);                            
});

When we run this, we get a Success:

And on refreshing our record we see the lookup fields have been updated.

 

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

 

2 Responses to Updating Lookup Fields in JavaScript using Xrm.WebApi

  1. hello sir ,my question is that my suppose i created a lead record and in this lead record i have some fields and out of those all field one field is of type lookup.
    then i qualify this lead record and an opportunity is created .
    now in this opportunity if i update the lookup field value then this value must be updated in the lead record as well.
    hope you will reply soon. thankyou have a nice day

    • Hey Gaurav,
      In this case you can trigger Microsoft flow to update record Automatically, whenever you change the particular field of opportunity,update the Lead Record.

Leave a Reply

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