Saturday, 8 September 2012

Making rejection comments mandatory in approval processes

When I was recently asked to set up an approval process on a custom object in a application, I noticed that it was possible to reject an approval request without having to populate the comment field.

This didn't seem to make much sense to me, if an approval is rejected, it is likely that the requesting user will want to know why this is the case. I was surprised to find that there was no setup option to configure the process this way. 

I found that it is possible to make comments mandatory through adding a custom field, some extra steps to your approval process, and an apex trigger for the object. Here is a step by step guide if you want to do the same:

1) Create a new picklist custom field for your object. Set the label as "Approval Status". Add three picklist values ("Pending", "Approved" & "Rejected"). Make this field visible and editable by System Administrators only.




2) Navigate to the field updates menu (Setup --> App Setup --> Create --> Workflows & Approvals --> Field Updates) and create a new field update worfkflow action. Set the name of the new action to "Approval Status Pending" and unique name to "Approval_Status_Pending" and object to the object you added the custom field to. Then select the Approval Status field from the resulting drop down. In the new field value options, select "A specific value" then pick "Pending" status.



3) Repeat step 2 for the "Approved" and "Rejected" picklist values, creating field updates called "Approval Status Approved" and "Approval Status Rejected" respectively.



4) Navigate to your approval process. In "Initial Submission Actions" add the "Approval Status Pending" field update action. Add "Approval Status Approval" update to "Final Approval Actions" and "Approval Status Rejection" to "Final Rejection Actions".



5) Create a new "RequireRejectionComment" before update Apex trigger on your object with the following body (substituting "Invoice_Statement__c" for your object API name).

trigger RequireRejectionComment on Invoice_Statement__c (before update) 
{

  Map<Id, Invoice_Statement__c> rejectedStatements 
             = new Map<Id, Invoice_Statement__c>{};

  for(Invoice_Statement__c inv: trigger.new)
  {
    /* 
      Get the old object record, and check if the approval status 
      field has been updated to rejected. If so, put it in a map 
      so we only have to use 1 SOQL query to do all checks.
    */
    Invoice_Statement__c oldInv = System.Trigger.oldMap.get(inv.Id);

    if (oldInv.Approval_Status__c != 'Rejected' 
     && inv.Approval_Status__c == 'Rejected')
    { 
      rejectedStatements.put(inv.Id, inv);  
    }
  }
   
  if (!rejectedStatements.isEmpty())  
  {
    // UPDATE 2/1/2014: Get the most recent approval process instance for the object.
    // 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 :rejectedStatements.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 ((pi.Steps[0].Comments == null || 
           pi.Steps[0].Comments.trim().length() == 0))
      {
        rejectedStatements.get(pi.TargetObjectId).addError(
          'Operation Cancelled: Please provide a rejection reason!');
      }
    }  
  }
}

The trigger captures any update where the approval status is changed to "rejected" from another value. The field should have this value if it gets rejected, thanks to the way we set up the approval process. 

If the field has been updated in this way, a SOQL query is used to analyse the last created rejection approval history object related to the trigger object. If the comment is empty, the update gets stopped, and an error message is added to the screen.

Here are some screen shots of rejecting a request in chatter, and the resulting error message:



So there you have it, now all your rejections must have a comment, otherwise the operation is reversed! Because the rejection logic is handled in a trigger, this approach works for rejections through the standard pages, in chatter, and in Apex code.

Update 30/12/2013 : here is a sample test method for the trigger, hope it helps, remember to adapt it for your own implementation
/*
    A sample test class for the Require Rejection Comment trigger
*/
@isTest
public class RequireRejectionCommentTest
{
    /*
        For this first test, create an object for approval, then
        simulate rejeting the approval with an added comment for explanation.
        
        The rejection should be processed normally without being interrupted.
    */
    private static testmethod void testRejectionWithComment()
    {
        // Generate sample work item using utility method.
        Id testWorkItemId = generateAndSubmitObject();
        
        // Reject the submitted request, providing a comment.
        Approval.ProcessWorkitemRequest testRej = new Approval.ProcessWorkitemRequest();
        testRej.setComments('Rejecting request with a comment.');
        testRej.setAction  ('Reject');
        testRej.setWorkitemId(testWorkItemId);
    
        Test.startTest();        
            // Process the rejection
            Approval.ProcessResult testRejResult =  Approval.process(testRej);
        Test.stopTest();
        
        // Verify the rejection results
        System.assert(testRejResult.isSuccess(), 'Rejections that include comments should be permitted');
        System.assertEquals('Rejected', testRejResult.getInstanceStatus(), 
          'Rejections that include comments should be successful and instance status should be Rejected');
    }
    
