function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Richard Houston 20Richard Houston 20 

Visualforce Confirmation Dialogue Box - Validation Rule - Save & Update Function

I'm having an issue with what appears to be an order of opperations issue with a visualforce page, custom save method, validation rules, and an onclick confirmation dialogue. Before adding the onclick piece, the page would try the save, and if it encountered a validation rule, would not update any fields in the current record.

However, with the onclick message, it now does write values to the current record. Problematically it writes to the "Submitted__c" field as true. This then changes the VF page to disable any editing. But if it encounters a validation rule, the user still needs to be able to edit. 

So the question is, why does the save method behave differently when the onclick dialogue box is added? 

Validation rule: each field requires a response (didn't want to use required fields due to issues with rerender attributes)
Or(ISBLANK(Field1),
ISBLANK(Field2),
ISBLANK(Field3),
ISBLANK(Field4))

Save method:
public PageReference saveandSubmit(){
         Try{             
           PursuitChild.Submitted__c = true;
           PursuitChild.Submitted_User__c= UserInfo.GetUserID();
           PursuitChild.Date_Submitted__c =  date.today();
                Upsert PursuitChild;
           ParentOpp.Submit_for_Approval__c = true;
           ParentOpp.Priority_Survey__c = PursuitChild.Proposed_Priority__c;
           update ParentOpp;       
           System.debug(PursuitChild);
           PageReference ref = new PageReference('/' + PursuitChild.Opportunity__c);
           ref.setRedirect(true);
           return ref;
         }
         catch(DmlException e){
           System.debug('The following exception has occurred: ' + e.getMessage());
           Apexpages.Addmessages(e);
           } 
           return null;
    }

Button with onclick function:
 
<apex:commandButton value="Save and Submit" action="{!saveandSubmit}" onclick="if(!confirm('After submitting this record it cannot be edited again.')){return false;}" />

I do have <apex:pagemessages /> included in my page to display any errors.

Thanks for the help!


Everything was operating as expected prior to adding the onclick function asking the user to confirm the action. 

 
Shikha AgashiShikha Agashi
Please add immediate attribute to skip validation rule:
 
<apex:commandButton value="Save and Submit" immediate="true" action="{!saveandSubmit}" onclick="if(!confirm('After submitting this record it cannot be edited again.')){return false;}" />

 
Richard Houston 20Richard Houston 20
Shikha,

Thanks for the reply, however I do not want the button to skip the validation rule. 

It is currently doing that without an immediate attribute. 

I figured out a possible solution with the order of operations. 
public PageReference saveandSubmit(){
         Try{ 
            //Upsert the record before updating additional fields in the method
           //This will cause any validation rules to run before writing changes
           //If the upsert is successful, then continue to update the record
           Upsert PursuitChild;
           PursuitChild.Submitted__c = true;
           PursuitChild.Submitted_User__c= UserInfo.GetUserID();
           PursuitChild.Date_Submitted__c =  date.today();
             upsert PursuitChild;