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
Afrose AhamedAfrose Ahamed 

AFTER Update fires when record Insert

Hi All,
When i update record only after update firing as expected. But i insert record both after insert and after update firing. Im not sure its due to recursion. But in my org there is no work flow update rule in this object. So im not sure its recursion issue or not. But i tried to add boolean static variable but still the same execution is happening. Please someone help on this to understand better.
trigger ParamsUpdate on Obj1__c (after insert, after update ) {
    
    if(trigger.IsAfter && (trigger.IsInsert || trigger.IsUpdate))    
    {
    List<String> obj2Ids = new List<String>();
	
    for( Obj1__c objs: Trigger.New){
        obj2Ids.add ( objs.Obj2__c);
    }
        
    
    List<Obj3__c> obj3ids = [SELECT id, Obj4__c FROM Obj3__c WHERE obj2__c IN : obj2Ids];
    
    List<String> obj4idlst= new List<String>();
    
    for( Obj3__c obj3 : obj3ids){
        obj4idlst.add( obj3.Obj4__c);
    }
    
        
        MyLogics.updateParams ( obj4idlst ); 
        }
        
}
 
trigger ParamsUpdate on Obj1__c (after insert, after update ) {
    
    if(trigger.IsAfter && (trigger.IsInsert || trigger.IsUpdate))    
    {
    List<String> obj2Ids = new List<String>();
	
    for( Obj1__c objs: Trigger.New){
        obj2Ids.add ( objs.Obj2__c);
    }
        
    
    List<Obj3__c> obj3ids = [SELECT id, Obj4__c FROM Obj3__c WHERE obj2__c IN : obj2Ids];
    
    List<String> obj4idlst= new List<String>();
    
    for( Obj3__c obj3 : obj3ids){
        obj4idlst.add( obj3.Obj4__c);
    }
    
        
        MyLogics.updateParams ( obj4idlst ); 
        }
        
}

Regards,
Afrose Ahamed M.G.
 
Best Answer chosen by Afrose Ahamed
AnkaiahAnkaiah (Salesforce Developers) 
Hi Afrose,

In your code, below code will execute both insert or update conditions.

If you want run the code seperately then your context variable conditions will be like below.
trigger ParamsUpdate on Obj1__c (after insert, after update ) {
    
    if(trigger.IsAfter && trigger.IsInsert)    
    {
    List<String> obj2Ids = new List<String>();
	
    for( Obj1__c objs: Trigger.New){
        obj2Ids.add ( objs.Obj2__c);
    }
	
	if(trigger.Isafter &&trigger.IsUpdate){
	
	//write your logic to execute
	}
        
    
    List<Obj3__c> obj3ids = [SELECT id, Obj4__c FROM Obj3__c WHERE obj2__c IN : obj2Ids];
    
    List<String> obj4idlst= new List<String>();
    
    for( Obj3__c obj3 : obj3ids){
        obj4idlst.add( obj3.Obj4__c);
    }
    
        
        MyLogics.updateParams ( obj4idlst ); 
        }
        
}

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi,

What is the error you were facing?

Thanks!!
Afrose AhamedAfrose Ahamed

Hi @Ankaiah,
Im not facing any errors, Actually when i insert a new record after update trigger also executing. So its taking insert  and update DML counts in  one transaction. Why After update trigger is occuring when inserting a record here?

 

Regards,

Afrose Ahamed M.G.

AnkaiahAnkaiah (Salesforce Developers) 
Hi Afrose,

In your code, below code will execute both insert or update conditions.

If you want run the code seperately then your context variable conditions will be like below.
trigger ParamsUpdate on Obj1__c (after insert, after update ) {
    
    if(trigger.IsAfter && trigger.IsInsert)    
    {
    List<String> obj2Ids = new List<String>();
	
    for( Obj1__c objs: Trigger.New){
        obj2Ids.add ( objs.Obj2__c);
    }
	
	if(trigger.Isafter &&trigger.IsUpdate){
	
	//write your logic to execute
	}
        
    
    List<Obj3__c> obj3ids = [SELECT id, Obj4__c FROM Obj3__c WHERE obj2__c IN : obj2Ids];
    
    List<String> obj4idlst= new List<String>();
    
    for( Obj3__c obj3 : obj3ids){
        obj4idlst.add( obj3.Obj4__c);
    }
    
        
        MyLogics.updateParams ( obj4idlst ); 
        }
        
}

