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
justin brown 7justin brown 7 

Approval Process Controllers!

Hello All,

I have a VF page that contains fields relevant to a submission form. This VF page is currently being controlled by a standard controller and the object has an associated approval process. I was wondering what a controller extension would look like in order to have the user input their information submit the form (via) VF page and submit it for approval, Have the appropriate approver view the record on the VF page and approve it by simply checking a box on the VF page. 
 
SandhyaSandhya (Salesforce Developers) 
Hi Justin Brown,
 
Please find below code which creates the approval process for a student__c custom object when a user clicks on submit button on visual force page.
 
With some changes, I hope you will be able to workout.
 
VFPage
----------
<apex:page controller="StudentApprovalProcess">
  <apex:form >
  <apex:pageBlock >
  <apex:commandButton value="Submit Approval" action="{!approvalProcess}"/>
  </apex:pageBlock>
  </apex:form>
  </apex:page>

Controller
------------
public with sharing class Task53ApprovalProcess {

  
    public void approvalProcess() {
    
    student__c btls = new student__c();
           
           student__c b = [select id, name from student__c limit 1];
        User user1 = [SELECT Id FROM User WHERE Alias='sredd'];
            
        // Create an approval request for the student
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
       
        req1.setComments('Submitting request for approval.');
       
        req1.setObjectId(b.id);
        
        // Submit on behalf of a specific submitter
        req1.setSubmitterId(user1.Id);
         
        // Submit the record to specific process and skip the criteria evaluation
       // req1.setProcessDefinitionNameOrId('PTO_Request_Process');
        req1.setSkipEntryCriteria(true);
        
        // Submit the approval request for the student
        Approval.ProcessResult result = Approval.process(req1);

Please accept my solution as Best Answer if my answer was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya