Guest User

Untitled

a guest
Feb 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. trigger RequireApprovalComments on Invoice_Statement__c (before update)
  2. {
  3. // Create a map that stores all the objects that require editing
  4. Map<Id, Invoice_Statement__c> approvalStatements =
  5. new Map<Id, Invoice_Statement__c>{};
  6.  
  7. for(Invoice_Statement__c inv: trigger.new)
  8. {
  9. // Put all objects for update that require a comment check in a map,
  10. // so we only have to use 1 SOQL query to do all checks
  11.  
  12. if (inv.Approval_Comment_Check__c == 'Requested')
  13. {
  14. approvalStatements.put(inv.Id, inv);
  15. // Reset the field value to null,
  16. // so that the check is not repeated,
  17. // next time the object is updated
  18. inv.Approval_Comment_Check__c = null;
  19. }
  20. }
  21.  
  22. if (!approvalStatements.isEmpty())
  23. {
  24. // UPDATE 2/1/2014: Get the most recent process instance for the approval.
  25. // If there are some approvals to be reviewed for approval, then
  26. // get the most recent process instance for each object.
  27. List<Id> processInstanceIds = new List<Id>{};
  28.  
  29. for (Invoice_Statement__c invs : [SELECT (SELECT ID
  30. FROM ProcessInstances
  31. ORDER BY CreatedDate DESC
  32. LIMIT 1)
  33. FROM Invoice_Statement__c
  34. WHERE ID IN :approvalStatements.keySet()])
  35. {
  36. processInstanceIds.add(invs.ProcessInstances[0].Id);
  37. }
  38.  
  39. // Now that we have the most recent process instances, we can check
  40. // the most recent process steps for comments.
  41. for (ProcessInstance pi : [SELECT TargetObjectId,
  42. (SELECT Id, StepStatus, Comments
  43. FROM Steps
  44. ORDER BY CreatedDate DESC
  45. LIMIT 1 )
  46. FROM ProcessInstance
  47. WHERE Id IN :processInstanceIds
  48. ORDER BY CreatedDate DESC])
  49. {
  50. // If no comment exists, then prevent the object from saving.
  51. if ((pi.Steps[0].Comments == null ||
  52. pi.Steps[0].Comments.trim().length() == 0))
  53. {
  54. approvalStatements.get(pi.TargetObjectId).addError(
  55. 'Operation Cancelled: Please provide a reason ' +
  56. 'for your approval / rejection!');
  57. }
  58. }
  59. }
  60. }
Add Comment
Please, Sign In to add comment