My use case is to make approval comments mandatory regardless of "Approved" or "Rejected".

I went through this thread (How do I make approval comments mandatory?) and I was able to make the comments mandatory only in the case of rejected by following that example.

But as earlier said my use case is to make approval comments mandatory for all approvals.

trigger ApprovalTrigger on Approval__c (before update,after update) {

  System.debug('~~~ Trigger Started ');

 Map<Id, Approval__c> ApprovalsMap = new Map<Id, Approval__c>{};

  for(Approval__c app: trigger.new)

  System.debug('~~~ Inside Second For Loop');

    Approval__c oldApproval = System.Trigger.oldMap.get(app.Id);

    //if (oldApproval .Approval_Status__c != 'Rejected' && app.Approval_Status__c == 'Rejected')

    if(oldApproval.Approval_Status__c != app.Approval_Status__c) //All Status change
        ApprovalsMap .put(app.Id, app);

    System.debug('~~~ ApprovalsMap '+ApprovalsMap);

  for (ProcessInstance pi : [SELECT TargetObjectId, 
                                 SELECT Id, StepStatus, Comments 
                                 FROM Steps
                                 WHERE StepStatus IN ('Rejected','Approved')
                                 ORDER BY CreatedDate DESC
                                 LIMIT 1 
                               FROM ProcessInstance
                               WHERE TargetObjectId In 
                               ORDER BY CreatedDate DESC

  System.debug('~~~ Inside Third For Loop');
  System.debug('~~~ Pi Instance Size '+pi.Steps.size());
  System.debug('~~~ Pi Instance 0'+pi.Steps);

      if (pi.Steps.size() > 0 && (pi.Steps[0].Comments == null || pi.Steps[0].Comments.trim().length() == 0))
          'Please provide approval comments');

This is what the debug log is printed when I approved the record successfully without any approval comments.

09:54:38.0 (24721169)|USER_DEBUG|[3]|DEBUG|~~~ Trigger Started 
09:54:38.0 (43421464)|USER_DEBUG|[21]|DEBUG|~~~ Inside Second For Loop
09:54:38.0 (45165790)|USER_DEBUG|[33]|DEBUG|~~~ ApprovalsMap {***Map Data***}}
09:54:38.0 (52125727)|USER_DEBUG|[51]|DEBUG|~~~ Inside Third For Loop
09:54:38.0 (52298722)|USER_DEBUG|[52]|DEBUG|~~~ Pi Instance Size 0
09:54:38.0 (52397392)|USER_DEBUG|[53]|DEBUG|~~~ Pi Instance 0()

Can someone help me out with whats going on ?

  • 2
    Can you check if your approval__c object has records of status either rejected or approved. – Venkatz Mar 15 '17 at 3:12

You might try to explicitly tell the trigger to check Rejected and Approved like this:

if((oldApproval.Approval_Status__c != 'Rejected' && app.Approval_Status__c == 'Rejected') ||
 (oldApproval.Approval_Status__c != 'Approved' && app.Approval_Status__c == 'Approved'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.