4

On click of a Save Button, I am saving a record in Controller and in VF Page I have added oncomplete attribute to that Button to redirect to another page. There are some criteria to save a record, If not fulfilled an error msg is thrown else will save and should be redirected to another page.

Visualforce page:

<apex:commandButton status="sts" value="SavenContinue"  title="SavenContinue"  styleClass="btn btn-info" rerender="err" oncomplete="highlightactive('Tab2','Second Tab','First Tab');"  action="{!SaveMethod}" />

Controlelr:

Public pagereference Save(){ 
try{
IF(Obj.LName != '' && Obj.LName != NULL){
insert Obj;
}
else{
ApexPages.addmessage(new    ApexPages.message(ApexPages.severity.Error,'Please fill the LastName*'));
}
}
catch(Exception e){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,e.getMessage()));
}
}

The issue I am facing is, whether there is an error or not tab1 is being redirected to tab2. But I need to throw an error instead of redirecting to tab2. Only on successful save tab2 should be displayed.

Please let me know what I am missing in this code.

3
  • Try adding "return null" after writing "ApexPages.addMessage". Commented Jun 15, 2015 at 14:10
  • Are you building a completely custom controller or using a standard controller?
    – drakored
    Commented Jun 16, 2015 at 9:43
  • custom controller Commented Jun 17, 2015 at 6:55

1 Answer 1

2

Your oncomplete code will be executed when the Save method finishes executing, regardless of whether your object was inserted or not.

You need to add some logic to the oncomplete method that can check whether the Save method executed successfully or not, and only redirect if it did.

It may be as simple as having a boolean variable in the Controller that you change on successful execution of the Save method.

In my head, it would look something like this:

Controller

public Boolean isSaveSuccessful { get; set; }

public pagereference Save(){ 
   try {
       if (Obj.LName != '' && Obj.LName != NULL) {
            insert Obj;
            isSaveSuccessful = true;
       }
       else{
           ApexPages.addmessage(
               new ApexPages.message(ApexPages.severity.Error,
                                     'Please fill the LastName*')
           );
           isSaveSuccessful = false;
       }
   }
   catch(Exception e){
           ApexPages.addmessage(
               new ApexPages.message(ApexPages.severity.Error,e.getMessage())
           );
           isSaveSuccessful = false;
    }
}

Visualforce Page

<apex:outputPanel id="pnlToRerender" layout="none">
    <script type="text/javascript">
         function onSaveComplete() {
             if ({!isSaveSuccessful}) {
                 highlightactive('Tab2','Second Tab','First Tab');
             }
         }
     </script>
</apex:outputPanel>

<apex:commandButton status="sts" value="SavenContinue"  title="SavenContinue"  styleClass="btn btn-info" rerender="pnlToRerender" oncomplete="onSaveComplete();"  action="{!Save}" />

I haven't actually tested this exact code, but you get the idea.

1
  • 1
    Another useful alternative instead of manually setting the success flag during the save (unless you have a specific need) is this boolean: public boolean pageHasError { get { return ApexPages.hasMessages(ApexPages.Severity.Error); } }
    – drakored
    Commented Jun 16, 2015 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .