Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

javascript - Load lookup field from another lookup field and insert into new field?

Posted on by 880

Hi,

I have my work order entity. On this i have the service account lookup field realted to account. On the account record i have a parent account field.

Also i have created a new field called "x_parentaccount" on my work order. I wan't the parent account from the selected service account to be populated into my new field x_parentaccount. I cannot get this to work?

// GET Lookup
function retrieveParentAccount(executionContext) {

    // Access the field on the form
    var field = executionContext.getFormContext().getAttribute("msdyn_serviceaccount");

    // Verify it does exist on the form
    if (field != null) {

        // Get its field value; Returns the Lookup object
        var value = field.getValue();

        // To get the Id, Name and Entity Name (account/contact)
        var record_id = field.getValue()[0].id;
        var record_name = field.getValue()[0].name;
        var record_entityName = field.getValue()[0].entityType;
        record_id=record_id.replace('{','').replace('}','');
        GetRelatedData(executionContext,record_id);

    }
}

    function GetRelatedData(executionContext,record_id) {
        var formContext = executionContext.getFormContext();

        //
        Xrm.WebApi.retrieveRecord("account", record_id, "?$select=parentaccountid").then(
            function success(result) {
                if (result.parentaccountid == true)
                    formContext.getAttribute(x_parentaccount).setValue(true);


            },
            function error(error) {
                Xrm.Navigation.openAlertDialog({
                    text: error.message
                });
            }
        );
    }

Another more simpl script was this - but cannot get this to work either...

function retrieveParentaccount(executionContext) {
	var formContext = executionContext.getFormContext();
    // Access the field on the form
    var fieldValue = formContext.getAttribute("msdyn_serviceaccount").getValue();
    // Verify it does exist on the form
    if (fieldValue != null) {
        // To get the Id, Name and Entity Name (account/contact)
        var record_id = fieldValue[0].id;
		
		Xrm.WebApi.retrieveRecord("account", record_id, "?$select=parentaccountid").then(
            function success(result) {
                if (result.parentaccountid == true) {
                    formContext.getAttribute("x_parentaccount").setValue(true);
				}
            },
            function error(error) {
                Xrm.Navigation.openAlertDialog({ text: error.message });
            }
        );
    }
}

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: javascript - Load lookup field from another lookup field and insert into new field?

    If you ask me I usually use CrmRestBuilder to pre-build the code of WebApi operations to avoid mistypes and use it my real code. It's not possible to build the whole code using CrmRestBuilder.

  • rskadk1 Profile Picture
    rskadk1 880 on at
    RE: javascript - Load lookup field from another lookup field and insert into new field?

    It works perfect by using the code you provided Leah Ju. A side question. I tried using the rest builder. and followed your instructions. However is it possible to build the entire code you provided with the tool? Or did you only build the retrieve code and inserted the other part your self?

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: javascript - Load lookup field from another lookup field and insert into new field?

    Hi rskadk1,

    As Andrew suggested, you can use CRM REST Builder to build retrieve code:

    Download page: https://github.com/jlattimer/CRMRESTBuilder

    1.retrieve Account:

     pastedimage1606273435356v1.png

    2.js code.

     

    function setParentAccount(executionContext) {
        var formContext = executionContext.getFormContext();
        var serviceAccount = formContext.getAttribute('msdyn_serviceaccount').getValue();
        var serviceAccount = serviceAccount[0].id.slice(1, -1);
    if(serviceAccount != null)
    {
    
        Xrm.WebApi.online.retrieveRecord("account", "" serviceAccount "", "?$select=_parentaccountid_value").then(
            function success(result) {
                var _parentaccountid_value = result["_parentaccountid_value"];
                var _parentaccountid_value_formatted = result["_parentaccountid_value@OData.Community.Display.V1.FormattedValue"];//name
                var _parentaccountid_value_lookuplogicalname = result["_parentaccountid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];//type
    
                if(_parentaccountid_value!=null)
                {
                    var value = new Array();
                    value[0] = new Object();
                    value[0].id = _parentaccountid_value;
                    value[0].name = _parentaccountid_value_formatted;
                    value[0].entityType = _parentaccountid_value_lookuplogicalname;
                    formContext.getAttribute("new_x_parentaccountid").setValue(value); //set the lookup value finally
                }
                else
                {
                    alert("No parent account");
                }
            },
            function(error) {
                Xrm.Utility.alertDialog(error.message);
            }
        );
    }
    }

    3.Test.

    pastedimage1606273483325v2.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: javascript - Load lookup field from another lookup field and insert into new field?

    To pregenerate the code that retrieves value of your parentaccountid you can use github.com/.../2.6.0.0

  • rskadk1 Profile Picture
    rskadk1 880 on at
    RE: javascript - Load lookup field from another lookup field and insert into new field?

    I looked at your link. I can’t see to figure out how I can modify my code to get it correct..

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: javascript - Load lookup field from another lookup field and insert into new field?

    Hello,

    You doesn't handle lookup response the right way. Check this post on how you should do it - carldesouza.com/.../

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans