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
Sfdc_beginnerSfdc_beginner 

Not able to invoke an approval process using trigger

Hi All,

 

I am trying to invoke an approval process using trigger which in turn is invoked when a user submits a records in a VF form whose approval status is "Submitted for approval". But the approval process is not getting triggered.

 

The approval process name is Practice_Submission_Approval.and the trigger code is listed below.

 

trigger PracticeSubmitForApproval on Practice__c (before insert) {
  for(Practice__c  appsub: Trigger.new){
       if (appsub.Approval_Status__c == 'Submitted for approval')
         {
            appsub = [ select ID from Practice__c where id in :Trigger.new ];
            Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest();
            request.setComments('Submitting request for approval.');
            request.setObjectId(appsub.id);
            Approval.ProcessResult result = Approval.process(request); 
            System.assert(result.isSuccess());   
         }     
                                       }
]

 

Please correct me if my code is wrong.

 

Thanks in advance

 

 

Avidev9Avidev9

THere are few changes I will suggest

//in after trigger : Ids are generated
trigger PracticeSubmitForApproval on Practice__c (after insert) { for(Practice__c appsub: Trigger.new){
//make sure the value that you are entering and the value you are comparing matches exactly with all the cases
//Make Sure the trigger is active
//Make sure approval process is active
if (appsub.Approval_Status__c == 'Submitted for approval') { Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest(); request.setComments('Submitting request for approval.'); request.setObjectId(appsub.id); Approval.ProcessResult result = Approval.process(request); System.assert(result.isSuccess()); } }

 

 

 

 

 

 

liron169liron169

Hello,

 

Also advise to use try-catch, as it possible that the process failed when trying to send it.

In this case, the exception message will tell you the reason

 

try
{
appsub = [ select ID from Practice__c where id in :Trigger.new ];
            Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest();
            request.setComments('Submitting request for approval.');
            request.setObjectId(appsub.id);
            Approval.ProcessResult result = Approval.process(request);
            System.assert(result.isSuccess());  
}
catch(Exception e){
        System.debug('#### : failed: ' + e);
}

Bhawani SharmaBhawani Sharma
Make sure your record fulfills Approval process entry criteria.
crop1645crop1645

Following up on the last post - you can debug the entry criteria by looking at the Debug Log 'Workflow' debug trace in case you ahve complex entry criteria