Announcements
HI Everyone
I am new to development and trying to get option set value from account to lead on existing account selection but my JS is not working , can any one suggest what is the mistake here:
function retrieveData(executionContext) {
debugger;
var formcontext = executionContext.getFormContext();
var accountId = formcontext.getAttribute("parentaccountid").getValue();
if (accountId != null) {
Xrm.WebApi.retrieveRecord("account", accountId[0].id, "?$select=name,ownershipcode").then(
function success(result) {
var accountOwner = result("ownershipcode@OData.Community.Display.V1.FormattedValue");
formcontext.getAttribute("new_ownership").setValue(accountOwner);
},
function (error) {
console.log(error.Message);
}
);
}
}
Thank you all it worked
Hi Beginner Developer,
Please try Bipin Kumar's code. It could work on my side. Just pay attention to one thing. The items' value of the new ownership field on lead entity should be the same with that field on account entity.
Something like these screenshots:
Ownership on the Account table:
Custom field on the Lead entity:
Below is my test:
Thank you Guido!
Yes , both are option set only
but not populating any value
Hi,
Try below code
function retrieveData(executionContext) {
debugger;
var formcontext = executionContext.getFormContext();
var accountId = formcontext.getAttribute("parentaccountid").getValue();
if (accountId != null) {
Xrm.WebApi.online.retrieveRecord("account", accountId[0].id, "?$select=name,ownershipcode").then(
function success(result) {
var ownershipcode = result["ownershipcode"]; // Choice
formcontext.getAttribute("new_ownership").setValue(ownershipcode);
},
function(error) {
console.log(error.message);
}
);
}
}
main problem is the syntax on how you retrieve the value, you should use square brackets, so code will be
var accountOwner = result["ownershipcode@OData.Community.Display.V1.FormattedValue"];
second is the type of the field new_ownership, because you are setting the text of the ownershipcode right now, if is an optionset too (with the same values or not) change the way to set the field and the value to fetch (not gettting the formattedvalue but just the value)
André Arnaud de Cal...
294,099
Super User 2025 Season 1
Martin Dráb
232,866
Most Valuable Professional
nmaenpaa
101,158
Moderator