+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Copy fields values to other record

Hello All,
Going in circles over how to resolve this.
I have Object Projects.  
The Project has a lookup to an Account
The account can reside on multiple Projects

So Project 1, Project 2, Project 3 all have the same loopup record 'Account Test'
So if field 'user' is Updated on Project 1.  How can I update the 'user' field on Project 2 and Project 3. 

Cheers,
P


 
Best Answer chosen by Phuc Nguyen 18
Suraj Tripathi 47Suraj Tripathi 47
Hi Phuc,

Check this code:-
trigger updateUser on projects__c(before update){
  map<id,id> accidVsOwnerid = new map<id,id>();
  for(project__c pro: trigger.old){
   if(oldMap.get(pro.id).OwnerId!=newMap.get(pro.id).OwnerId){
      map.put(pro.accountid__c,newMap.get(pro.id).OwnerId)
   }
 }
 List<account> accList=[select name,ownerid from account where id IN: accidVsOwnerid.keyset()];
 List<project__c> proList=[select name.ownerid,accountid__c from project__c where accountid__c IN: accidVsOwnerid.keyset()];
 List<project__c> updateList= new List<project__c>();
 for(account acc: accList){
   for(project__c pro: proList){
      if(pro.accountid__c==acc.id){
        pro.ownerid=accidVsOwnerid.get(acc.id);
        updateList.add(pro);
      }
    }
 }
 if(updateList.size()>0){
  update updateList;
 }
}

​​​​​​​In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer.