Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Auto Populate lookup field

(0) ShareShare
ReportReport
Posted on by 7

Hi,
I keep tried to find lookup populate JS, but there are not much of detailed descriptions


lookup Field A : phone num (custom entity)

lookup Field B : Name of Person (contact)

i try to make a JS to populate when i type 'Field A' than 'Field B' populate.


pastedimage1668472644305v1.png

inside 'Field A' has 'Field B' (contact) lookup field.

pastedimage1668472762701v2.png

  • Suggested answer
    MikeC282 Profile Picture
    MikeC282 2,115 on at
    RE: Auto Populate lookup field
    [quote user="Uneducated Programmer"

    Andrew Butenko 
    appriciate to your help, but i don't have good skills :(


    It's "?$select=_cr337_patient_name_01_value"

    i repair my codes. But, it still i put 'lookup A' not showing 'lookup B'

    function setLookup(executionContext) {

        var formContext = executionContext.getFormContext();

        var lookupAValue = formContext.getAttribute("cr337_phonenum_01").getValue();

    if (lookupAValue != null) {


       Xrm.WebApi.online.retrieveMultipleRecords("cr337_nok_phonenum_01",id , "?$select=_cr337_patient_name_01_value").then(
        function success(results) {
            console.log(results);
            var lookup = [;

                lookup[0 = {};

                lookup[0.id = id;

                lookup[0.entityType = cr337_nok_phonenum_01;

                lookup[0.name = _cr337_patient_name_01_value;
           
                formContext.getAttribute("cr337_patient_01").setValue(lookup);

        },
        function(error) {
            console.log(error.message);
                }
            );
        }
    }




    [/quote

    Use this:

    function setLookup(executionContext) {
    
        var formContext = executionContext.getFormContext();
    
        var lookupAValue = formContext.getAttribute("cr337_phonenum_01").getValue();
    
    if (lookupAValue != null) {
    
        var id = lookupAValue[0].id; // you literally forgot to declare and set a value for the id variable being used in the the webApi call below
    // It should be retrieveRecord not retrieveMultipleRecords. You're getting a single record by its id, not multiple records by some filters
       Xrm.WebApi.online.retrieveRecord("cr337_nok_phonenum_01",id , "?$select=_cr337_patient_name_01_value").then( 
        function success(result) {
            console.log(result);
            // If the selected field is a lookup like a contact lookup field then the result comes with 3 properties: id, entityType and name
    		var cr337_patient_name_01 = result["_cr337_patient_name_01_value"]; // Lookup
    		var cr337_patient_name_01_formatted = result["_cr337_patient_name_01_value@OData.Community.Display.V1.FormattedValue"];
    		var cr337_patient_name_01_lookuplogicalname = result["_cr337_patient_name_01_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
            
                var lookup = [];
                lookup[0] = {};
    
                //lookup[0].id = id; This is wrong you're using the ID of the phone number record instead of the contact off course this won't set the lookup for the contact...
    
                //lookup[0].entityType = cr337_nok_phonenum_01;
    
                //lookup[0].name = _cr337_patient_name_01_value;
    
                lookup[0].id = cr337_patient_name_01; // This is the correct way to set the lookup
    
                lookup[0].name = cr337_patient_name_01_formatted;
    
                lookup[0].entityType = cr337_patient_name_01_lookuplogicalname;
    
    
           
                formContext.getAttribute("cr337_patient_01").setValue(lookup);
    
        },
        function(error) {
            console.log(error.message);
                }
            );
        }
    }

    I included in the script some comments on what you did wrong and commented out the bits that you wrote wrong and added some comments to it. I would advise potentially doing a JavaScript course first before attempting anymore client side scripting in Dynamics.

    Kind regards,

    Mike

  • RE: Auto Populate lookup field

    a33ik 
    appriciate to your help, but i don't have good skills :(


    It's "?$select=_cr337_patient_name_01_value"

    i repair my codes. But, it still i put 'lookup A' not showing 'lookup B'

    function setLookup(executionContext) {

        var formContext = executionContext.getFormContext();

        var lookupAValue = formContext.getAttribute("cr337_phonenum_01").getValue();

    if (lookupAValue != null) {


       Xrm.WebApi.online.retrieveMultipleRecords("cr337_nok_phonenum_01",id , "?$select=_cr337_patient_name_01_value").then(
        function success(results) {
            console.log(results);
            var lookup = [];

                lookup[0] = {};

                lookup[0].id = id;

                lookup[0].entityType = cr337_nok_phonenum_01;

                lookup[0].name = _cr337_patient_name_01_value;
           
                formContext.getAttribute("cr337_patient_01").setValue(lookup);

        },
        function(error) {
            console.log(error.message);
                }
            );
        }
    }




  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Auto Populate lookup field

    Are you sure that you used Dataverse Rest Builder and that the field type is a lookup?

    When the field you try to query is a lookup Dataverse Rest Builder should give you the query like ?$select=_cr337_patient_name_01_value

    not like ?$select=cr337_patient_name_01

  • RE: Auto Populate lookup field

    Bipin Kumar 
    i downloaded Dataverse REST on my instance , and i try to build code with your advise

    but it still not worked. . .

    function LookupValue(executionContext){

       var formContext = executionContext.getFormContext();

       var lookupvalue1= formContext.getAttribute("cr337_phonenum_01");

       if (lookupvalue1 != null) {

       var lookup = lookupvalue1.getValue();

       var id = lookup[0].id;

       if ((lookup != null)) {

       Xrm.WebApi.retrieveRecord("cr337_nok_phonenum_01", id, "?$select=cr337_patient_name_01").then(

           function success(results) {

       {

           var lookup = []; // // Creating a new lookup Array

           lookup[0] = {}; // new Object  

           lookup[0].id = id; // GUID of the lookup id

           lookup[0].name = results.cr337_patient_name_01; // Name of the lookup

           lookup[0].entityType = "cr337_nok_phonenum_01"; // Entity Type of the lookup entity

           formContext.getAttribute("cr337_contact_01").setValue(lookup);

                   }

               });

           }  

       }

    }

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,961 Moderator on at
    RE: Auto Populate lookup field

    Hi,

    Did you try to do what I suggested?

    Are you still facing an issue?

    Please provide your source code ...

    Have you download Dataverse rest builder zip file and imported to your instance?

  • RE: Auto Populate lookup field

    a33ik 

    'cr337_patient_name_01' is lookup field

  • Suggested answer
    Saeid G Profile Picture
    Saeid G 144 on at
    RE: Auto Populate lookup field

    I am assuming Lookup Field B has the value of Field A since you want to dynamically populate A after B, right? If that is the case you do not need to use WebApi.

    Either Filter the Lookup B

    formContext.getControl(arg).addCustomFilter(filter, entityLogicalName)

    learn.microsoft.com/.../addcustomfilter

    or create Custom View

    formContext.getControl(arg).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault)

    learn.microsoft.com/.../addcustomview

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,961 Moderator on at
    RE: Auto Populate lookup field

    Hi,

    You need to check my third link I shared earlier.

    var lookup = [];

       lookup[0] = {};

       lookup[0].id = Id;

       lookup[0].entityType = EntityType;

       lookup[0].name = Name;

    You will get Id, EntityType and Name from result.

    Then use below code

    formContext.getAttribute("").setValue(lookup);

    You can also generate code using Dataverse reset builder tool

    carldesouza.com/.../

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Auto Populate lookup field

    That won't work this way.

    If you want to reference the field from the result it will look like the following:

    formContext.getAttribute("cr337_patient_01").setValue(result.cr337_patient_name_01);

    What type of field is cr337_patient_name_01?

  • RE: Auto Populate lookup field

    thanks to answer my question.
    but, i think, this way is a little bit different with what i want to build.

    it's scenario about what i want to make JS like,

    when after insert or typing 'lookup field A'. after that, auto populate 'lookup field B'. 

    I try to make JS like this. but, it's not worked...

    function setLookup(executionContext) {

        var formContext = executionContext.getFormContext();

        var lookupAValue = formContext.getAttribute("cr337_phonenum_01").getValue();

        var lookupAGuid = lookupAValue [0].id.replace('{','').replace('}','');

    if (lookupAValue != null) {

        Xrm.WebApi.online.retrieveRecord("cr337_nok_phonenum_01", lookupAGuid, "?$select=cr337_patient_name_01").then(
            function success(result) {

            formContext.getAttribute("cr337_patient_01").setValue(cr337_patient_name_01);

            });
        }
    }




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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,387 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans