function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gowkanapalli JanardhanreddyGowkanapalli Janardhanreddy 

Approval Process - override standard Approve/Reject page?

sfdc Beginnersfdc Beginner
Follow the below approach to over-ride standard approval process page -

1. Create a custom visualforce page as a replica of standard approval/rejection page.
2. Create a controller to have methods for approve/reject/cancel. Approval Process api is available in apex reference book.
3. Create a home page component in which a java-script will be written to override  the "Approve/Reject" button on Lead.
4. Add this home page component in home page layout.

Visualforce Page

<apex:page controller="ProcessInstanceController" tabStyle="Lead">
   <apex:form >
   <apex:sectionHeader title="Lead" subtitle="{!objLead.Name}"/>
   <apex:pageBlock title="Approve/Reject Approval Request">
       <apex:pageBlockButtons location="bottom">
           <apex:commandButton value="Approve" action="{!approve}"/>
           <apex:commandButton value="Reject" action="{!reject}"/>
           <apex:commandButton value="Cancel" action="{!cancel}"/>
       <apex:pageBlockSection columns="1">
           <apex:pageBlockSectionItem >
               Name <apex:outputField value="{!objLead.Name}"/>
           <apex:pageBlockSectionItem >
               Lead Owner <apex:outputField value="{!objLead.Owner.Name}"/>
           <apex:pageBlockSectionItem >
               Rejection Reason <font color="red">(Mandatory while Rejection)</font><apex:inputField value="{!objLead.Rejection_Reason__c}"/>
           <apex:pageBlockSectionItem >
              Comments <font color="red">(Mandatory while Rejection)</font> <apex:inputTextArea value="{!objLead.Comments__c}" rows="5" cols="100"/>


public class ProcessInstanceController {
    public String processId;
    public String leadId;
    public ProcessInstance objProcessInstance;
    public Lead objLead {get; set;}
    public PageReference redirectPage;
    public ProcessInstanceController(){
        processId = ApexPages.currentPage().getParameters().get('id');
        leadId = ApexPages.currentPage().getParameters().get('leadId');
        objLead = [select Name,Owner.Name,Rejection_Reason__c,Comments__c from Lead where id =:leadId];
        redirectPage = new PageReference('/'+leadId);
    public PageReference Approve(){
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        Approval.ProcessResult result =  Approval.process(req);
        update objLead;
        return redirectPage ;
    public PageReference Reject(){
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        Approval.ProcessResult result =  Approval.process(req);
        update objLead;
        return redirectPage ;      
     public PageReference Cancel(){
     return redirectPage ;

Home Page Component

<div id="demoContainer"><script language="JavaScript">try{var pa=document.getElementById("demoContainer").parentNode.parentNode; = "none";}catch(err){alert('There was an error on this webpage='+err.description);}</script><script language="JavaScript">function CustomApproval(){try{var str=location.href;var leadId=str.substr(str.indexOf(".com/")-0 + 5,15);var divid=leadId+'_RelatedProcessHistoryList_body';var Approvalprocessdiv=document.getElementById(divid);if(Approvalprocessdiv!=null){var originalHTML=Approvalprocessdiv.innerHTML;var newHtml=originalHTML.replace('/p/process/ProcessInstanceWorkitemWizardStageManager?','/apex/ProcessInstanceWorkitemWizard?LeadId='+leadId+'&');Approvalprocessdiv.innerHTML=newHtml;}}catch(err){alert(err);}}var oldonload = window.onload; if (typeof window.onload != 'function'){window.onload = oldonload;} else { window.onload = function() {if (oldonload) {oldonload();}CustomApproval();}} </script></div>

Dominic SebastianDominic Sebastian
I am sorry, but I am really new to  salesforce. If there is a way that you could you explain what each code does. I never understood what
leadId = ApexPages.currentPage().getParameters().get('leadId'); means 
and what does req.setWorkitemId(processId); mean. It would be helpful.
Stephen SpragueStephen Sprague
This will no longer work after this summer release, summer '14, due to the changes they're making to home page components.  Bummer!  It may work if you had already set it up but they're working towards disabling this functionality as they're going to be putting the components in an iframe which won't be able to override the buttons (interact with the page).

The VF page will still work, but the way you get to it won't.  You'll have to override the detail pages approval history related list w/ a vf component or override the entire page w/ vf including a custom approval history related list (to hide the approve/reject link) which I'm working on now.
Girish AhujaGirish Ahuja
Guys any solution on this ?
There's a way to adapt the Javascript in the Home Page Component into a Custom Link that'll work through at least Winter '16.  Take a look at this StackExchange post here:

Unfortunately, I don't really consider that a quality workaround as we're still working on borrowed time until Winter '16.  SF really needs to allow us to override standard buttons and links (such as approvals) with Visualforce pages.

I'd love if there was a long-term solution.... anyone have one?
The below implementation to hide Approve/Reject link can be a long term solution. 

1. Create visualforce page with relatedList ProcessSteps.
2. jQuery is used to hide the Approve/Reject link on the approval history.
3. Edit Layout -> Remove standard Approval History
4. Edit Layout -> Select visualforce page created in step 1 and drag and drop on to HTML space of Page Layout.

Go through the below link to see the code.