I created two apex pages to test clicking a button to change pages and it works fine. but my cancel button just refreshes the page. can anyone please explain why this isn't working or what I need to do to get it to work

public class custNewCase {
Public String acctName {get;set;}
Public List<SelectOption> symptoms {get;set;}
Public List<SelectOption> asset250 {get;set;}  
Public List<sm1e__smEquipment__c> smEq {get;set;}
Public String ncSubject {get;set;}
Public String ncDescription {get;set;}
Public String usrid {get;set;}
Public User usr {get;set;}
Public String errMsg {get;set;}
Public String retURL {get;set;}
Public List<Account> acct {get;set;}

public PageReference cancelNC() {
  PageReference pr = new PageReference('/apex/CustomerCaseTView');
  errMsg = 'Clicked cancel button';
  return pr;

public PageReference gotoP2() {
  PageReference pr = new PageReference('/apex/RP2');
  return pr;

public PageReference retP1() {
  PageReference pr = new PageReference('/apex/RP1');
  return pr;

public void fillDD() {
  usrid = UserInfo.getUserId();
  usr = [Select AccountId From User Where id=:usrid];
  acct = [SELECT Name from Account WHERE id=:usr.AccountId];
  If (acct.Size() > 0) { acctName = acct[0].Name; }
  else { acctName = 'No Account Assigned'; }
  symptoms = new List<SelectOption>();
  symptoms.add(new SelectOption('None','-- Select from list --'));
  Schema.DescribeFieldResult fr = Case.Symptom__c.getDescribe();
  List<Schema.PicklistEntry> pl = fr.getPicklistValues();
  for(Schema.PicklistEntry p : pl) { symptoms.add(new   SelectOption(p.getValue(), p.getValue())); }
  asset250 = new List<SelectOption>();
  try { smEq = [SELECT ID, Name FROM sm1e__smEquipment__c WHERE sm1e__Account__c=:usr.AccountId And sm1e__AssetRecordType__c='S250' ORDER BY Name];
If (smEq.Size() > 0) {
  for(sm1e__smEquipment__c sm : smEq) { asset250.add(new SelectOption(sm.id, sm.name)); }
} catch (Exception e) { errMsg = e.getmessage(); system.debug(e); }


the gotoP2 and retP1 functions work absolutely perfectly in simple vf pages with a button to switch back and forth but the cancel button on this page just refreshes the page and doesn't change the url to the CustomerCaseTView page

`<apex:page standardStylesheets="false" sidebar="false"     controller="custNewCase" action="{!fillDD}">
<apex:stylesheet value="{!$Resource.CustViewCSS}"/>
<apex:form id="frm" style="line-height:1.4;">
<br />
  <apex:pageBlock >
  <apex:pageBlockButtons location="top">
    <apex:commandButton action="{!cancelNC}" value="Cancel"/>

<table width="100%"><tr><td width="30%">
<font size="4">Creating new Service Case for:<br />{!acctName}</font></td>

</td></tr></table><br />
<table width="100%">
  <tr><td style="width:10%;text-align:right;"><b>Asset:</b></td>
    <td><apex:selectList style="padding: 2px; margin: 5px;" id="asset250" value="{!asset250}" size="1">
      <apex:selectOptions value="{!asset250}"/>
<tr><td style="width:10%;text-align:right;"><b>Subject:</b></td><td>    <apex:inputText style="width:500px" value="{!ncSubject}" id="subject"/></td></tr>
<tr><td style="width:10%;text-align:right;"><b>Symptom:</b></td>
<td><apex:selectList style="padding: 2px; margin: 5px;" id="symptom" value="{!symptoms}" size="1">
      <apex:selectOptions value="{!symptoms}"/>
 <tr><td style="width:10%;text-align:right;vertical-align:top;"> <b>Description:</b></td><td><apex:inputTextarea style="width:500px;height:100px;" value="{!ncDescription}" id="desc"/></td></tr>
Error Message: {!errMsg}
  • 1. Use PageReference redirect = Page.CustomerCaseTView; instead. 2. Are you already on the CustomerCaseTView page?
    – Adrian Larson
    Commented Mar 8, 2016 at 16:20
  • They start on CustomerCaseTView they click a button and it takes them to the page I have supplied the code for. All using the exact same method of returning a PageReference from apex. Then if they decide to cancel the process and push my cancel button I want it to take them back to the CustomerCaseTView page. The suggested change made no difference it still just refreshes the page
    – Ohmicron
    Commented Mar 8, 2016 at 16:26
  • The change makes little functional difference, it is just more programmatic.
    – Adrian Larson
    Commented Mar 8, 2016 at 16:27

1 Answer 1


Your issue is probably down to validation on the page, when validation (required fields or errors) happen they block buttons from firing their actions, for actions where you don't want validation to occur you can set the immediate flag on your button


<apex:commandButton action="{!cancelNC}" value="Cancel"/>


<apex:commandButton action="{!cancelNC}" value="Cancel" immediate="true"/>

If this works then there's an error on the page which isn't visible as you don't have an <apex:pagemessages/> component on your page

  • setting immediate="true" made it work. So I must have an error somewhere. Thanks a ton thegogz. At least now I know why it's not working and can hopefully fix the error.
    – Ohmicron
    Commented Mar 8, 2016 at 16:28
  • 1
    No Problem, if you add the <apex:pagemessages/> component to your page you should see the error appear there.
    – thegogz
    Commented Mar 8, 2016 at 16:29
  • Yes I did and now I see the error. Thanks again thegogz your help is sincerely appreciatted
    – Ohmicron
    Commented Mar 8, 2016 at 16:38

You must log in to answer this question.

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