Navigation Property for Lookup Field in Xrm.WebAPI Create – How to use
In this below blog, you will come to know how to use assign value to single lookup field and polymorphic lookup field while passing data in Xrm.WebAPI request in Javascript for Dataverse Power Apps
Single Entity Lookup
If you want to assign value of a lookup field which is associated with a single entity then use below format.
“<SchemaName_of_LookupField>@odata.bind”:”/<setnameofentity>(<id_of_parent_record>)”
Example : “sbi_BankAccount@odata.bind”: “/sbi_sbibankaccounts(“+currentBankAccountRecId+”)”
Schema Name can ebe copied from Solution => Table -> Field -> Property -> Schema Name
NOTE :
Entity Set name can be copied using Advanced Tool from Power Apps Solution Entity Component. Navigate to your solution, select the entity from solution, click Tool and choose Copy Set Name.
Polymorphic Entity Lookup
If you want to assign polymorphic lookup field value like Customer datatype then you have to append target entity logical name with scena name of the column as given below.
“<SchemaName_of_LookupField>_<targetentitylogicalname>@odata.bind”:”/<setnameoftargetentity>(<id_of_parent_record>)”
Example : “sbi_Customer_contact@odata.bind”: “/contacts(<contactid>)”
“sbi_Customer_account@odata.bind”: “/accounts(<contactid>)”
FINAL CODE with Both Case
Here I have given a sample code with both lookup type. In this code Bank Account is a Single entity lookup but Customer is a polymorphic lookup. For Power Automate also when you assign you have to assign in correct way.
function createCreditCard(formContext)
{
var currentBankAccountRecId= formContext.data.entity.getId().replace("{","").replace("}","");
var customer = formContext.getAttribute("sbi_customer").getValue();
var customerid = customer[0].id.replace("{","").replace("}","");
var data =
{
"sbi_Customer_contact@odata.bind": "/contacts("+customerid+")",
"sbi_cardtype":918820001,
"sbi_BankAccount@odata.bind": "/sbi_sbibankaccounts("+currentBankAccountRecId+")",
"sbi_maximumlimit": 150000
}
// create account record
Xrm.WebApi.createRecord("sbi_sbicard", data).then(
function success(result) {
alert("Credit card is created with ID: " + result.id);
// perform operations on record creation
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
}
In Power Automate, you have to assign corrctly as given below screenshot.