This was selected as the best answer
Afrose AhamedAfrose Ahamed

Hi @Ankaiah,
Thanks for your response.

Now i have separated my context variable but still After update operation executin when  i insert a record. Actually my logic is same when  insert or update happen. but when i insert record its should need to call only After insert operation and when i update record its need to call only After update operation. 

trigger ParamsUpdate Obj1__c (after insert, after update ) {
    
    if(trigger.IsAfter && trigger.IsInsert)    
    {
    List<String> obj2Ids = new List<String>();
    
    for( Obj1__c objs: Trigger.New){
        obj2Ids.add ( objs.Obj2__c);
    }
        
    
    List<Obj3__c> obj3ids = [SELECT id, Obj4__c FROM Obj3__c WHERE obj2__c IN : obj2Ids];
    
    List<String> obj4idlst= new List<String>();
    
    for( Obj3__c obj3 : obj3ids){
        obj3idlst.add( obj3.Obj4__c);
    }
    
        
        Mylogics.updateparams ( obj4idlst ); 
        }
        
if(trigger.IsAfter && trigger.Update)    
    {
    List<String> obj2Ids = new List<String>();
    
    for( Obj1__c objs: Trigger.New){
        obj2Ids.add ( objs.Obj2__c);
    }
        
    
    List<Obj3__c> obj3ids = [SELECT id, Obj4__c FROM Obj3__c WHERE obj2__c IN : obj2Ids];
    
    List<String> obj4idlst= new List<String>();
    
    for( Obj3__c obj3 : obj3ids){
        obj3idlst.add( obj3.Obj4__c);
    }
    
        
        Mylogics.updateparams ( obj4idlst ); 
        }
}

Afrose AhamedAfrose Ahamed

Hi @Smith Smith 3,

 

Really sorry, I forget to share my apex class. And my concept obj1 is child for  obj2. Obj 3 child for obj4. When obj1 insert or update every obj1 and obj3 child records deleted again it will insert as new record.

 

public with sharing class MyLogics{
   
    public static void updateParams( List<String> obj4idlst ){
          
        Map<String,List<String>> paramsMap = new Map<String,List<String>>();
        
        List<Obj3__c> oldparams = [SELECT id FROM Obj3__c WHERE Obj4__c IN : obj4idlst ];  
        
        RecordsManager.DeleteAsSystem( oldparams );
         // Delete oldparams;  
 
        
        List<Obj4__c> Objs4 = [SELECT id,name,(SELECT id,name,Obj2__c FROM Objs3__r) FROM Obj4__c WHERE id IN : obj4idlst];
        List<String> paramids = new List<String>();
        
       
        
        for( Obj4__c obj4: Objs4){ 
            for( Obj3__c lst : obj4.Objs3__r){
                paramids.add ( lst.Obj2__c );
            } 
            	
        }
        
        
        List<Obj2__c> obj2new = [SELECT id,name,(SELECT name FROM objs1__r) FROM Obj2__c WHERE id in : paramids];
        
        
        List<String> tempParam = new List<String>();
        for( Obj2__c func: obj2new){
           
            for( Obj1__c Param : func.objs1__r ){
                tempParam.add ( Param.Name );
            }      
            paramsMap.put ( func.Id, tempparam);
        }
        
        
        
        List<Obj3__c> paramnewlist = new List<Obj3__c>();
        for( Obj4__c lsts: Objs4){ 
            for( Obj3__c FTS : lsts.objs3__r ){
                if( paramsMap.get ( FTS.Obj2__c) != null){
                    for( String parameterName : paramsMap.get ( FTS.Obj2__c )){
                        paramnewlist.add( new Obj3__c( name=parameterName, Obj4__c=lsts.Id));
                    }
                }    
            } 
        }
          
        RecordsManager.InsertAsSystem( paramnewlist );
          
    }  
}