web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Option set value from account to lead on parent account for lead selection

(0) ShareShare
ReportReport
Posted on by 55

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);
}

);
}

}

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    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)

  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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);

    }

    );

    }

    }

  • Beginner Developer Profile Picture
    55 on at

    Thank you Guido!

    Yes , both are option set only

    but not populating any value

  • Suggested answer
    Community Member Profile Picture
    on at

    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:

    pastedimage1652160219906v2.png

    Custom field on the Lead entity:

    pastedimage1652160350336v3.png

    Below is my test:

    Set-Ownership.gif

  • Beginner Developer Profile Picture
    55 on at

    Thank you all it worked

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 41 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 39

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans