function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mike1983mike1983 

Simple Trigger Question

Ok So I have 2 objects X and Y. When 2 fields on X are updated I want to Change 2 fields on Y and I am having a bit of trouble wrapping my head arround the code. Both X and Y have a field that is a unique Idenfier and they are the same for both objects. so X and Y may have a field "uniqueId" and if they are tied its = 123 for both. Heres what I am thinking but it seems to be failing can someone tell me whats wrong here. 

 

 

trigger UpdateYwhenXChanges on X__c (after update) { List<Y__c> yObj= new List<Y__c>(); for (X__c changeRec: Trigger.New) { yObj.update(y__c(field1=changeRec.field1, field2=changeRec.field2 where UniqueId=changeRec.UniqueId)); update yObj; } }

 

Any Help or just a code sample of something similar would be great.


Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
kprkpr
If X and Y share a unique id, first get the Id value from X(which you get from Trigger.new). Then query for Y using this Id. then use the resulting sObject you get from the query to update Y.