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 controller extension


Hi All, Im attemtping to write a controller extention to the standard controller "CIOA_Submissions__c" I have a created a VF page which serves as my submission form and it is being controlled by the standard controller. I need my extension to render the approval process via a 'submit' commandbutton, and also have the record approved from the visual force page via the 'approve' commandbutton. I get an invalid type for "Approval.ProcessWorkitemRequest". Any suggestions on code corrections?

public class ApprovalExtender{
CIOA_Submissions__c extension;

public ApprovalExtender(ApexPages.standardController stdController){
    this.extension=(CIOA_Submissions__c) stdController.getRecord();
}

public override void int(){
}

public PageReference approve(){
   Id id=ApexPages.currentPage().getParameters().get('id');
    
    Integer cnt=[SELECT COUNT() FROM Processinstance WHERE TargetObjectid = :id AND Status='Pending'];
    System.debug ('count=' + cnt);
    if(cnt==0){
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Not pending record'));
        return null;
    }
    return null;
}
    Id processId=[SELECT Id FROM ProcessInstance WHERE TargetObjectId = :id AND Status = 'Pending' Limit 1].Id;
    Id workitemId=[SELECT Id FROM ProcessInstanceWorkItem WHERE ProcessInstanceId = :processId Limit 1].Id;
    
    Approval.ProcessWorkitemRequest = new Approval.ProcessWorkitemRequest();
    request.setAction('Approve');
    request.setNextApproverIds(new Id[]{UserInfo.getUserId()});
    request.setWorkitemId(workitemId);
    Approval.ProcessResult result=Approval.process(request);
    
    if(result.isSuccess()){
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Submission Approved'));
        }
        else{
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Submission Pending'));
        }
        return null;
    }
     
     public PageReference submit(){
         Id id=ApexPages.currentPages().getParameters().get('id');
         
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval');
        req1.setNextApproverIds(new Id[]{Userinfo.getUserId()});
        req1.setObjectId(id);
        Approval.ProcessResult result = Approval.process(req1);
         
          if(result.isSuccess()){
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Submission Submitted Successfully'));
        }
        else{
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity,ERROR,'Submission Not Submitted Please Try Again'));
        }
        return null;
        }
     }
Rohit K SethiRohit K Sethi
Hi ,

I thing there is little bit mistake in code that is why it raise an error. The error is that you not declare variable "request".
 Approval.ProcessWorkitemRequest = new Approval.ProcessWorkitemRequest();

So declare the variable request then it will be run.
 Approval.ProcessWorkitemRequest request = new Approval.ProcessWorkitemRequest();

Thanks.