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
HayaHaya 

Return from Save and Cancel buttons

My vf page, requestCreate,  is working fine except for when the Cancel button is pressed the user is returned to the home page. I would like for the user to return to the requestCreate page. 

 

I cloned the RequestNewExtension from an existing extension.

 

I have Save and Cancel buttons:

<apex:pageBlockButtons location="both">
<apex:commandButton value="Save" action="{!Save}" />
<apex:commandButton value="Cancel" action="{!Cancel}"/>
</apex:pageBlockButtons>

 

When the user chooses SaveI direct the user to a Thank You pagethat works fine.

 

I tried different codes for the Cancel button unsuccessfully. I would like to return the user back to the requestCreate page

 

Here is my extenstion:

public with sharing class RequestNewExtension {
    public String Comment
    {
        get;
        set;
    }
    public String Description
    {
        get;
        set;
    } 
    public String Subject
    {
        get;
        set;
    }
    public Boolean UseAssignmentRules
    {
        get;set;
    }
   
    public Case cs;
    ApexPages.StandardController controller;
   
    public RequestNewExtension (ApexPages.StandardController con)
    {
        controller = con;
        this.cs = (Case) con.getRecord();
    }
   
    public PageReference Save()
    {
        CaseComment com = new CaseComment();
        if(Comment.Length() > 0)
        {
            com.commentBody = Comment;
            com.ParentId = cs.Id;
            if(UseAssignmentRules == true)
            {
                AssignmentRule  AR = new AssignmentRule();
                AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
               
                //Creating the DMLOptions for "Assign using active assignment rules" checkbox
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
                controller.getRecord().setOptions(dmlOpts);               
            }
            insert com;
        }
        String retURL = ApexPages.currentPage().getParameters().get('retURL');
        String CurrentId = ApexPages.currentPage().getParameters().get('id');
        PageReference redirectPG;
        if(retURL != null)
            redirectPG = new PageReference('/' + retURL);
        else if(CurrentId != null)
            redirectPG = new PageReference('/' + CurrentId);
       
        controller.Save();
       
         return Page.Request_Create_Thankyou;
    }
 }

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

public PageReference Cancel(){

return null;

}

 

Is it not working?

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

All Answers

souvik9086souvik9086

public PageReference Cancel(){

return null;

}

 

Is it not working?

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

This was selected as the best answer
Ajay.DixitAjay.Dixit

Just override Cancel as you have done with Save.

Use PageReference to refer to desired page.

 

return new PageReference(path where you want to navigate)

HayaHaya

I tried this in the past and got error messages.

 

I must have done something wrong.

 

Now I am returning to the calling page but the values I  entered are still on the page.

 

How do I clear the page?

Ajay.DixitAjay.Dixit

you have to set redirect as true

PageReference pageRef = new PageReference('partialURL');

pageRef.setRedirect=true;

return pageRef;

 

If you still see values then you can specifically set them as null in Cancel function.

crop1645crop1645

My experience is:

 

  1. If you use the standard controller action="{!cancel}" on Page X and the user clicks the associated Cancel button...
  2. The page that is next displayed is taken by SFDC from the retURL parm provided to page X.
  3. 'retURL' is case sensitive 

Overriding the cancel action in a standard Controller extension allows you to customize the next page on Cancel. However, the user tends to expect to go back from whence they came