Skip to main content

Notifications

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

);
}

}

  • Beginner Developer Profile Picture
    55 on at
    RE: Option set value from account to lead on parent account for lead selection

    Thank you all it worked

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Option set value from account to lead on parent account for lead selection

    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
    RE: Option set value from account to lead on parent account for lead selection

    Thank you Guido!

    Yes , both are option set only

    but not populating any value

  • Verified answer
    Bipin D365 Profile Picture
    28,977 Moderator on at
    RE: Option set value from account to lead on parent account for lead selection

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

    }

    );

    }

    }

  • Suggested answer
    Guido Preite Profile Picture
    54,077 Moderator on at
    RE: Option set value from account to lead on parent account for lead selection

    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)

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,099 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans