+ Start a Discussion

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.



Best Answer chosen by Admin (Salesforce Developers) 
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.