Skip to main content

Notifications

Microsoft Dynamics CRM forum
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,477 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

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,522 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,441 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans