I have a visualforce page having a Case detail view from which there is a link which opens a new visualforce Page for attachment for that particular case. I am opening that attachment visualforce page by passing the values through Query string.

<apex:outputLink styleClass="btn btn-link" target="_top"
  New Attachment 

Then there is a the visualforce Page and controller.

The problem is that even through the attachment is working fine, I am not able to redirect the user back to the Case Detail page from which the user clicked the New Attachment link (button). After attachment the user stays on the same attachment visualforce page. User should be redirected back to Case Detail on Finish Uploading & Submit with No attachment (or Cancel) button. Please tell me the best way to redirect user back to their selected CaseDetail page preferablly using visualforce or Apex as well (if Needed).

// The visualforce Page:

<apex:page controller="WSC_NewAttachment_Case" docType="html-5.0" showHeader="false" sidebar="false" cache="false" standardStylesheets="true">
                <!-- Insert global site header css and meta tags-->
   <c:AWSHeader />
      <style type="text/css">
             margin-left: 0px;

          <!-- container section start -->
           <section id="container" class="">

           <!-- Header Bar -->
           <c:AWSHeaderBar />
           <!--header end-->
                        <!--main content start-->
                        <section id="main-content" style="width:100%;">
                                <section class="wrapper">

               <flow:interview id="flowId" name="Drinking_Water_Enquiry_or_concern" finishLocation="{!URLFOR('/AWSHome')}" interview="{!myflow}" rendered="{! NOT(upload)}">
                      <apex:param name="varRetailerContactEmail" value="{!$User.Email}"/>
                   <apex:form enctype="multipart/form-data">

                         <apex:outputPanel id="theAttachmentPanel" >
                         <apex:sectionHeader title="Attachment Upload (Upto 10 MB)"/>

                              <apex:pageMessages />
                              <apex:pageBlock title="Upload an Attachment">

                               <apex:pageBlockButtons >
                                     <apex:commandButton action="{!SaveMore}" value="Upload More"/>
                                     <apex:commandButton action="{!Save}" value="Finish Uploading" />
                                     <apex:commandButton action="{!Cancel}" value="Submit with no Attachment" />

                                <apex:pageBlockSection showHeader="false" columns="2" id="block1">

                                 <apex:pageBlockSectionItem >
                                     <apex:outputLabel value="File Name" for=" filesName "/>
                                     <!-- <apex:inputText value="{!attachment.name}" id="filesName"/> -->
                                      <apex:inputText value="{!displayName}" id="filesName"/>

                                 <apex:pageBlockSectionItem >
                                      <apex:outputLabel value="File" for="file"/>
                                      <apex:inputFile value="{!attachment.body}" filename="{!fName}" id="file"/>

                      <apex:pageBlockSectionItem >
                          <apex:outputLabel value="Description" for="description"/>
                          <apex:inputTextarea value="{!attachment.description}" id="description"/>





// And the relating Apex Class (Controller):

public with sharing class WSC_NewAttachment_Case {

// The attachment object
public Attachment attachment {
        get {
                if (attachment == null)
        attachment = new Attachment();
        return attachment;

// Specific flow name for flow type

public String varCaseId = ApexPages.currentPage().getParameters().get( 'varCaseId' );
public String varUpload = ApexPages.currentPage().getParameters().get( 'varUpload' );
public Boolean upload;
public Boolean uploaded = false;
public String fName{ get; set;}
public String displayName{get; set;}

// The Case Id value from the flow
  public String getvarCaseId()

        return varCaseId;


// The varUpload variable value from the flow
//public String getvarUpload()
 // {
   //   If(myFlow == null)
   //             Return String.valueOf('0');
   //     Else
     //           Return myflow.varUpload;

 Public Boolean getupload()
         If(varUpload == '1' && uploaded!= null && uploaded == false)
               Return true;
                 Return false;

// Save method, leading to next step
public PageReference Save()
        return upload(System.Boolean.valueOf(true));

// Save More method, leading to the attachment upload functionality again
public PageReference SaveMore()
        return upload(System.Boolean.valueOf(false));

// Cancel method, leading to next step in flow
public PageReference Cancel()
        This.uploaded =  true;
        attachment = null;
        return null;

// Upload method for actual attachment upload, setting the upload variable appropriately to allow/ not allow another attachment upload
public PageReference upload(Boolean varUploaded)
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = varCaseId; // 'a0C61000004sw8h'; // the record the file is attached to
    attachment.IsPrivate = true;

    if( (!(attachment.name != null && attachment.name != '')) && (fName != null && fName != ''))
        String afterExt = fName.substringAfter('.');
        System.debug('afterExt :: ' + afterExt );
        if(displayName != null && displayName != '')
            attachment.name = displayName + '.' + afterExt;
            attachment.name = fName;
        if(!(attachment.body != null))
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please specify a file to be uploaded'));
        else if (!(attachment.name != null && attachment.name != ''))
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'Please specify a file name'));

        insert attachment;
        displayName = '';
        fName = '';

    catch (DMLException e)
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
      attachment = new Attachment();

    //ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    This.uploaded =  varUploaded;
    // true for save and false for Save more
    return null;

// End of upload

  • in your upload method change last 'return null;' to 'return new PageReference('/'+varCaseId);' Commented Jul 9, 2016 at 21:32

1 Answer 1


The most proper way to construct a redirect PageReference is to use the StandardController.view method. In this case, you need to also construct the record itself with the varCaseId.

PageReference view = new ApexPages.StandardController(new Case(Id=varCaseId)).view();
return view;

You don't even need to cache it, I just do so here because I find it more illustrative. As @Ilya mentions in the comments, you want to replace return null with this at the end of your upload method.

For custom Visualforce Pages, the programmatic approach would be:

PageReference detail = Page.CaseDetail;
detail.getParameters().put('id', varCaseId);
return detail;
  • Thanks a lot Adrian. You gave me an idea of how to go forward about it. I was trying to redirect it in the wrong part of the code earlier. Also the specific requirement was that I cannot use the standard controller pages as it is for a community and use has to be redirected back to specific CaseDetail visualforce page with the same ID. Here is how I resolved it with the help of your idea. PageReference view = new PageReference('/apex/CaseDetail?id='+this.varCaseId); return view;
    – AMAN
    Commented Jul 10, 2016 at 22:44
  • @AMAN See my updated post for a cleaner approach to that as well.
    – Adrian Larson
    Commented Jul 10, 2016 at 23:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .