+ Start a Discussion
ChickenOrBeefChickenOrBeef 

Trigger that runs after approval process (read-only causing issues)

Hello everyone,

We have an approval process that does the following:

-We have a custom object called 'Library Requests' that is attached to Accounts with a Master-Detail relationship
-Our sales reps create a Library Request for the account they want to request (as long as the account is owned by the 'Library' user)
-They submit the Library Request for approval
-Once the Library Request is approved by their manager, a checkbox field call 'Approved' is populated (for reporting purposes)
-I then also receive an email and have to transfer the account to the requested user manually

So I tried to write a trigger that simply transfers the account to the person who created the request once the 'Approved' box is populated.

The issue? When I go to test the trigger by submitting a request for approval, I get an error message that says the following:

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

So obviously the issue is that the Library Request is read-only, but I don't see any way to no longer make it read-only. It seems that happens by default and can't be changed.

Any idea what to do? Here is my code:

trigger automateLibraryApproval on Library_Request__c (after update) {
   
    Set<String> accountsInTrigger = New Set<String>();
   
    for(Library_Request__c lib : trigger.new){
        if(Library_Request__c.Requested_Account__c != NULL){
            accountsInTrigger.add(lib.Requested_Account__c);
        }
    }
   
    List<Account> listAccounts = [
               SELECT
                Id,
                OwnerId
               FROM
                Account
               WHERE
                Id In :accountsInTrigger
              ];
   
    Map<String,Account> mapAccounts = New Map<String,Account>();
   
    for(Account acc : listAccounts){
        mapAccounts.put(acc.Id,acc);
    }
   
    Map<String,String> mapAccountOwners = New Map<String,String>();
                                              
    for(Account acc2 : listAccounts){
        mapAccountOwners.put(acc2.Id,acc2.OwnerId);
    }
                                                
    List<Account> accountsToUpdate = New List<Account>();
   
    for(Library_Request__c req : trigger.new){
        if(req.Approved__C = TRUE && mapAccountOwners.get(req.Requested_Account__c) == '005A0000003R9p0'){
            Account libraryRequestAccount = mapAccounts.get(req.Requested_Account__c);
            libraryRequestAccount.OwnerId = req.CreatedById;
            accountsToUpdate.add(libraryRequestAccount);
        }    
    }
    update accountsToUpdate;
}



Let me know if you need any other info from me.

Thanks!
-Greg
Best Answer chosen by ChickenOrBeef
Gupta.VishalGupta.Vishal
Hi ,

on your line number :36   Library_Request__c req : trigger.new  
trigger.new is read only on this contect and you are assigning TRUE in the next line 
use this 

req.Approved__C == TRUE

and things will be alright :)