1

I am trying to make Approval Comments mandatory for the Approver while Approving. I was using the following apex trigger, approval process steps and workflow field update as per the following blog

http://christopheralunlewis.blogspot.com/2012/10/making-all-approval-comments-mandatory.html

If the update could cause workflow to fire again, then I realized it would be possible by adding a workflow rule that caused another update to the same object. This would avoid the order of execution problems, because the workflow and resulting update would occur after the insertion of the step, not before like the previous update.

So it is possible to create mandatory comments for each step, using a double step trigger. Here are the steps to make this possible:

1) Create a new picklist field on your approval process object called "Approval Comment Check". Assign two picklist values "Required" and "Requested", with no default, like this:

2) Create a new workflow field update action, call it "Approval Comment Required" and configure it so that your new Approval Comment Check field is updated to the "Required" value. Check the "Re-evaluate Workflow Rules after Field Change" checkbox.

3) Create another field update action, called "Approval Comment Requested". This update should change the Approval Comment Check field to the "Requested" value. This time, do not check the "Re-evaluate Workflow Rules after Field Change" option.

4) Create a new workflow rule, on your object going through the approval process, called "Approval Comment Flag". Set the evaluation criteria to "When a record is created, or when a record is edited and did not previously meet the rule criteria". The rule criteria should be defined as when the Approval Comment Check field equals "Required". When you have finished, click Save & Next

5) On the new workflow summary page, click Add Workflow Action underneath Immediate Workflow Actions. From the drop menu, click on Select Existing Action. Add the "Approval Comment Requested" rule you created in step 3 and then click Done. Your Summary Screen should look something like this.

!!!!Don't forget to activate your workflow rule before continuing!!!!

6) Create the following before update trigger on your approval process object.

 trigger RequireApprovalComments on Invoice_Statement__c (before update) 
 {
   // Create a map that stores all the objects that require editing 
    Map<Id, Invoice_Statement__c> approvalStatements = 
    new Map<Id, Invoice_Statement__c>{};

   for(Invoice_Statement__c inv: trigger.new)
   {
     // Put all objects for update that require a comment check in a map,
     // so we only have to use 1 SOQL query to do all checks

     if (inv.Approval_Comment_Check__c == 'Requested')
     { 
  approvalStatements.put(inv.Id, inv);
  // Reset the field value to null, 
  // so that the check is not repeated,
  // next time the object is updated
  inv.Approval_Comment_Check__c = null; 
     }
   }  

   if (!approvalStatements.isEmpty())  
   {
     // UPDATE 2/1/2014: Get the most recent process instance for the approval.
     // If there are some approvals to be reviewed for approval, then
     // get the most recent process instance for each object.
     List<Id> processInstanceIds = new List<Id>{};

for (Invoice_Statement__c invs : [SELECT (SELECT ID
                                          FROM ProcessInstances
                                          ORDER BY CreatedDate DESC
                                          LIMIT 1)
                                  FROM Invoice_Statement__c
                                  WHERE ID IN :approvalStatements.keySet()])
{
    processInstanceIds.add(invs.ProcessInstances[0].Id);
     }

// Now that we have the most recent process instances, we can check
// the most recent process steps for comments.  
for (ProcessInstance pi : [SELECT TargetObjectId,
                               (SELECT Id, StepStatus, Comments 
                                FROM Steps
                                ORDER BY CreatedDate DESC
                                LIMIT 1 )
                           FROM ProcessInstance
                           WHERE Id IN :processInstanceIds
                           ORDER BY CreatedDate DESC])
{
  // If no comment exists, then prevent the object from saving.                 
  if ((pi.Steps[0].Comments == null || 
       pi.Steps[0].Comments.trim().length() == 0))
  {
    approvalStatements.get(pi.TargetObjectId).addError(
     'Operation Cancelled: Please provide a reason ' + 
     'for your approval / rejection!');
  }
}                                       
   }
  }

7) Finally in your approval process, at each step you want a comment to be mandatory, add the "Approval Comment Required" field update created in step 2 to the approval actions. You can also add this update to the rejection action to make a comment mandatory for rejection at a particular step, although as a tip, if you want to make a comment mandatory on all rejections, simply add the field update to the final rejection actions (note this only works if the rejection behaviour is 'Final Rejection' rather than 'Go Back 1 Step'.

Now, I followed all the steps and tested in Salesforce classic. The validation message came successfully, error message was displayed and record was not approved without comments. However, when I checked the same in lightning Validation message was displayed, but the record was still approved. What I am trying to understand is that, what is causing this difference.

protected by Community Jun 27 at 12:08

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Browse other questions tagged or ask your own question.