Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Populate lookup field based on another lookup field on the form.

Posted on by 60

I have two lookup on the form lets Say A  and B.

I need to populate B based on the value in A as A has a main form where this lookup B is populated with the value and that value should populate in my form where both this field are present

I cannot use mapping for it as B and A is 1:N related and user is not allowed to fill B first so that we can populate A. but vice versa is needed.

I hope I have cleared my requirements

Can i get any JavaScript sample code . I am pretty new to it so needed help

Thanks in advance.

  • RE: Populate lookup field based on another lookup field on the form.

    Hi  DevCrmUser,

    Please go through the below link to get the clear idea on how to autopopulate lookup by another lookup with fireOnChange()

    https://www.tamilarasu.me/2022/12/auto-populate-lookup-based-on-other.html

    if this helps you please like and verify my answer

  • Verified answer
    Ray Profile Picture
    Ray 1,505 on at
    RE: Populate lookup field based on another lookup field on the form.

    As my understand, you want to get the B from A's record and set value for B? here is a demo code for you, this function should bind to the OnChange event of A, you may need change some of the variables:

    function fieldAOnChange(executionContext) {
    	var formContext = executionContext.getFormContext();
    	// Get the value of lookup field A
    	var lookupValue = formContext.getAttribute("lookupA").getValue();
    
    	// Get the record associated with lookup field A
    	var lookupRecord = lookupValue[0].id;
    
    	// Retrieve the value of field B from the associated record
    	Xrm.WebApi.retrieveRecord("entitynameA", lookupRecord, "?$select=new_fieldB").then(
    		function success(result) {
    			// Set the value of lookup field B
    			formContext.getAttribute("lookupB").setValue([{ id: result.new_fieldB, name: result.new_fieldBName, entityType: "entitynameB" }]);
    		},
    		function (error) {
    			console.log(error.message);
    		}
    	);
    }
    
    

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans