Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

JavaScript that triggers based on field A on Entity A and sets Field B on Entity B

(0) ShareShare
ReportReport
Posted on by

So

I have a lookup field A for accounts on Entity A.
When somebody changes that field I want the same account to be set on Lookup field B on Entity B.

Trying to google some examples but can't find any for this scenario.

  • Suggested answer
    PabloCRP Profile Picture
    1,088 on at
    RE: JavaScript that triggers based on field A on Entity A and sets Field B on Entity B

    Hi, you must make so minor changes

    function serLookUp(context){
    var formContext = context.getFormContext();
    var lookupAName = "new_examplelookup";//Your lookup logicalName in entity A
    var lookupA = formContext.getAttribute(lookupAName).getValue();
    var entityNameB = "new_entity";//Your entity B logical Name  
    var entityNameBId = "9e39ca39-0d05-4700-9c21-23addfbf1f2b";//Your entity B GUID, You can retrive it with API or by passing it as parameter to this function
    var lookupBSchemaName = "new_ExampleEntityB";//Your lookup SCHEMA NAME in entity B
    var datalookupB = generateData(lookupA,entityNameBFieldSchemaName);
    Xrm.WebApi.updateRecord(entityNameB,entityNameBId,datalookupB).then(
    	function(res){
    		alert("Update success");
    	},
    	function(err){
    		console.log(err);
    	},
    );
    }
    
    function generateData(lookup,lookupBSchemaName){
    	var data = {};
    	var lookupBSchemaNameTemp = lookupBSchemaName "@odata.bind";
    	if(lookup!==null)
    		data[lookupBSchemaNameTemp] = "/accounts(" lookup[0].id.replace(/[{|}]/g,"") ")";
    	else
    		data[lookupBSchemaNameTemp] = null;
    
    	return data;
    }

    regards.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,245 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,925 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans