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,...
Answered

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

(0) ShareShare
ReportReport
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.

I have the same question (0)
  • Verified answer
    Ray Profile Picture
    1,537 on at

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

  • Tamilarasu Arunachalam Profile Picture
    587 on at

    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

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 171 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 83

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans