Salesforce Guru:
I am writing a Salesforce visualforce page extension controller to implement validation and other biz logic in it. However, my code doesn't work as expected. It seems the code validating sub category and serial number is never reached. The system debug log shows the sub category is always null though I have input values in VF page. Can you pls help? thank you very much!
public class CaseCreationExtension {
ApexPages.StandardController stdCtrl;
Case newCase;
public CaseCreationExtension(ApexPages.StandardController controller){
if (!Test.isRunningTest()){
controller.addFields(new List<String>{'Categ__c', 'Sub_Category__c', 'Id', 'Serial_Number__c', 'Refund_Required__c', 'Total_Value__c', 'Per_Month__c'});
system.debug('AddFields');
}
system.debug('Initial Controller');
this.stdCtrl = controller;
newCase = (Case)stdCtrl.getRecord();
system.debug('AccountId' + newCase.AccountId);
}
public PageReference validateSaveRedirect(){
system.debug('Sub Category:' + newCase.Sub_Category__c);
system.debug('Serial Number:' + newCase.Serial_Number__c);
if(newCase.Sub_Category__c == 'Cancellations' && newCase.Serial_Number__c == null){
system.debug('Adding error message');
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Serial Number must be specified for cancellation case.');
ApexPages.addMessage(myMsg);
system.debug('finished Adding error message');
return null;
}else{
if(null==stdCtrl.save()){
return null;
}
/*try{
stdCtrl.save();
}
catch(system.Exception e){
ApexPages.Message msg = new apexPages.Message(Apexpages.Severity.ERROR, e.getdmlMessage(0));
ApexPages.addMessage(msg);
return null;
}*/
stdCtrl.save();
system.debug('New Case Id:' + newCase.Id);
PageReference pr = new PageReference('/' + newCase.Id);
pr.setRedirect(true);
return pr;
}
}
public PageReference cancelRedirect(){
stdCtrl.cancel();
system.debug('New Case Id:' + newCase.Contact);
PageReference pr = new PageReference('/' + newCase.Contact);
pr.setRedirect(true);
return pr;
}
}
What did you base your VF form on?
<apex:inputField value="{!case.Sub_Category__c}"/>
? It seems thenewCase
variable is private + it doesn't have any getter/setter.From what I remember the act of calling
(Case)stdCtrl.getRecord();
"decouples" the variables. It's more of a copy than a reference to same object. So it's likely thestdCtrl
has then values you've set but they weren't passed on to thenewCase
because it was "cloned" out ofstdCtrl
before the user has edited the data. Insert aSystem.debug(stdCtrl);
into your method; if I'm right - you'll see your new values in there.You could move the
(Case)stdCtrl.getRecord();
into that method.Or - change your apex to say
public Case newCase {get;set;}
and modify the VF form to reference{!newCase.Sub_Category__c}
etc.