+ Start a Discussion
cradiyacradiya 

Not able to update field in after insert trigger. ( system.finalexception record is read-only )

I am trying to fetch data from description fied and update that in other field called "Email Sub". I am getting following errors

 

1. system.finalexception record is read-only

 

I have also tried before insert trigger, but field is not getting updated

 

 

trigger ParseBodySubject on BMCServiceDesk__Incident__c (after Insert, before update) {

    for(BMCServiceDesk__Incident__c incident : Trigger.new){

              String  description = String.valueof([Select id, BMCServiceDesk__incidentDescription__c from BMCServiceDesk__Incident__c where id=:trigger.new limit 1]);                                System.debug('Desc:'+description );

              String Sub = description.substringAfterLast('Subject:');              

              System.debug('Sub:' +Sub);

              incident.Email_Subject__c= Sub;    // Error Line       

       }

}

 

Appreciate your help

Best Answer chosen by Admin (Salesforce Developers) 
cradiyacradiya

Thank you all for your quick reply.

 

I am facing one more problem since couple of days related to approval process. It would be great help if anyone can help me to get it resolved.

 

Here is the link for that problem

http://boards.developerforce.com/t5/Apex-Code-Development/Error-in-mass-approving-records-from-Home-tab-when-Approval/m-p/640031

 

Here is the working code which fulfill my requirement.

 

trigger ParseBodySubject on BMCServiceDesk__Incident__c (before Insert) {
List<BMCServiceDesk__Incident__c> incidentList = new List<BMCServiceDesk__Incident__c>();
   
    for(BMCServiceDesk__Incident__c incident : Trigger.new){
            
       incident.Email_Subject__c = incident.BMCServiceDesk__incidentDescription__c.substringAfter('Subject:').substringBefore ('Body');
       incident.Email_Body__c = incident.BMCServiceDesk__incidentDescription__c.substringAfter('Body:');
       incidentList.add(incident);                                                     
              
    }
      
}

All Answers

Eli Flores, SFDC DevEli Flores, SFDC Dev

Hmmm it looks like you are just referencing the same object in the soql query. 

 

it seems like you could simply the whole thing to be:

 

for(BMCServiceDesk__Incident__c incident : Trigger.new){
 incident.Email_Subject__c= incident.BMCServiceDesk__incidentDescription__c== null ? '' : incident.BMCServiceDesk__incidentDescription__c.substringAfterLast('Subject:'); 
}

 if  the description is being populated somewhere else you should take out the after insert here and just add the extra line to the trigger where description is being populated. Actually, you wouldn't need to do that as that will trigger the before update in this trigger. 

 

Also  you never ever want to run a soql query in a loop. You'll want to familiarize yourself with bulkifying code:

http://wiki.developerforce.com/page/Best_Practice:_Bulkify_Your_Code

 

Nickname_BravoNickname_Bravo

Silly question, but did you check to make sure the field level security is not read-only?

souvik9086souvik9086

You cannot update fields on triggered records when you are in an after trigger - move this into a before trigger.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Sfd developerSfd developer

Hi,

 

trigger ParseBodySubject on BMCServiceDesk__Incident__c (after Insert, before update) {
	List<BMCServiceDesk__Incident__c> incidentList = new List<BMCServiceDesk__Incident__c>();
	for(BMCServiceDesk__Incident__c incident : Trigger.new){
		incident.Email_Subject__c= incident.Description == null ? '' : incident.description.substringAfterLast('Subject:'); 
		incidentList.add(incident);
	}
	update incidentList;
}

 In after trigger, record in the trigger context is read only. you cannot modify. Try the above code or change it before insert trigger 

cradiyacradiya

Thank you all for your quick reply.

 

I am facing one more problem since couple of days related to approval process. It would be great help if anyone can help me to get it resolved.

 

Here is the link for that problem

http://boards.developerforce.com/t5/Apex-Code-Development/Error-in-mass-approving-records-from-Home-tab-when-Approval/m-p/640031

 

Here is the working code which fulfill my requirement.

 

trigger ParseBodySubject on BMCServiceDesk__Incident__c (before Insert) {
List<BMCServiceDesk__Incident__c> incidentList = new List<BMCServiceDesk__Incident__c>();
   
    for(BMCServiceDesk__Incident__c incident : Trigger.new){
            
       incident.Email_Subject__c = incident.BMCServiceDesk__incidentDescription__c.substringAfter('Subject:').substringBefore ('Body');
       incident.Email_Body__c = incident.BMCServiceDesk__incidentDescription__c.substringAfter('Body:');
       incidentList.add(incident);                                                     
              
    }
      
}

This was selected as the best answer