Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Auto Populate lookup field

Posted on by 5,005

Hi

Scenario 

Field a: lookup field 

Field b: lookup field 

Based on lookup field a . value should autopopulate in lookup field b .

How is that possible . any suggestions will be marked as verified 

ASAP

  • Suggested answer
    RE: Auto Populate lookup field

    Hi ,

    Please Do watch  this Blog for Auto Populating Lookup Field Values

    dynamics365starsolutions.blogspot.com/.../auto-populate-lookup-values-using.html

    Hope it is Helpful for You

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Auto Populate lookup field

    Hi,

    use below code to fill lookup B based on lookup A

    function fillLookup(executionContext)
    {
      var formContext = executionContext.getFormContext();
    
      var lookup = formContext.getAttribute("new_state").getValue();
      if (lookup != null) 
      {
    	var lookunfieldStateId=lookup[0].id;
    	Xrm.WebApi.online.retrieveMultipleRecords("new_city", "?$select=new_name&$filter=_new_state_value eq " lookunfieldStateId "&$orderby=createdon desc&$top=1").then(
        function success(results) {
            for (var i = 0; i < results.entities.length; i  ) {
                var new_name = results.entities[i]["new_name"];
    			var new_cityid = results.entities[i]["new_cityid"];
    			var value = new Array(); //create a new object array
    			value[0] = new Object();
    			value[0].id = new_cityid; // set ID to ID
    			value[0].name = new_name; //set name to name
    			value[0].entityType = "new_city"; //optional
    			formContext.getAttribute("new_city").setValue(value);
            }
        },
        function(error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );
      }
    }

    I have use order by created on field desc and applied top1 to get first record. You can change this as per your logic

    Add this function on onchange of Lookup field A.

    lookup01.PNG

    lookup02.PNG

    lookup03.PNG

    I have tested this code my trial instance and it is working.

    If found helpful, Please mark my answer verified.

  • sandeepc Profile Picture
    sandeepc 5,005 on at
    RE: Auto Populate lookup field

    Any suggestions

  • sandeepc Profile Picture
    sandeepc 5,005 on at
    RE: Auto Populate lookup field

    When I open record and select lookup field a . it should auto populate . both the fields are mandatory and workflow also did not triggered

  • sandeepc Profile Picture
    sandeepc 5,005 on at
    RE: Auto Populate lookup field

    But value should display before creating record or updating record it should auto populate .

  • Suggested answer
    Carlo Velasco Profile Picture
    Carlo Velasco 787 on at
    RE: Auto Populate lookup field

    Hi I believe that the best approach would be using a business rule.

    docs.microsoft.com/.../create-business-rules-recommendations-apply-logic-form

  • sandeepc Profile Picture
    sandeepc 5,005 on at
    RE: Auto Populate lookup field

    Can you pls help me by sending links or any similar code which will be helpful

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Auto Populate lookup field

    Hi,

    If we have relationship added between Lookup A entity record with Lookup B entity record then we can write JS on change of Lookup field A and get Lookupfield B using xrm.webpi. Then set the value of Lookup B.

    Question is what if we have multiple records for a lookup field A value? Which value to be to autopopulated?

    Is it based on modified on/created if that is the case you could add Top1 in web api.

    If found helpful, Please mark my answer verified.

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

Featured topics

Product updates

Dynamics 365 release plans