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...
Unanswered

Javascript to retrieve related entity record using KeyValues not working

(0) ShareShare
ReportReport
Posted on by 5

In CRM, I have written a JavaScript on Account entity form, to auto-populate related Primary Contact when Parent Account is selected however it is not populating any value in Primary Contact lookup and its without error. What is wrong in the script?

function relatedcontacts(executionContext)
{
var formContext = executionContext.getFormContext();
if (formContext.getAttribute("parentaccountid").getValue() != null)
{
var prntacc = formContext.getAttribute("parentaccountid").getValue()[0].keyValues;
primarycont = prntacc.primarycontactid;
formContext.getAttribute("primarycontactid").setValue(primarycont);
}
else
{
return false;
}
}

 

Also what are the possible ways to retrieve related entity value using Javascript ? 

I have the same question (0)
  • LeoAlt Profile Picture
    16,331 Moderator on at

    Hi partner,

    If you want to set a value to lookup field, please use the following format.

    // GET and SET Lookup / Customer field value
    var functionName = function (executionContext) {

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

        // 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 reocord_id = field.getValue()[0].id;
            var record_name = field.getValue()[0].name;
            var record_entityName = field.getValue()[0].entityType;

            // Set its field value
            field.setValue([{
                id: reocord_id,
                name: record_name,
                entityType: record_entityName
            }]);
        }
    }

    BTW, if  you want to get related entity value, you could use web api to do this.

    1.Get the related record id from lookup field.

    2.Get the related record data from web api. For example I want to get the full name of contact by contactid.

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

        //get if the contact allows to have a phone call.
        Xrm.WebApi.retrieveRecord("contact""91af308b-a11b-ea11-a828-000d3a579c9f","?$select=fullname").then(
            function success(result) {
                var firstname=result["fullname"];
                

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


    }

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/retrieverecord

    Best Regards,

    Leo

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
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 80

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans