+ Start a Discussion
SFDC Coder 8SFDC Coder 8 

Issue in Trigger while updating

Hi All,
I have two custom objects (Obj1__c & Obj2__c). I have account name as common in two objects.
I have three fields in Obj1 and Obj2.
When ever I am updating fileds in Obj1, I want to copy same information in Obj2 also.
I have written a trigger but its not copying fileds.
Here is my code
trigger updateFilds on Obj1__c (after update,before insert) {

      
  List<Id> accountIds = new List<Id>();


for(Obj1__c acc: trigger.new){
        accountIds.add(acc.AccId__c);
        
    }
    
    List<Obj2__c> obj2Lst= [Select Id,F1__c ,F2__c ,F3__c,Account_Name__C FROM Obj2__c where Account_Name__C IN:accountIds];
   
    if(Trigger.isUpdate ){
   
    for(Obj1__c new2: trigger.new){
        if(!obj2Lst.isEmpty()){
            new2.F1__c       = obj2Lst[0].F1__c ;
            new2.F2__c       = obj2Lst[0].F2__c ;
            new2.F3__c       = obj2Lst[0].F3__c ;
            
        }
    }     
    }   

}

Please help where it is going wrong.
Thanks in Advance
Best Answer chosen by SFDC Coder 8
sfdcMonkey.comsfdcMonkey.com
hi SFDC coder 8
where you can update obj 2 in your code after set value
try below code
trigger updateFilds on Obj1__c (after update,before insert) {
  List<Id> accountIds = new List<Id>();
for(Obj1__c acc: trigger.new){
        accountIds.add(acc.AccId__c);
    }
    
   List<Obj2__c>  LstForUpdate = new List<Obj2__c>();
   List<Obj2__c> obj2Lst= [Select Id,F1__c ,F2__c ,F3__c,Account_Name__C FROM Obj2__c where Account_Name__c IN:accountIds];
   
    if(Trigger.isUpdate ){
      
   
    for(Obj1__c new2: trigger.new){
        if(!obj2Lst.isEmpty()){
        
          for(Obj2__c Oobj2 :obj2Lst ){
          
            Oobj2.F1__c = new2.F1__c ;
			Oobj2.F2__c = new2.F2__c ;
			Oobj2.F3__c = new2.F3__c ;
              LstForUpdate.add(Oobj2);  
           }
        
        }
      }
      update LstForUpdate ;       
    } 
}
thanks
let me inform if it helps you

All Answers

sfdcMonkey.comsfdcMonkey.com
hi SFDC coder 8
where you can update obj 2 in your code after set value
try below code
trigger updateFilds on Obj1__c (after update,before insert) {
  List<Id> accountIds = new List<Id>();
for(Obj1__c acc: trigger.new){
        accountIds.add(acc.AccId__c);
    }
    
   List<Obj2__c>  LstForUpdate = new List<Obj2__c>();
   List<Obj2__c> obj2Lst= [Select Id,F1__c ,F2__c ,F3__c,Account_Name__C FROM Obj2__c where Account_Name__c IN:accountIds];
   
    if(Trigger.isUpdate ){
      
   
    for(Obj1__c new2: trigger.new){
        if(!obj2Lst.isEmpty()){
        
          for(Obj2__c Oobj2 :obj2Lst ){
          
            Oobj2.F1__c = new2.F1__c ;
			Oobj2.F2__c = new2.F2__c ;
			Oobj2.F3__c = new2.F3__c ;
              LstForUpdate.add(Oobj2);  
           }
        
        }
      }
      update LstForUpdate ;       
    } 
}
thanks
let me inform if it helps you
This was selected as the best answer
GarryPGarryP
Hi Piyush,

The solution you have inlined will work but the logic is not bulkified. I have modified your solution a bit.
trigger updateFilds on Obj1__c (after update, before insert) {
    List<Id> accountIds = new List<Id>();
    for (Obj1__c acc : trigger.new) {
        accountIds.add(acc.AccId__c);
    }
    List<Obj2__c>  LstForUpdate = new List<Obj2__c>();
    //get this information in map
    //List<Obj2__c> obj2Lst = [Select Id, F1__c , F2__c , F3__c, Account_Name__C FROM Obj2__c where Account_Name__c IN:accountIds];
    mAp<String,Obj2__c> mapNewObject2 = new mAp<String,Obj2__c> ();
    For(Obj2__c obj2: [Select Id, AccId__c,F1__c , F2__c , F3__c, Account_Name__C 
    FROM Obj2__c 
    where Account_Name__c IN:accountIds]){
        mapNewObject2.put(obj2.AccId__c,obj2);
    }
    //Do not use the For loop inside a for loop. This will fail in bulk scenarios.
    //in case there are 201 objects being loaded and each have same account
    //loop will iterate over 201*200 times (Exception)  
    /*if (Trigger.isUpdate ) {
        for (Obj1__c new2 : trigger.new) {
            if (!obj2Lst.isEmpty()) {
                for (Obj2__c Oobj2 : obj2Lst ) {
                    Oobj2.F1__c = new2.F1__c ;
                    Oobj2.F2__c = new2.F2__c ;
                    Oobj2.F3__c = new2.F3__c ;
                    LstForUpdate.add(Oobj2);
                }
            }
        }
        update LstForUpdate ;
    }*/
    //use Trigger loop and use map created above
    //this will only run 201 times
    if (Trigger.isUpdate ) {
        for (Obj1__c newObject1 : trigger.new) {
            if (mapNewObject2.containsKey(newObject1.AccId__c)) {
                //get obj2 from Map
                Obj2__c newObj2Updated = mapNewObject2.get(newObject1.AccId__c);
                    newObj2Updated.F1__c = new2.F1__c ;
                    newObj2Updated.F2__c = new2.F2__c ;
                    newObj2Updated.F3__c = new2.F3__c ;
                    LstForUpdate.add(newObj2Updated);
            }
        }
        update LstForUpdate ;
    }
}