Search code examples
salesforceapex-codevisualforce

How to add custom confirmation dialog box with Visualforce


As my Apex code show in below,

when once user execute 'saveSearchItems' action with 'apex:commandButton', if result of the validation got 'false', I want to show confirmation dialog box to user message with 'Validation failed, do you want to continue?'.

When user press 'OK' button I want to save the items (by executing saveSearchItems method) and if user press 'NO' I want to stop the saving (do nothing).

Can anyone please tell me how I can achieve this with Apex & Visualforce. Thanks a lot.

 /**
  * Save list of SearchItems 
  */
 public void saveSearchItems()  { 

    // Validate values of SearchItems 
       Boolean validateResult = validateSearchItems(this.searchItemList);

    // Save SearchItems   
       saveSearchItems(this.searchItemList);         
 }

Solution

  • Apex code executes at server, not at client so showing dialog boxes cannot happen from Apex. For that reason you will either have to move the validation to the client (using javascript onclick handler) or you will have to split the saving process in two Ajax calls to server, one to validate the other to save and then use the intermediary validation result to optionally show dialog box. The choice depends on the complexity of the validation (validateSearchItems)