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.
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
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); } ); }
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,522 Super User 2024 Season 2
Martin Dráb 228,441 Most Valuable Professional
nmaenpaa 101,148