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
SeanCenoSeanCeno 

Control Redirect on Approval Process Request

All,
Due to the Winter '16 update, my approval process submission started throwing an error for our expense reports. I have corrected most of the code so that the approval goes through, but I can't seem to get the redirect to work. What do I need to add to my code in order for the browser to redirect to main object page after the user clicks "I agree" or how can I provide a success message to the users?

Line 55:
//redirect page after submitted
        if(result.isSuccess()){
            new pageReference.getParameters().put('retURL', '/' +  controller.getId());
        }
Full Class:
public with sharing class ExpenseReport_ValidateBeforeSubmit {
    private ApexPages.StandardController controller;
    public Boolean isValidated { set; get; }
    
    public ExpenseReport_ValidateBeforeSubmit(ApexPages.StandardController controller) {
        this.controller = controller;
        this.isValidated = false;
    }
    
    public PageReference validateBeforeSubmit() {
        ExpenseReport expenseReport = new ExpenseReport(controller);
        
        if (expenseReport.getExpenseLineItemListSize() == 0) {
            expenseReport.addMessage(ApexPages.Severity.Error, 'Unable to submit: '
                + 'An expense report requires at least one expense line item.');
        }
        
        for(Expense_Line_Items__c expenseLineItem : expenseReport.getExpenseLineItemList()) {
            expenseReport.setExpenseLineItem(expenseLineItem);
            
            if (expenseReport.getIsAttendeeRequiredButMissing()) {
                expenseReport.addMessage(ApexPages.Severity.Error, 'Unable to submit: '
                    + 'The expense line item categorized as "' + expenseLineItem.Category__c + '" '
                    + 'dated "' + expenseLineItem.Date_of_Expense_Item__c.format() + '" '
                    + 'and in the amount of $' + expenseLineItem.Expense_line_Item_Amount__c + ' '
                    + 'requires at least one non-owner attendee.');
            }
        }
        
        if (ApexPages.hasMessages(ApexPages.Severity.Error))
            return null;
        
        // Update the expense report to a draft status if a status is missing
        if (expenseReport.getExpenseReport().Approval_Status__c == null) try {
            update new Expense_Record__c(Id = expenseReport.getExpenseReport().Id, Approval_Status__c = 'Draft');
        } catch (System.Exception pException) {
            ApexPages.addMessages(pException);
        }
        
        isValidated = ApexPages.hasMessages(ApexPages.Severity.Error) == false;
        return null;
    }
    
    //NEW CODE
    public void confirmCertificationStatement() {
        // Create an approval request for the Expense Report
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        req1.setObjectId(controller.getId());
        
        // Submit the approval request for the Expense Report
        Approval.ProcessResult result = Approval.process(req1);
        
        //redirect page after submitted
        if(result.isSuccess()){
            new pageReference.getParameters().put('retURL', '/' +  controller.getId());
        }
        
        //Verify the result
        System.assert(result.isSuccess());
        
        System.assertEquals(
        'Pending', result.getInstanceStatus(),
        'Instance Status'+result.getInstanceStatus());
        
        //OLD CODE
        //public pageReference confirmCertificationStatement() {
        //PageReference pageReference = new PageReference('/p/process/Submit');
        //pageReference.setRedirect(true);
        //pageReference.getParameters().put('id',  controller.getId());
        //pageReference.getParameters().put('retURL', '/' +  controller.getId());
        //return pageReference;
        //}
    }
}

Visualforce
<apex:page standardController="Expense_Record__c" extensions="ExpenseReport_ValidateBeforeSubmit" action="{!validateBeforeSubmit}">
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" />
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" />
    <apex:sectionHeader title="Submit for Approval: {!$ObjectType.Expense_Record__c.Label}" subtitle="{!Expense_Record__c.Name}" />

    <apex:pageMessages />

    <apex:form style="padding: 1.5em;" styleClass="certificationStatement" rendered="{!AND(isValidated)}">
        <div style="font-size: 130%; font-weight: bold; padding-bottom: 1em;"><apex:outputText value="Certification Statement" /></div>
        <div><apex:outputText value="By submitting this report, I certify that all expenses were incurred while performing firm business and are true and correct. All listed expenses are allowable as described in the firms travel and entertainment and business expense policies. Any expenses not business related or in compliance with firm policy that do not have prior management approval may result in disciplinary action from the firm." /></div>
        <div>&nbsp;</div>
        <apex:commandButton action="{!confirmCertificationStatement}" value="I agree" />
        <apex:commandButton action="{!$Page.ExpenseReport}?id={!Expense_Record__c.Id}" value="I do not agree" />
    </apex:form>

    <apex:form style="padding: 1.5em;" rendered="{!NOT(isValidated)}">
        <apex:commandButton action="{!$Page.ExpenseReport}?id={!Expense_Record__c.Id}" value="Return to Expense Report" />
    </apex:form>
</apex:page>


 
Best Answer chosen by SeanCeno
SeanCenoSeanCeno
In case anybody needs this, the correct code for the method is below. Very simple, just wasn't seeing it.
 
//NEW CODE
    public PageReference confirmCertificationStatement() {
        // Create an approval request for the Expense Report
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        req1.setObjectId(controller.getId());
        
        // Submit the approval request for the Expense Report
        Approval.ProcessResult result = Approval.process(req1);
        
        //redirect page after submitted
        PageReference pr = null;
        if(result.isSuccess()){
            pr = new PageReference('/' + controller.getId());
        }
        return pr;
}