[SalesForce] Why does the returned PageReference not work

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');
  pr.setRedirect(true);
  errMsg = 'Clicked cancel button';
  return pr;
}

public PageReference gotoP2() {
  PageReference pr = new PageReference('/apex/RP2');
  pr.setRedirect(true);
  return pr;
}

public PageReference retP1() {
  PageReference pr = new PageReference('/apex/RP1');
  pr.setRedirect(true);
  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"/>

  </apex:pageBlockButtons>
<table width="100%"><tr><td width="30%">
<font size="4">Creating new Service Case for:<br />{!acctName}</font></td>
<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}"/>
    </apex:selectList>
</td></tr>
<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}"/>
    </apex:selectList>
</td></tr>
 <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>
</table>
Error Message: {!errMsg}
</apex:pageBlock>
</apex:form>
</apex:page>

Best Answer

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

Change

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

To

<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

Related Topic