Skip to main content

Notifications

Service | Customer Service, Contact Center, Fie...
Answered

Dynamics retrieve a value from related parent Asset and assign to a field

Posted on by

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);
    },
  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics retrieve a value from related parent Asset and assign to a field

    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:

    javascript - Load lookup field from another lookup field and insert into new field? - Dynamics 365 General Forum Community Forum

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans