Saturday 17 November 2012

Approval Process via Chatter Feed

Many times, users need to wait with fingers crossed, when a record is submitted for Approval and waiting for the Approval/Comments, especially when the record is locked for editing once it is submitted.

Organization need to have a well defined methodologies and necessarily more than one like chatter alerts, continuous eMail reminders, followup tasks etc., and hence reminding the Approvers then and there, which is the first part of speeding up the process.

The counterpart is making the Approval Job easier for the Approvers, which is demonstrated in this post.

Please read my previous post on Chatter Approvals - A Collaborative Method of Approval Process.... In this method Approvals will be posted in the Chatter, and Approvers can Approve or Reject it with a button click available in the Chatter Post.

This post shows an alternate way of processing Approval process based on the keyword in the Approver Chatter FeedComment.

Approvers can post a keyword in the record chatter like <Approved> or <Rejected> or <Recalled>, and the trigger operating on the FeedItem or FeedComment processes the Approval process based on the comment received.


This Trigger operates on FeedComment,
trigger ContractApproval on FeedComment (after Insert) 
{
    for (FeedComment f : trigger.new)
    {
        string temp = f.ParentId;
if(temp.startsWith(Schema.getGlobalDescribe().get('Contract').getDescribe().getkeyprefix()) && 
f.CommentType == 'TextComment' && 
        (f.CommentBody.containsIgnoreCase('')||
f.CommentBody.containsIgnoreCase('')||
f.CommentBody.containsIgnoreCase('')))
        {
            ProcessInstance pi = [SELECT Id FROM ProcessInstance WHERE TargetObjectId=:f.ParentId AND Status='Pending' LIMIT 1];
            ProcessInstanceWorkitem piw = [SELECT Id,ActorId,ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ActorId=:f.CreatedById and ProcessInstanceId=:pi.Id];            
            Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();            
            if(f.CommentBody.containsIgnoreCase(''))
            {
                req.setComments('Request Approved by Chatter..!!');
                req.setAction('Approve');
            }                                    
            else if(f.CommentBody.containsIgnoreCase(''))
            {
                req.setComments('Request Rejected by Chatter..!!');
                req.setAction('Reject');
            }
            else if(f.CommentBody.containsIgnoreCase(''))
            {
                req.setComments('Request Recalled by Chatter..!!');
                req.setAction('Removed');        
            }
            req.setWorkitemId(piw.Id);                                  
            Approval.ProcessResult result = Approval.process(req);
            FeedComment a = new FeedComment(FeedItemId=f.FeedItemId,CommentType='TextComment');  
            if(result.isSuccess())                                    
            {
                a.CommentBody='Tag received and the process was '+result.getInstanceStatus();                
            }
            else if(!result.isSuccess())
            {
                a.CommentBody='Tag received but some error occured, please contact sys admin';
            }
            Insert a;
        }
    }
}

You can also try this trigger operating on FeedItem / FeedComment, based on your requirement.





Approval Process in salesforce UI




No comments:

Post a Comment