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 :
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

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    First get the value of your lookup field in your javascript

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

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

    Now use CRM webapi.retrieverecord to fetch contact lookup from custom entity

    learn.microsoft.com/.../retrieverecord

    Now set the lookup B

    carldesouza.com/.../

  • Suggested answer
    MikeC282 Profile Picture
    2,175 on at

    If you need to get the contact lookup stored in the record in field A you need to use the web API to retrieve the lookup data to then use in your JS script. Note that it'll then be asynchronous so you'll need to handle that.

  • Uneducated Programmer Profile Picture
    7 on at

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

            });
        }
    }




  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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?

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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
    Saeid G Profile Picture
    144 on at

    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

  • Uneducated Programmer Profile Picture
    7 on at

    a33ik 

    'cr337_patient_name_01' is lookup field

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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?

  • Uneducated Programmer Profile Picture
    7 on at

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

                   }

               });

           }  

       }

    }

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans