3

I have a unique requirement.

  • I have a field called Approver Comments on a custom object.
  • I have also created an approval process on the object.
  • Now when approver approves then I want to capture approver's comment inside the above field.

How can I do that? Any thoughts?

4

You can access the approvers comments via the ProcessSteps relationship. It depends on exactly when you need to do this, but for example if you wanted to do it when the final approval/rejection takes place, you could do the following:

  • Add a field to the record to indicate its comments copying time - a checkbox that defaults to false
  • Add a final approval/rejection access that carries out a field update on the record to set the checkboxvalue to true
  • Create an update trigger that checks the value of the checkbox and if it is true, extracts the various comments and adds them to the custom comments field

Here's a code snippet that extracts the comments from each step for a case identified by 'csId':

  List<Case> cases=[Select c.Id, (Select Id, IsPending, ProcessInstanceId, TargetObjectId, StepStatus, OriginalActorId, ActorId, RemindersSent, Comments, IsDeleted, CreatedDate, CreatedById, SystemModstamp From ProcessSteps) 
                      From Case c where id=:csId];

  if (cases.size()>0)
  {
     Case cs=cases[0];
     String commentsStr='';
     for (ProcessInstanceHistory ps : cs.ProcessSteps)
     {
        commentStr+='\nComment from user ' + ps.ActorId + ' : ' + ps.comments;
     }
  }
  • Yeah, I already tried that. Actually when trigger runs via field update then inside after update trigger we don't get updated comment. for i.e. (ProcessInstanceHistory:{TargetObjectId=a0XU0000002XOB1MAO, Id=04iU0000000DRoQIAW}, ProcessInstanceHistory:{TargetObjectId=a0XU0000002XOB1MAO, Comments=Submitting request for approval., Id=04hU0000000Dv41IAC}) This is what I got. Recent comment I didn't get. – doga Oct 30 '12 at 9:45
  • That's the reason I had it at the end of the process, as a final field update. If you try to capture the comment as part of a step, it won't have been committed yet as its part of the same transaction. In that case I'd suggest you fire an @future method call from your trigger, as that way the transaction containing the comments will have committed. – Bob Buzzard Oct 30 '12 at 9:49

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.