+ Start a Discussion
MarniMarni 

Fire Apex Trigger Only Once

Hi, I am new to Apex and have been working on developing a few triggers that will create a new Task based on specific criteria.  The trigger will only fire after some sort of update on the record.  I have been able to successfully write the trigger but the issue I am having is how to write it so it only fires once.  Below is the trigger I wrote.  Any help in how to change it so that it only fires once would be greatly appreciated.  Thank you!

 

trigger ARGOCoverageLetterTask on Claim__c (after update) {
Claim__c SO=Trigger.new[0];
if (SO.Name == 'ARGO*'){
Task argoCoverageTask = new Task();
argoCoverageTask.ActivityDate = SO.LVL_Received_Date__c + 30;   
argoCoverageTask.WhatId = SO.id;
argoCoverageTask.OwnerId = SO.Assigned_Examiner2__c;   
argoCoverageTask.Priority = 'Normal';   
argoCoverageTask.Status = 'Not Started';   
argoCoverageTask.Subject = 'Coverage Letter';   
Insert argoCoverageTask;
}// else nothing
}



Best Answer chosen by Admin (Salesforce Developers) 
raseshtcsraseshtcs

The error is because the trigger is after update. You cannot update the record in the after update trigger. Change the trigger to before update.

All Answers

spraetzspraetz

So it only fires once EVER per Claim__c?  Or once per transaction?

spraetzspraetz

I guess either way you'd want to create a custom field checkbox on the Claim__c called "Task_Created__c" or something like that.  Default the value to false.

 

In the trigger, check if that checkbox is not null and not true.  

 

If it isn't null and isn't true, create your task. 

 

After creating your task, update the field to true so that in the future when the trigger is executed, it will fail the check and it won't create the task.

MarniMarni

This apex trigger is on a custom object.  When a record on the object is created is has to go through an approval process.  Once approved, this apex trigger will fire.  Once it is fired and creates that task, it should never fire again for that particular record.  Is that what you are asking?

MarniMarni

That could work but would the field saying whether or not the task has been created be automatically updated by the trigger or would it need to be update manually?  I ask because if it requires a manual update that wouldn't work due to the volume.

 

Can I put a field update as part of the trigger?  I do something similar in the approval process that once approved it updates a field which the trigger references in order to determine if the record has been approved.

spraetzspraetz
trigger ARGOCoverageLetterTask on Claim__c (after update) {
    List<Task> tasks = new List<Task>();
    for(Claim__c claim : Trigger.new){
        if(claim.Name == 'ARGO*' && claim.Task_created__c != null && claim.Task_created__c == false){
            //Do your Task creation
            Task argoCoverageTask = new Task();
            argoCoverageTask.ActivityDate = SO.LVL_Received_Date__c + 30;   
            argoCoverageTask.WhatId = SO.id;
            argoCoverageTask.OwnerId = SO.Assigned_Examiner2__c;   
            argoCoverageTask.Priority = 'Normal';   
            argoCoverageTask.Status = 'Not Started';   
            argoCoverageTask.Subject = 'Coverage Letter';   
            tasks.add(argoCoverageTask);
            
            //Update the field on Claim so this isn't done again
            claim.Task_Created__c = true;
        }
    }  
    
    insert tasks;
} 

 

All you need to do to update the field on a record being passed into the trigger is change the value during the trigger.

 

Note the code above as it will accomplish what you want and it is also bulkified to run faster and not run into governor limits should hundreds of Claims be inserted at once.   

MarniMarni

Thanks!  This is definitely helpful.  Just one more question, when I try running through our process I get the following error when I go to Approve the record:

 

Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger AWACCoverageLetterTask caused an unexpected exception, contact your administrator: AWACCoverageLetterTask: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.AWACCoverageLetterTask: line 16, column 13". 

 

Is it an issue because a record is locked to editing while in the approval process?  I checked line 16 column 13 and it is the update field code (underlined below) but that field is not read-only.  Any ideas?

 

Thanks again for your help on this!

 

trigger AWACCoverageLetterTask on Claim__c (after update) {
 List<Task> tasks = new List<Task>();
    for(Claim__c claim : Trigger.new){
        if(claim.RecordTypeID == '012A0000000nXBH' && claim.Task_created__c != null && claim.Task_created__c == false){
            //Do your Task creation
            Task awacCoverageTask = new Task();
            awacCoverageTask.ActivityDate = claim.LVL_Received_Date__c + 30;   
            awacCoverageTask.WhatId = claim.id;
            awacCoverageTask.OwnerId = claim.Assigned_Examiner2__c;   
            awacCoverageTask.Priority = 'Normal';   
            awacCoverageTask.Status = 'Not Started';   
            awacCoverageTask.Subject = 'AWAC APEX Coverage Letter';   
            tasks.add(awacCoverageTask);
            
            //Update the field on Claim so this isn't done again
            claim.Task_Created__c = true;
        }
    }  
    
    insert tasks;
}



MarniMarni

I have made on additional update to the trigger as it should only fire if it has been approved so the trigger looks like this now but I still get the same error when I try to approve the process:

 

trigger AWACCoverageLetterTask on Claim__c (after update) {
 List<Task> tasks = new List<Task>();
    for(Claim__c claim : Trigger.new){
        if(claim.RecordTypeID == '012A0000000nXBH' && claim.Task_created__c != null && claim.Task_created__c == false && claim.Approval_Received__c == true){
            //Do your Task creation
            Task awacCoverageTask = new Task();
            awacCoverageTask.ActivityDate = claim.LVL_Received_Date__c + 30;   
            awacCoverageTask.WhatId = claim.id;
            awacCoverageTask.OwnerId = claim.Assigned_Examiner2__c;   
            awacCoverageTask.Priority = 'Normal';   
            awacCoverageTask.Status = 'Not Started';   
            awacCoverageTask.Subject = 'AWAC APEX Coverage Letter';   
            tasks.add(awacCoverageTask);
            
            //Update the field on Claim so this isn't done again
            claim.Task_Created__c = true;
        }
    }  
    
    insert tasks;
}



raseshtcsraseshtcs

The error is because the trigger is after update. You cannot update the record in the after update trigger. Change the trigger to before update.

This was selected as the best answer
MarniMarni

That worked!  Thank you so much for your help!