I am trying to retrieve a value from a related record and assign it to a field on a form. For example on a form called Customer Asset I have two fields 'Asset Owner' and 'Site', if I click on the value in the 'Site' field it takes me to the 'Site' form where there is a field called 'Parent Account' this contains a value, I would like to retrieve the 'Parent Account' value and assign it to the 'Asset Owner' field in the Customer Asset form
I am a beginner and trying to create a JS that will assign the value from the 'Parent Account' field to the 'Asset Owner' field.
Can anyone let me know what is wrong with my code?
assignSiteValueToAssetOwner: function (executionContext) {
var formContext = executionContext.getFormContext();
var assetOwner = formContext.getAttribute("dnz_assetowner").getValue();
Xrm.WebApi.online.retrieveMultipleRecords("msdyn_customerasset", "?$expand=msdyn_account($select=_parentaccountid_value)&$filter=msdyn_customerassetid eq 00000000-0000-0000-0000-000000000000").then(
function success(results) {
console.log(results); for (var i = 0; i < results.entities.length; i++) {
var result = results.entities[i];
// Columns
var msdyn_customerassetid = result["msdyn_customerassetid"]; // Guid
// Many To One Relationships
if (result.hasOwnProperty("msdyn_account")) {
var msdyn_account_parentaccountid = result["msdyn_account"]["_parentaccountid_value"]; // Lookup
var msdyn_account_parentaccountid_formatted = result["msdyn_account"]["_parentaccountid_value@OData.Community.Display.V1.FormattedValue"];
var msdyn_account_parentaccountid_lookuplogicalname = result["msdyn_account"]["_parentaccountid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
}
if (assetOwner === null) {
formContext.getAttribute('dnz_assetowner').setValue(formContext.getAttribute('msdyn_parentaccountid').getValue());
}
}
})
},
function(error) {
console.log(error.message);
},
Hi Matt,
Based on your description, you want to get lookup field(Parent Account) from another lookup field(Site) and insert into new lookup field(Asset Owner).
Parent Account is one lookup field related to account, the Asset Owner must be also one lookup field related to account!(I'm confused because of its naming), because only lookup fields from the same entity can be populated.
And you can use CRM REST Builder to build retrieve code easily: https://github.com/jlattimer/CRMRESTBuilder
You can refer following link, which provides detailed code for similar scenarios:
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156