    /*
        For this test, create an object for approval, then reject the request, mark the approval status as pending, then
        without a comment explaining why. The rejection should be halted, and
        and an apex page message should be provided to the user.
    */
    private static testmethod void testRejectionWithoutComment()
    {
        // Generate sample work item using utility method.
        Id testWorkItemId = generateAndSubmitObject();
        
        // Reject the submitted request, without providing a comment.
        Approval.ProcessWorkitemRequest testRej = new Approval.ProcessWorkitemRequest();
        testRej.setComments('');
        testRej.setAction  ('Reject');      
        testRej.setWorkitemId(testWorkItemId);
    
        Test.startTest();        
            // Attempt to process the rejection
            try
            {
                Approval.ProcessResult testRejResult =  Approval.process(testRej);
                system.assert(false, 'A rejection with no comment should cause an exception');
            }
            catch(DMLException e)
            {
                system.assertEquals('Operation Cancelled: Please provide a rejection reason!', 
                                    e.getDmlMessage(0), 
                  'error message should be Operation Cancelled: Please provide a rejection reason!'); 
            }
        Test.stopTest();
    }
    
    /*
        When an approval is approved instead of rejected, a comment is not required, 
        mark the approval status as pending, then ensure that this functionality still holds together.
    */
    private static testmethod void testApprovalWithoutComment()
    {
        // Generate sample work item using utility method.
        Id testWorkItemId = generateAndSubmitObject();
        
        // approve the submitted request, without providing a comment.
        Approval.ProcessWorkitemRequest testApp = new Approval.ProcessWorkitemRequest();
        testApp.setComments ('');
        testApp.setAction   ('Approve');
        testApp.setWorkitemId(testWorkItemId);
    
        Test.startTest();        
            // Process the approval
            Approval.ProcessResult testAppResult =  Approval.process(testApp);
        Test.stopTest();
        
        // Verify the approval results
        System.assert(testAppResult.isSuccess(), 
                     'Approvals that do not include comments should still be permitted');
        System.assertEquals('Approved', testAppResult.getInstanceStatus(), 
           'All approvals should be successful and result in an instance status of Approved');
    }
    
