You need to sign in to do that
Don't have an account?
Developer.mikie.Apex.Student
cancel button immediate="True" Required Fields Help
Hey there,
I have a Cancel button on my visualforce page, it works for every single one of my custom objects except the transactions object. Upon testing it out manually It would not cancel the record creation page and return to my tabbed VF page because of required fields. I searched a bit and learnt about immediate="true" which would trigger the cancel function before the validation rules can take effect.
Upon inserting the immediate="true" parameter into my visualforce page and testing, I was re-directed to a page that said I had insufficient privledges to access the page.
Below are my extension and VF page with relevent sections highlighted. Any light on the subject would be appreciated.
Extension:
public class extSaveTraButton {
public Transaction__c tra {get;set;}
public extSaveTraButton(ApexPages.StandardController controller) {
this.tra = (Transaction__c)controller.getRecord();
}
Public PageReference saveDestinyTransaction(){
if(System.currentPageReference().getParameters().get('clone') == '1'){
//Set record id to null
tra.Id = null;
}
insert tra;
// Send the user to the detail page for the new account.
PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+tra.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Transactions');
return pageRef;
}
Public PageReference cancelDestinyTransaction(){
PageReference pageRef = new PageReference('/apex/DestinyAccount?id='+tra.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Transactions');
return pageRef;
}
}
VF page:
<apex:page standardController="Transaction__c" extensions="extSaveTraButton">
<apex:form >
<apex:pageBlock >
<apex:commandButton value="Save" action="{!saveDestinyTransaction}"/>
<apex:commandButton value="Cancel" immediate="true" action="{!cancelDestinyTransaction}"/>
<apex:pageBlockSection >
<apex:repeat value="{!$ObjectType.Transaction__c.FieldSets.TransactionsFieldSet}" var="f">
<apex:inputfield value="{!Transaction__c[f]}">
</apex:inputfield></apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
I have a Cancel button on my visualforce page, it works for every single one of my custom objects except the transactions object. Upon testing it out manually It would not cancel the record creation page and return to my tabbed VF page because of required fields. I searched a bit and learnt about immediate="true" which would trigger the cancel function before the validation rules can take effect.
Upon inserting the immediate="true" parameter into my visualforce page and testing, I was re-directed to a page that said I had insufficient privledges to access the page.
Below are my extension and VF page with relevent sections highlighted. Any light on the subject would be appreciated.
Extension:
public class extSaveTraButton {
public Transaction__c tra {get;set;}
public extSaveTraButton(ApexPages.StandardController controller) {
this.tra = (Transaction__c)controller.getRecord();
}
Public PageReference saveDestinyTransaction(){
if(System.currentPageReference().getParameters().get('clone') == '1'){
//Set record id to null
tra.Id = null;
}
insert tra;
// Send the user to the detail page for the new account.
PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+tra.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Transactions');
return pageRef;
}
Public PageReference cancelDestinyTransaction(){
PageReference pageRef = new PageReference('/apex/DestinyAccount?id='+tra.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Transactions');
return pageRef;
}
}
VF page:
<apex:page standardController="Transaction__c" extensions="extSaveTraButton">
<apex:form >
<apex:pageBlock >
<apex:commandButton value="Save" action="{!saveDestinyTransaction}"/>
<apex:commandButton value="Cancel" immediate="true" action="{!cancelDestinyTransaction}"/>
<apex:pageBlockSection >
<apex:repeat value="{!$ObjectType.Transaction__c.FieldSets.TransactionsFieldSet}" var="f">
<apex:inputfield value="{!Transaction__c[f]}">
</apex:inputfield></apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
All Answers
In that scenario you have two options:
1.) remove immediate tag from the button
2.) place actionsupport onchange inside each input, have it call a void method that is blank in the controller...purpose being to update view state when the onchange event occurs for each input.
I assume that I need to keep the immediate="true" tag as a result of the validation rules preventing redirection to the page before upon clicking cancel.
Which would mean that I would have to go with option 2? How would I go about adding actionsupport? Do i add it to the commandbutton on the VF page or to the class?
For the action support approach, do a blank method in your controller something like:
public void refreshvariables()
{
}
Then for each input field something like this:
<apex:inputfield value="{!Transaction__c[f]}">
<apex:actionSupport event="onchange" action="{!refreshvariables}"/>
</apex:inputfield>
The problem that made me use the immediate on the button was that upon clicking cancel, when the button was meant to redirect to my tabbed VF page with the transactions tab open (transaction is a child of account and part of my tabbed VF page). It gave me an error and stayed on the page as the required fields stopped the page navigation.
I will attempt this action support approach.
Thank you so much for your assistance phillip, you have been a massive help.
public class extSaveTraButton {
public Transaction__c tra {get;set;}
public extSaveTraButton(ApexPages.StandardController controller) {
this.tra = (Transaction__c)controller.getRecord();
}
Public PageReference saveDestinyTransaction(){
if(System.currentPageReference().getParameters().get('clone') == '1'){
//Set record id to null
tra.Id = null;
}
insert tra;
// Send the user to the detail page for the new account.
PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+tra.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Transactions');
return pageRef;
}
Public PageReference cancelDestinyTransaction(){
PageReference pageRef = new PageReference('/apex/DestinyAccount?id='+tra.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Transactions');
return pageRef;
}
public void refreshvariables()
{
<apex:inputfield value="{!Transaction__c[Amount__c]}">
<apex:actionSupport event="onchange" action="{!refreshvariables}"/>
</apex:inputfield>
<apex:inputfield value="{!Transaction__c[Date_of_Payment__c]}">
<apex:actionSupport event="onchange" action="{!refreshvariables}"/>
</apex:inputfield>
}
}
ok, this is my class:
public class extSaveTraButton {
public Transaction__c tra {get;set;}
public extSaveTraButton(ApexPages.StandardController controller) {
this.tra = (Transaction__c)controller.getRecord();
}
Public PageReference saveDestinyTransaction(){
if(System.currentPageReference().getParameters().get('clone') == '1'){
//Set record id to null
tra.Id = null;
}
insert tra;
// Send the user to the detail page for the new account.
PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+tra.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Transactions');
return pageRef;
}
Public PageReference cancelDestinyTransaction(){
PageReference pageRef = new PageReference('/apex/DestinyAccount?id='+tra.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Transactions');
return pageRef;
}
public void refreshvariables(){
}
}
This is my page:
<apex:page standardController="Transaction__c" extensions="extSaveTraButton">
<apex:form >
<apex:pageBlock >
<apex:commandButton value="Save" action="{!saveDestinyTransaction}"/>
<apex:commandButton value="Cancel" immediate="True" action="{!cancelDestinyTransaction}"/>
<apex:pageBlockSection >
<apex:repeat value="{!$ObjectType.Transaction__c.FieldSets.TransactionsFieldSet}" var="f">
<apex:inputfield value="{!Transaction__c[f]}">
<apex:actionSupport event="onchange" action="{!refreshvariables}"/>
</apex:inputfield>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
and still whenever i click cancel with immediate="true" it tells me that I do not have the permission and without immediate="true" the validation rules will not allow navigation away from the record creation page
Where you say "tra.Id = null" looks like you are trying to clone and force an insert. I don't think that will work, you don't have permission to write or set the Id field. A better approach would be:
Transaction__c traClone = tra.clone();
And then insert traclone and redirect to it.
Also after that, if you run into the error again, open up the dev console or do a debug monitor for the user and see what line you are getting the error from.
I wrote the suggested line into the code as such:
public class extSaveTraButton {
public Transaction__c tra {get;set;}
public extSaveTraButton(ApexPages.StandardController controller) {
this.tra = (Transaction__c)controller.getRecord();
}
Public PageReference saveDestinyTransaction(){
if(System.currentPageReference().getParameters().get('clone') == '1'){
//Set record id to null
Transaction__c traClone = tra.clone();
}
insert tra;
// Send the user to the detail page for the new account.
PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+tra.account__c+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Transactions');
return pageRef;
But I still got a validation rule prevention when attempting to cancel without the immediate="true' tag and I got insufficient privledges when I had it.
I know this is a stupid question, but how do I use the developer console to find out where the error is?
(be careful and make sure you click on the right log, you may need to sort them by date/time...also it should list a status like the error message you get)
Credit to this blog post for helping find the right solution! http://www.tgerm.com/2014/06/cancel-out-of-html5-browser-based-validations-novalidate.html