Approval / Reject functionality on custom buttons of object

11,655

Solution 1

1) You'd need to create a VF page which does the work you need and the redirects back to wherever you want you user to be. Described here: http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/

2) Have a look at 2nd half (the req2 part) of this example: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_process_example.htm|StartTopic=Content%2Fapex_process_example.htm|SkinName=webhelp . Then instead of getting the newWorkItemIds.get(0) value from the result, you'd probably want to query it from the ProcessInstanceWorkitem table

Hope this helps you move on

Solution 2

Option1 : Apex Code (http://blog.jeffdouglas.com/2010/01/04/automating-salesforce-approval-processes-with-apex-triggers/)

Below apex code can be used to submit record for approval

// create the new approval request to submit
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Submitted for approval. Please approve.');
req.setObjectId(Trigger.new[i].Id);
// submit the approval request for processing
Approval.ProcessResult result = Approval.process(req);
// display if the reqeust was successful
System.debug('Submitted for approval successfully: '+result.isSuccess());

Option2 : On click JS (http://jamesrsullivan.com/salesforcecom-start-and-approval-process-from)

Navigate To: Setup > Customize > Opportunities > Buttons and Links Press the "New" button and use these details: Label: Approve Name: Approve Display Type: Detail Page Button Behavior: Execute JavaScript OnClick JavaScript:

if ((Modal.confirm && Modal.confirm('Once you submit this record for approval, you might not be able to edit it or recall it from the approval process depending on your settings. Continue?')) || (!Modal.confirm && window.confirm('Once you submit this record for approval, you might not be able to edit it or recall it from the approval process depending on your settings. Continue?'))) navigateToUrl('/p/process/Submit?id={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}');

Replace Opportunity with your object API name.

Share:
11,655
Prady
Author by

Prady

Salesforce's Force.com platform consultant having worked with enterprise clients like New York times, Cisco and company85 to name a few. Over the last 12 years, I have developed a wide range of websites using HMTL, DHTML,wordpress, .net, SQL server and applications using the force.com platform including sites for startup companies , small businesses and Enterprise clients. Currently working with Cm-Focus LLC - Salesforce developers and Consultants 1: http://www.cm-focus.com@pradykr also representing cloudtrainer.org

Updated on July 10, 2022

Comments

  • Prady
    Prady almost 2 years

    I am trying to implement the functionality of having the approve and reject buttons on custom button in the object.

    I have 2 questions

    1. Can we call apex method from a custom button?
    2. How can we mimic the approval / reject functionality in apex class?

    thanks

  • Prady
    Prady over 12 years
    Thanks for the info.. I am able to submit for approval, the one thing i wanted to do was to allow the managers to approve the submitted record from the buttons. i have a VF page which submits the record for approval. I want the managers to have a look at the record and then approve or reject it
  • Prady
    Prady over 12 years
    i am not sure if i understood when you meant query from ProcessInstanceWorkitem
  • sorenkrabbe
    sorenkrabbe over 12 years
    In the example, the WorkitemId which is being approved (using the req2 variable) is taken from when the examples launches the approval process. This is probably not the case in your code. I assume an approval process has already been kicked off, and you just want to approve the next step. Therefore you want to find the WorkItemId in the data layer. You do this by querying the from ProcessInstanceWorkitem. Something like this should give you the id you're after: [Select id From ProcessInstanceWorkitem where ProcessInstance.TargetObjectId=:idOfObjectToApprove];