    /*
        Put many objects through the approval process, some rejected, some approved,
        some with comments, some without. Only rejctions without comments should be
        prevented from being saved.
    */
    private static testmethod void testBatchRejctions()
    {
        List<Invoice_Statement__c> testBatchIS = new List<Invoice_Statement__c>{};
        for (Integer i = 0; i < 200; i++)
        {
            testBatchIS.add(new Invoice_Statement__c());
        }   
           
        insert testBatchIS;
        
        List<Approval.ProcessSubmitRequest> testReqs = 
                         new List<Approval.ProcessSubmitRequest>{}; 
        for(Invoice_Statement__c testinv : testBatchIS)
        {
            Approval.ProcessSubmitRequest testReq = new Approval.ProcessSubmitRequest();
            testReq.setObjectId(testinv.Id);
            testReqs.add(testReq);
        }
        
        List<Approval.ProcessResult> reqResults = Approval.process(testReqs);
        
        for (Approval.ProcessResult reqResult : reqResults)
        {
            System.assert(reqResult.isSuccess(), 
                          'Unable to submit new batch invoice statement record for approval');
        }
        
        List<Approval.ProcessWorkitemRequest> testAppRejs 
                                                  = new List<Approval.ProcessWorkitemRequest>{};
        
        for (Integer i = 0; i < 50 ; i++)
        {
            Approval.ProcessWorkitemRequest testRejWithComment = new Approval.ProcessWorkitemRequest();
            testRejWithComment.setComments  ('Rejecting request with a comment.');
            testRejWithComment.setAction    ('Reject');
            testRejWithComment.setWorkitemId(reqResults[i*4].getNewWorkitemIds()[0]);
            
            testAppRejs.add(testRejWithComment);
            
            Approval.ProcessWorkitemRequest testRejWithoutComment = new Approval.ProcessWorkitemRequest();
            testRejWithoutComment.setAction    ('Reject');
            testRejWithoutComment.setWorkitemId(reqResults[(i*4)+1].getNewWorkitemIds()[0]);
            
            testAppRejs.add(testRejWithoutComment);
            
            Approval.ProcessWorkitemRequest testAppWithComment = new Approval.ProcessWorkitemRequest();
            testAppWithComment.setComments  ('Approving request with a comment.');
            testAppWithComment.setAction    ('Approve');
            testAppWithComment.setWorkitemId(reqResults[(i*4)+2].getNewWorkitemIds()[0]);
            
            testAppRejs.add(testAppWithComment);
            
            Approval.ProcessWorkitemRequest testAppWithoutComment = new Approval.ProcessWorkitemRequest();
            testAppWithoutComment.setAction    ('Approve');
            testAppWithoutComment.setWorkitemId(reqResults[(i*4)+3].getNewWorkitemIds()[0]);
            
            testAppRejs.add(testAppWithoutComment);            
        }
            
        Test.startTest();        
            // Process the approvals and rejections
            try
            {
                List<Approval.ProcessResult> testAppRejResults =  Approval.process(testAppRejs);
                system.assert(false, 'Any rejections without comments should cause an exception');
            }
            catch(DMLException e)
            {
                system.assertEquals(50, e.getNumDml());
                
                for(Integer i = 0; i < 50 ; i++)
                {
                    system.assertEquals((i*4) + 1, e.getDmlIndex(i));
                    system.assertEquals('Operation Cancelled: Please provide a rejection reason!', 
                                        e.getDmlMessage(i));
                }
            }    
        Test.stopTest();
    }
    
    /*
        Utility method for creating single object, and submitting for approval.
        
        The method should return the Id of the work item generated as a result of the submission.
    */
    private static Id generateAndSubmitObject()
    {
        // Create a sample invoice statement object and then submit it for approval.
        Invoice_Statement__c testIS = new Invoice_Statement__c();
        insert testIS;
        
        Approval.ProcessSubmitRequest testReq = new Approval.ProcessSubmitRequest();
        testReq.setObjectId(testIS.Id);
        Approval.ProcessResult reqResult = Approval.process(testReq);
        
        System.assert(reqResult.isSuccess(),'Unable to submit new invoice statement record for approval');
        
        return reqResult.getNewWorkitemIds()[0];
    }
}

