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
DevilJDevilJ 

Need help with cancel() Method

Hello,

 

I have created a wizard for a custom client object.  Following the example in the Visualforce guide, I created this method to cancel the edit:

 

 

public PageReference cancel() {   PageReference clientPage = new ApexPages.StandardController(client).view();   clientPage.setRedirect(true);   return clientPage;    }

 

 

I created a similar method for the save() method and it works great.

 

The problem I am having is when I hit cancel, my page throws errors for all the fields that require validation.  Is their a way to do a cancel from a custom controller that would just cancel much like the standardcontroller {!cancel} method?

 

Thanks,

 

DevilJ

Best Answer chosen by Admin (Salesforce Developers) 
DevilJDevilJ

Got it.  Thank you for your help!

All Answers

DevilJDevilJ

Got the partial answer (using immediately="true" in the <apex:commandButton> tag. 

 

But still having problem that method does nothing.  If I hit the cancel button, nothing happens.  The save with uses the same syntax seems to work fine.

 

Here is the code for my two methods:

 

 

public class newClientController {
    public newClientController(ApexPages.StandardController controller) {

    }

       Integer currentPage = 0;
       Client__c client;   
   public Client__c getClient() {
      if(client == null) client = new Client__c();
      return client;
   }
// Code...

//...Code

public PageReference save(){ insert client; PageReference clientPage = new ApexPages.StandardController(client).view(); clientPage.setRedirect(true); return clientPage; } public PageReference cancel() { PageReference clientPage = new ApexPages.StandardController(client).view(); clientPage.setRedirect(true); return clientPage; }

Any suggestions?

 

DevilJ

 

Imran MohammedImran Mohammed

Are you still getting validation errors when you click cancel.

One more question, are you trying to hit the cancel button before the record is inserted in database.

DevilJDevilJ

I am no longer getting validation errors since putting the "immediately="true" attribute in the <apex:commandButton> tag of the button that calls the cancel() method in the controller.  Yes, I am looking to have the form cancel before doing an insert of the data.

Imran MohammedImran Mohammed

Thats the problem, before the record persists in database, you are trying to view the detail page of that record.

Thats what view() function does there.

You make a check whether the client is null in cancel method and then redirect accordingly.

public PageReference cancel() {
	    PageReference clientPage;   							    if(client != null)								                   clientPage = new ApexPages.StandardController(client).view();
	    else											clientPage = new Pagereference('redirect to other page');			    clientPage.setRedirect(true);
            return clientPage; 
    }
DevilJDevilJ

Imran,

 

Thanks.

 

Is there a way to PageReference a tab page, say the Account tab?

 

You have:

 

 

clientPage = new Pagereference('redirect to other page');

 

 

 

Is there a way to reference the Account tab?

Imran MohammedImran Mohammed

if you want to redirect to account tab then your code should be,

clientPage = new Pagereference('/001');

let me know if any issues found.

DevilJDevilJ

Imran,

 

Thank you for your help!

 

This works perfectly.  I was following the wizard example in the visualforce manual and I can not figure out why it checks for null if it is to create a "new" opportunity. 

 

In any event, your time and understanding solved 2 days worth of me scratching my head.

 

Thank you my friend!

Imran MohammedImran Mohammed

Anyway, did you understand the flow and why a check is made or still have something else that needs to be cleared.

DevilJDevilJ

Got it.  Thank you for your help!

This was selected as the best answer