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
babrannobabranno 

Apex Trigger Error with Approval Process

I have an apex trigger that checks to see if it is a new record and then updates a field and then is supposed to start an approval process.  If the record is being updated the it checks the old and new value of two fields and determines if they are the same.  If not it updates a field and then kicks off an approval process.  The issue I am running into on both an insert or update is that it cannot save the field that I am updating and then execute the approval process because the approval process locks the record.  Do I need a separate before insert,before update trigger that updates the field and then kick off the after insert,after update trigger to start the approval process.  I have included by trigger below.  I appreciate any help.

 

trigger ACHSubmitForApproval on ACH_Information__c (after insert,after update) {

If(Trigger.isInsert){

//Update ACH Information Changed

for(ACH_Information__c ach: Trigger.new){

ach.ACH_Information_Changed__c = TRUE;
// create the new approval request to submit
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Submitted for approval. Please approve.');
req.setObjectId(ach.Id);
// submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
// display if the reqeust was successful
System.debug('Submitted for approval successfully: '+result.isSuccess());}}
else{
If(Trigger.isUpdate){

for(ACH_Information__c ach: Trigger.new){

if((Trigger.oldMap.get(ach.id).Bank_Account__c != ach.Bank_Account__c) || (Trigger.oldMap.get(ach.id).Bank_Routing__c != ach.Bank_Routing__c)){

ach.ACH_Information_Changed__c = TRUE;
// create the new approval request to submit
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Submitted for approval. Please approve.');
req.setObjectId(ach.Id);
// submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
// display if the reqeust was successful
System.debug('Submitted for approval successfully: '+result.isSuccess());}
}
}
}
}

sai.sfsai.sf

yes you need to saperate the process.update the field in before trigger and then kick off the approval process in after trigger.

babrannobabranno

I tried breaking up the trigger.  I moved the field update to a before insert,before update trigger and modified by trigger, but I am still getting and error, "Apex trigger ACHSubmitForApproval caused an unexpected exception, contact your administrator: ACHSubmitForApproval: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.ACHSubmitForApproval: line 9, column 1".  I have my modified trigger below.  Thanks again for the help.

 

trigger ACHSubmitForApproval on ACH_Information__c (after insert,after update) {

If(Trigger.isInsert){

//Update ACH Information Changed

for(ACH_Information__c ach: Trigger.new){

if(ach.ACH_Information_Changed__c = TRUE){
// create the new approval request to submit
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Submitted for approval. Please approve.');
req.setObjectId(ach.Id);
// submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
// display if the reqeust was successful
System.debug('Submitted for approval successfully: '+result.isSuccess());
}}}
else{
If(Trigger.isUpdate){

for(ACH_Information__c ach: Trigger.new){

if(ach.ACH_Information_Changed__c = TRUE){
// create the new approval request to submit
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Submitted for approval. Please approve.');
req.setObjectId(ach.Id);
// submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
// display if the reqeust was successful
System.debug('Submitted for approval successfully: '+result.isSuccess());
}
}
}
}
}


babrannobabranno

Sorry I realized had had some wasted code.  He is the modified trigger that is still giving me the error.

 

trigger ACHSubmitForApproval on ACH_Information__c (after insert,after update) {

 

//Update ACH Information Changed

for(ACH_Information__c ach: Trigger.new){

if(ach.ACH_Information_Changed__c = TRUE){
// create the new approval request to submit
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Submitted for approval. Please approve.');
req.setObjectId(ach.Id);
// submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
// display if the reqeust was successful
System.debug('Submitted for approval successfully: '+result.isSuccess());

}

}

}

babrannobabranno

I fixed the problem.  I had to change my = to ==.  My corrected code is below.

 

trigger ACHSubmitForApproval on ACH_Information__c (after insert,after update) {



//Update ACH Information Changed

     for(ACH_Information__c ach: Trigger.new){
          
            if(ach.ACH_Information_Changed__c == True){
            // create the new approval request to submit
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(ach.Id);
            // submit the approval request for processing
            Approval.ProcessResult result = Approval.process(req);
            // display if the reqeust was successful
            System.debug('Submitted for approval successfully: '+result.isSuccess());
            }
            }
            }

babrannobabranno

I thought I had this but I guess I spoke too soon.  The trigger works fine on the after update.  I am running into a problem with the after insert instances.  Again here is the code and the error I am getting.

 

Error: Apex trigger ACHInformationTrigger caused an unexpected exception, contact your administrator: ACHInformationTrigger: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id a0Ac0000000TqTYEA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ACHSubmitForApproval: execution of AfterUpdate caused by: System.DmlException: Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process.: [] Trigger.ACHSubmitForApproval: line 15, column 1: []: Class.SequenceActions.SequenceACHInformationPerAccount: line 93, column 1

 

trigger ACHSubmitForApproval on ACH_Information__c (after insert,after update) {



//Update ACH Information Changed

     for(ACH_Information__c ach: Trigger.new){
          
            if(ach.ACH_Information_Changed__c == True){
            // create the new approval request to submit
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(ach.Id);
            // submit the approval request for processing
            Approval.ProcessResult result = Approval.process(req);
            // display if the reqeust was successful
            System.debug('Submitted for approval successfully: '+result.isSuccess());
            }
            }
            }