110 comments:

  1. Interesting blog post. This is something I thought was impossible until I saw your creative solution. I can't believe there isn't just a check box to make approval comments required! Have you submitted a feature suggestion for this?

    Incidentally, as this is such a well constructed post, have you considered watermarking your screenshots to impede copy-thieves? (Like I have done on my blog, for example http://srlawr.blogspot.co.uk/2012/01/integrating-paypal-api-with.html)

    ReplyDelete
  2. I am creating a approval on campaign... but i am geeting a error..

    Validation Errors While Saving Record(s)
    There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger RequireRejectionComment caused an unexpected exception, contact your administrator: RequireRejectionComment: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 0: Trigger.RequireRejectionComment: line 31, column 1".

    ReplyDelete
    Replies
    1. Hi Madan,

      Apologies for the late reply, are you still having this problem? It sounds like your SOQL query to get the campaigns is not bringing back any results, which in turn then suggests that the id map is not being correctly populated.

      Check the population code is correctly set up, if you are still stuck then post the code in a reply comment and I will have a closer look :) .

      Regards,
      CAL

      Delete
    2. Hi CAL,

      Even I am getting similar error. The debugged and found that in PI Steps, it only found 1 step with status 'Started'. There was no entry for 'Rejected' status. Any Idea?

      Delete
    3. Hi there Mohit,

      After doing some debugging of my own, I managed to re-create the error by setting the approval status to "Rejected" as part of the initial submission actions, not as part of any rejection steps. Make sure that your workflow rules are configured correctly within the approval process, and that the correct field updates are matched to the appropriate steps.

      When do you get this error message, when approval is first submitted or when the rejection button is clicked?

      Regards,
      CAL

      Delete
    4. This comment has been removed by the author.

      Delete
    5. I had the same issue as Mohit in the Unit Tests. I believe it is because the precision on the CreateDate used to order the records is not good enough to reliably retrieve the latest Step:

      I had the same issue as Mohit in the Unit Tests. I believe it is because the precision on the CreateDate used to order the records is not good enough to reliably retrieve the latest Step. The lines marked "Inner Steps" are an additional query I added to check the theory by re-pulling all Steps:

      Starting testRejectionWithComment Test.

      Found Rejected Opportunity (0061F000002lLTrQAM). ContractStatus__c = Rejected

      Found ProcessInstanceId: 04g1F0000009UmTQAU

      Step 0: ProcessInstanceStep:{ProcessInstanceId=04g1F0000009UmTQAU, Id=04h1F0000009OJjQAM, CreatedDate=2018-02-03 16:11:07, StepStatus=Started}

      ProcessInstance found to evaluate: ProcessInstance:{Id=04g1F0000009UmTQAU, TargetObjectId=0061F000002lLTrQAM}. StepStatus: Started. Comments: null

      pi.Steps[0].Comments is supposedly empty.

      Inner Steps 0: ProcessInstanceStep:{ProcessInstanceId=04g1F0000009UmTQAU, Id=04h1F0000009OJkQAM, CreatedDate=2018-02-03 16:11:07, StepStatus=Rejected, Comments=Rejecting request with a comment.}

      Inner Steps 1: ProcessInstanceStep:{ProcessInstanceId=04g1F0000009UmTQAU, Id=04h1F0000009OJjQAM, CreatedDate=2018-02-03 16:11:07, StepStatus=Started}

      Delete
    6. Using the Salesforce Id of the Steps seems to be more reliable, and always increments vs. relying on the Timestamp.

      Delete
    7. add this condition before you add the error:
      if( (pi.Steps != null && pi.Steps.size() > 0 &&
      (pi.Steps[0].Comments == null || pi.Steps[0].Comments.trim().length() == 0)

      Delete
  3. im getting an error on the MAP on line 5? Error: Compile Error: unexpected token: Map at line 5 column 2

    ReplyDelete
    Replies
    1. Hi err.. Unknown :S,

      That seems unusual, are you copying all of the code above and pasting into a trigger for the object? When you get a message like this, it seems to suggest that the line before has not been terminated properly, or that you are defining the variable outside of the scope of a trigger or class.

      Hope that helps, if not, please post a sample of your version of the code in a reply

      Regards,
      CAL

      Delete
  4. can u plz provide the test class for this trigger as well ?

    ReplyDelete
    Replies
    1. Hi Spartan,

      I have just included this as part of the post, hope that helps, remember to adapt it for your own org.

      Regards.
      CAL

      Delete
  5. Hi Christopher,

    I'm currently working on a project that works on Opportunity and Quote. I have placed the trigger on Quote so whenever the quote is rejected, trigger is fired and shows message that comment is required. So far it's working. But when i tried the test class. I changed the all Invoice_Status__c to Quote and got this error System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, OpportunityId]: [Name, OpportunityId]

    Need your help. Thanks in advance.

    ReplyDelete
  6. I have Submitted an idea on this. Please Vote to have this implemented on salesforce standard functionality.
    https://success.salesforce.com/ideaView?id=08730000000DhQlAAK

    ReplyDelete
  7. Hi Christopher,

    I tried using your code its works great, but the error message is not coming on same page ..

    I have used campagin object and approves the record and its going to another page for displaying the error messages .. rejectedStatements.get(pi.TargetObjectId).addError(
    'Operation Cancelled: Please provide a rejection reason!');

    My requiremnet is to display the error on same page..
    Please suggest,thanks..



    ReplyDelete
    Replies
    1. Hi Prateek,

      Did the issue resolve for you ?? I am also looking for the same requirement that you are looking for, Showing the Error message on the same page.

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Helpful information on Salesforce, thanks a lot for sharing.

    Salesforce Consulting Services

    ReplyDelete
  10. Hello CAL,

    A very helpful post. I have a similar requirement to work with, i used your code and it works fine. I have two issue though : First, when i try to reject the code from standard page, the error comes on a new page. The popup only works using chatter. Do we have a way around this?
    Second and most important, even if i provide comments, the rejection error is still coming. So irrespective of whether comments are filled or not, i get an error upon hitting the reject button. I tried looking at the debug logs, comments field in the query fetches a NULL value.
    I am really desperate to get this thing going and would really appreciate if you could help me around this.

    Many Thanks,
    M

    ReplyDelete
  11. Hi Chris,
    Thank you very much for your efforts, i used your code to make rejection comments mandatory, it works superbly, but i m getting error in writing test class. could you please help me out here.
    i am not submitting the approval request as using process builder to do that, else is same

    ReplyDelete
  12. HI Chris,
    Is it any possible the error msg is through the same page

    ReplyDelete
  13. Hi Chris,

    I am getting error-"There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Please provide rejection comments!" even after my comments are not null.

    Please suggest

    Thanks
    Siddhant

    ReplyDelete
  14. I tried above solution, but not working, I am getting process instance of submitted not rejected one

    ReplyDelete
  15. This unique appearances utterly suitable. Each one of modest data are prepared with the help of great number of experience practical knowledge. I'm keen it again very much SEO

    ReplyDelete
  16. Can we increase a comment field character size....?

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Wonderful article, thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here. buy backlinks

    ReplyDelete
  19. (Michael Kors Outlet Store) has soul searching rubbish. (Coach Outlet Online) As well as perhaps this provides the inevitable settlement, Which may be glorious. Perhaps WWE branches for a"Big celeb" Story..

    Costner, A fantastic fit softball player, Was a perfect rendered hander, (Cheap Yeezy Shoes Sale) Still he can also whacked the right (Ray Ban New Wayfarer Polarized) way given. Film production company manager dreamed of it hit (Michael Kors Outlet Online) the homer golfing party (Michael Kors Outlet) as the right hander (New Yeezys 2020) growing turf since the remains field of operation fence. In order to aims, (Ray Ban Outlet Store) Costner couldn elevation which, (Coach Outlet Store Online) So film (Cheap Jordan Shoes Websites) production company folks introduced the teacher on the UNC Asheville softball sales teams, Ken Bagwell.

    ReplyDelete
  20. And Jacques, E. And Jomard, Ray Ban Glasses H. And Kastelic, V. Khalil, M. And Ayub, M. And Ray Ban Outlet Naeem, F. We are going to be very hard on ourselves Yeezy Discount hopefully we can pick ourselves on the field. One or two chances with the run Yeezy Boost 350 outs and a bit of sloppy work from time to time. That's the main area Coach Outlet we'd like Coach Outlet Store to improve.

    R., Standen, V. G., Arriaza, B. T. Werner Kruger, 19. Jannes Kirsten, 20. Roelof Smit, 21. But they don't contest Prince's chosen, Warholian ground as a magus of contemporary American culture. (Koons tried, but New Jordan Shoes 2020 his attempt was too weird for comprehension, let alone assent.) Prince's works make him an artist as anthropologist, illuminating folkways by recycling advertising photographs, cartoon and one liner jokes, soft core pornography, motorcycle cult ephemera, pulp novel covers, "Dukes of Hazzard" era car parts, celebrity memorabilia, Coach Handbags Clearance and other demotic flotsam. (more).

    ReplyDelete
  21. https://taniagold.com/subcategory/6/1/chain-necklace

    ReplyDelete
  22. https://khoshboresh.com/search/manto-clothing

    ReplyDelete
  23. شرکت بازرگانی کارن کویر مفتخر به ارائه متنوع ترین و بروز ترین محصولات در زمینه خرید قفل دیجیتال و قفل هوشمند درب به صورت مستقیم و بی واسطه با بهترین قیمت و بالاترین سطح مشتری مداری و ارائه خدمات شبانه روزی می‌باشد.

    شرکت بازرگانی کارن کویر

    ReplyDelete
  24. would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well Assignment help| دی اس ام ایران

    ReplyDelete