#Salesforce Development #Apex Trigger #Service CloudI could use some guidance on a small case trigger update that is not currently working. Below is the code from our trigger, which makes a call to an outside application (sending an external notification). After the call is made, I want to update a flag on the case (reason for the flag is so that the integration call is only performed once). Unfortunately, I currently get an error on the line that is in bold saying 'Execution of AfterUpdate caused by System.FinalException: Record is read-only'). I realize from my limited training that the AfterUpdate trigger doesn't allow you to update fields. Does anyone have a recommendation on how to get around this?
// For calling Rest Call for Integration
if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate))
{
// there's code that I left out, which creates the template for the integration call
CaseTriggerHelper.createAndInvokeMessageMakerRequest(mapCaseIdandTemplate);
// This is the new code I'd like to perform after the integration call to set the flag but only once.
For (Case cse : trigger.new)
{
if(cse.Status=='Closed' && !cse.ClosedCaseNotificationSent__c)
{
cse. ClosedCaseNotificationSent__c = true;
}
}
}
}
You cannot update same Case record in after Trigger.
You can create an instance and update it, if you want to do it in after Trigger.
Update your for loop logic like below:
List<Case> lstCaseToUpdate = new List<Case>();
for (Case cse : trigger.new) {
if(cse.Status=='Closed' && !cse.ClosedCaseNotificationSent__c){
Case objCase = new Case(Id = cse.Id);
objCase.ClosedCaseNotificationSent__c = true;
lstCaseToUpdate.add(objCase);
}
}
update lstCaseToUpdate;