web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archive)
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)
  • Tamilarasu Arunachalam Profile Picture
    558 on at
    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
    1,390 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

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Blog subscriptions now enabled!

Follow your favorite blogs

TechTalk: How Dataverse and Microsoft Fabric powers ...

Explore the latest advancements in data export and integration within ...

Leaderboard > 🔒Ι Microsoft Dynamics CRM (Archived)

Featured topics

Product updates

Dynamics 365 release plans