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
Jane NicolasJane Nicolas 

Insert and Updation on same trigger

How can I make IsInsert Trigger work also for Updation of records .Trigger was:

trigger OpportunityTrigger on Opportunity (after insert, after update){

if(Trigger.isInsert){

    List<AccountManagement__c> am = new List <AccountManagement__c>();
   
    
    for(Opportunity opp : trigger.new){

        AccountManagement__c a = new AccountManagement__c
        
        
(Planholder__c = opp.Planholder_Name__c,
        Plan_Number__c = opp.Plan_Number__c);
         
           
                      
                 am.add(a);
                 
    }
    
    if(am != null && am.size() > 0){
insert am;}


}


}
KaranrajKaranraj
Jane - Just remove the If condition logic in your trigger, it will work for both insert and update operation, else add the isUpdate condition check in the if condtion
 
trigger OpportunityTrigger on Opportunity (after insert, after update){

if(Trigger.isInsert || Trigger.isUpdate){ 
    List<AccountManagement__c> am = new List <AccountManagement__c>();
    for(Opportunity opp : trigger.new){

        AccountManagement__c a = new AccountManagement__c(Planholder__c = opp.Planholder_Name__c, Plan_Number__c = opp.Plan_Number__c);
       am.add(a);
                 
    }
    
    if(am != null && am.size() > 0){
    insert am;
}
}
}

This trigger code will work for insert and update operation so you don't need that condition in your code to check, if you going to perform the logic both in insert and update operation

Thanks,
Karanraj
http://www.karanrajs.com