Link to Approval Process Screen

When using approval process you may not specify email template.
In this case SF will use the default template for approval processes with the following structure:
http://login.salesforce.com/help/doc/en/wf_approval_settings.htm

In case you want a customise email template you need to create it.
Sometimes, even when you do create your own email template you want to have the link to the approval screen.

I'll demonstrate how it add it with approval process on Case object.

1.Create email template on Case object.
After desinging the template as needed, at the end add component:
<c:CmpApprovalURL objID="{!relatedTo.id}"/>



<messaging:emailTemplate subject="Case onHold Request" recipientType="User" relatedToType="Case">  
 <messaging:htmlEmailBody >  
  You have been requested to approve or reject this case:<br/>  
  {!relatedTo.Subject}<br/>  
  Contact: {!relatedTo.Contact.Name}<br/>  
  Priority: {!relatedTo.Priority}<br/><br/>  

  <c:CmpApprovalURL objID="{!relatedTo.id}"/>  
 </messaging:htmlEmailBody>  
</messaging:emailTemplate>  

2.Component:CmpApprovalURL.
Using String urlStr at class cls_CmpApprovalUrl


<apex:component controller="cls_CmpApprovalURL" access="global">  
 <apex:attribute name="objID" description="Obj ID" type="Id" assignTo="{!caseID}"/>  
 <a href="{!urlStr}">Log-in to approve or reject</a>  
</apex:component> 

3.Class:cls_CmpApprovalURL.
Need 2 variable :
1.Input for the CaseID
2.Output URL


public class cls_CmpApprovalURL  {  
 
 public String caseID {get; set;}  

 public String urlStr{  
  get  {  
   return cls_createApprovalURL.generateApprovalURL(caseID);  
  }}  
}  


4.Class cls_createApprovalURL
I create the function generateApprovalURL in this seperate class since it can be used for any object for any approval process.
It receive object ID, and return the correct URL for it's approval screen.


public class cls_createApprovalURL  {  
   
 public static String generateApprovalURL(String recordID)  {  
  String url='';  
  
  List<ProcessInstanceWorkitem> workItemLst =   
   [  SELECT id  FROM ProcessInstanceWorkitem  
    WHERE processInstance.TargetObjectId=:recordID];   

  if(workItemLst.size() > 0)  {  

   url='https://'+ System.URL.getSalesforceBaseUrl().getHost() +   
    '/p/process/ProcessInstanceWorkitemWizardStageManager?id=' + workItemLst[0].id;      
  }  
  return url;  
 }  
}


5.The result:

In the email received:

The link from the email:


7 comments:

  1. Why would you not use the Merge Field {!$ApprovalRequest.External_URL} ?

    ReplyDelete
  2. Hi Steve.
    I wasn't aware of this merge field.
    seems i develop here something which SF already provide.

    Thanks.
    Liron

    ReplyDelete
  3. Actually after further checks, the merge fields are available in html template. In case using visualforce email template you do need to generate this url by yourself.

    ReplyDelete
  4. Hi,

    Thanks very much for this, but I don't understand how to build the test class. How do you pass the ID into the test class?

    Thanks,

    ReplyDelete
    Replies
    1. Hi Mathew,
      You should create record of object which have approval process, submit it to approval and then use this record Id for the function.

      e.g. Assume that we have Approval Process on Case:

      static testMethod void myTest() {
      Case newCase = new Case();
      newCase.status = 'Escalated';
      insert newCase;


      Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
      req1.setObjectId(newCase.id);
      Approval.process(req1);

      cls_createApprovalURL.generateApprovalURL(newCase.id);
      }

      Delete
  5. Hi,
    There was a issue when we tried this approach.
    In an approval process, there are 4 approvers and only one approver is able to open the page - ProcessInstanceWorkitemWizardStageManager - via this approach.
    Rest all users are getting 'Insufficient Privileges' error.
    The catch is all these approvers are able to approve the record from Approval history section from the record page.
    Any help would be appreciated.

    Thanks

    ReplyDelete

Retire of Permission on Profiles

If you are working as a Salesforce admin/developer you've probably heard somewhere that Salesforce is planning to make a significant cha...