I have a custom object with a visualforce page, 2 controllers, and a custom button. The issue is if I use the "Save & New" custom button my validation for a field won't show the error on my page. However if I use the standard Save button it will. The "Save & New" WILL the exception if I don't fill out a required field.
Validation:
Rule Name No_Test_Validation
Active Checked
Error Condition Formula AND(ISPICKVAL(Result__c ,"No Test"), ISBLANK(Testing_Notes__c))
Error Message WARNING! If test result is "No Test" you need to enter Testing Notes
Visualforce Page:
<apex:page standardController="Test__c" showheader="True"><apex:pagemessages / ><apex:form >
<apex:sectionHeader title="{!$ObjectType.Test__c.label}" subtitle="{!Test__c.name}"/>
<apex:pageBlock title="{!$ObjectType.Test__c.label} Detail" >
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Edit Test Info" showHeader="true" columns="2">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Test Number" />
<apex:outputfield value="{!Test__c.Test_Number__c}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Inspection Id" />
<apex:outputfield value="{!Test__c.Inspection__c}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Type of Test" />
<apex:inputfield value="{!Test__c.Type_of_Test__c}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Test Value" />
<apex:inputField value="{!Test__c.Test_Value__c}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Result" />
<apex:inputField value="{!Test__c.Result__c}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Test Unit Id #" />
<apex:inputField value="{!Test__c.Test_Unit_ID__c}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Testing Notes" />
<apex:inputField value="{!Test__c.Testing_Notes__c}" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection showHeader="false" columns="2">
<apex:outputField value="{!Test__c.CreatedById}"/>
<apex:outputField value="{!Test__c.LastModifiedById}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
Controller:
public with sharing class ButtonSaveAndNew {
private Test__c foo;
private ApexPages.StandardController sController;
private String queryString;
public ButtonSaveAndNew (ApexPages.StandardController controller) {
sController = controller;
foo = (Test__c)controller.getRecord();
// Keep track of any inbound query string parameters so they can be passed along when creating a new record
List<string> pageUrl = ApexPages.currentPage().getUrl().split('\\?');
queryString = pageUrl[1];
}
public PageReference saveAndNew() {
try {
// Save the current sObject
sController.save();
// Get the Meta Data for Foo__c
Schema.DescribeSObjectResult describeResult = sController.getRecord().getSObjectType().getDescribe();
// Create PageReference for creating a new sObject and add any inbound query string parameters.
PageReference pr = new PageReference('/' + describeResult.getKeyPrefix() + '/e?' + queryString);
// Don't redirect with the viewstate of the current record.
pr.setRedirect(true);
return pr;
} catch(Exception e) {
// Don't redirect if something goes wrong. May be a validation or trigger issue on save.
ApexPages.addMessages(e);
return null;
}
}
}
Second Controller
public class getTotalTests {
ApexPages.StandardController sController;
public getTotalTests(ApexPages.StandardController controller) {
sController = controller;
system.debug('TotalTestLoad');
}
public Integer getTests(){
Test__c record = (Test__c)scontroller.getRecord();
List<Test__c> t = [SELECT Id, Inspection__c
FROM Test__c
WHERE Inspection__c = :record.Inspection__c];
Integer numb = t.size() +1;
return numb;
}
}