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
sancasanca 

Standard Approval process button in visualforce

Hi All ,

I have a requirement to use standard aproval process functionality in visualforce page . For that I used Standard controller and showed the detail page using apex:detail tag which shows the approval process button .

But when I click on the button , it is redirecting to detail record page , Instead I need it to remain in same visualforce page where the button is clicked and showing alert box saying record is sent for approval .
Please note that I dont want to use custom approval process with apex 
Can anyone help me here?
Regards,
 
Ajinkya1225Ajinkya1225
Hi Sanju,

You can invoke the approval process from the Approval Class (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_approval.htm).

Here is the snippet for your reference:

VF Page:
<apex:commandButton value="Submit for Approval" action="{!sumbitForApproval}">

Controller:
public PageReference sumbitForApproval(){
	try{
		Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
		req.setComments('Submitting request for approval'); // You can make comments dynamic
		req.setObjectId(RecordId);
		Approval.ProcessResult result = Approval.process(req);
	}
	catch (Exception ex){
		System.debug('Exception caught');
	}
	//return submitPageRef;
	return null;
}

Please upvote and mark this answer as solved, if it helped you.
Cheers!
Ajinkya Deshmukh
 
Umar Mohammad 12Umar Mohammad 12
whats that pagereference is ? How should I use standard approval process for satndard controller. a basic simple example would be appriciated.