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
Dman100Dman100 

display confirm dialog before processing update

I'm trying to display a confirm dialog to the user before executing an update.

 

I've tried the following:

 

<apex:pageBlockButtons > <apex:commandButton action="{!updateAccountsOpportunities}" value="Update" rerender="UpdateAccountsOpportunitiesSuccessMsg, AccountMaintenance" status="AccountsOpportunitiesLoadingStatus" onclick="return confirm('Are you sure you want to update the following records?')" /> <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" /> </apex:pageBlockButtons>

 

I discovered that I could process the update with the confirmation dialog using the actionFunction tag.  However, it causing some other problems.

 

I have three pageBlock sections wrapped in actionRegion tags, so that each pageBlock section will validate and process independently.

 

Once I added the actionFunction tag, the actionRegion no longer functions correctly.  When the page posts back, all three pageBlock sections validate.

 

Here is my VF code:

 

<apex:page Controller="AccountMaintenanceController"> <script> function confirmation() { var answer = confirm('Are you sure you want to update the following records?'); if (answer) { doSave(); } else { return false; } } </script> <h1>Manage Salesperson Territory Assignments</h1> <apex:form id="AccountMaintenance"> <apex:actionRegion > <apex:pageBlock title="Update Account and Opportunity Ownership" mode="edit"> <apex:pageBlockButtons > <apex:commandButton action="{!updateAccountsOpportunities}" value="Update" rerender="UpdateAccountsOpportunitiesSuccessMsg, AccountMaintenance" status="AccountsOpportunitiesLoadingStatus" /> <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" /> </apex:pageBlockButtons> <apex:pageMessages rendered="true" id="UpdateAccountsOpportunitiesSuccessMsg" /> <apex:actionStatus id="AccountsOpportunitiesLoadingStatus"> <apex:facet name="start"> Updating records... <img src="{!$Resource.AjaxLoading}"/> </apex:facet> </apex:actionStatus> <apex:pageBlockSection title="Used to transfer ownership of accounts and opportunities from one sales rep to another" columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="Change Owner From" for="old_account_owner" /> <apex:inputField value="{!AccountFrom.ownerId}" id="old_account_owner" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="Change Owner To" for="new_account_owner" /> <apex:inputField value="{!AccountTo.ownerId}" id="new_account_owner" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="Account Ownership" for="acct_owner" /> <apex:inputCheckbox value="{!acctChkbox}" id="acct_owner" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="Opportunity Ownership" for="oppy_owner" /> <apex:inputCheckbox value="{!oppyChkbox}" id="oppy_owner" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="Opportunity Email" for="oppy_email" /> <apex:inputCheckbox value="{!oppyEmailChkbox}" id="oppy_email" /> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:actionRegion> <apex:actionRegion > <apex:pageBlock title="Update Specific District or Account Ownership" mode="edit"> <apex:pageBlockButtons > <apex:commandButton value="Update" onclick="confirmation();" /> <apex:actionFunction name="doSave" action="{!updateChildAccountOwnership}" rerender="UpdateChildAccountsSuccessMsg, AccountMaintenance" status="ChildAccountsLoadingStatus" /> <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" /> </apex:pageBlockButtons> <apex:actionStatus id="ChildAccountsLoadingStatus"> <apex:facet name="start"> Updating records... <img src="{!$Resource.AjaxLoading}"/> </apex:facet> </apex:actionStatus> <apex:pageMessages rendered="true" id="UpdateChildAccountsSuccessMsg" /> <apex:pageBlockSection title="Used to update owner of specified account and related child accounts" columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="Change Owner To" for="new_account_owner" /> <apex:inputField value="{!AccountToByParent.ownerId}" id="new_account_owner" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="District Account to update" for="account_name" /> <apex:inputField value="{!DistrictAccount.parentId}" id="account_name" required="true" /> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:actionRegion> <apex:actionRegion > <apex:pageBlock title="Update Account Ownership by State" mode="edit"> <apex:pageBlockButtons > <apex:commandButton action="{!updateAccountByState}" value="Update" rerender="UpdateOwnerByStateSuccessMsg, AccountMaintenance" status="StateLoadingStatus" /> <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" /> </apex:pageBlockButtons> <apex:pageMessages rendered="true" id="UpdateOwnerByStateSuccessMsg" /> <apex:actionStatus id="StateLoadingStatus"> <apex:facet name="start"> Updating records... <img src="{!$Resource.AjaxLoading}"/> </apex:facet> </apex:actionStatus> <apex:pageBlockSection title="Used to update owner of accounts by selected state" columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="Change Owner To" for="new_account_owner" /> <apex:inputField value="{!AccountToByState.ownerId}" id="new_account_owner" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="State" for="state" /> <apex:inputField value="{!State.State__c}" id="state" required="true" /> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:actionRegion> </apex:form> </apex:page>

 

 

Thanks for any help.

Message Edited by Dman100 on 10-17-2009 09:52 PM
Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

In your second attempt, try returning the result of the confirm from the confirmation function.  I suspect that as it doesn't return a value when the user clicks yes may be causing the evaluation of the if some grief.

 

E.g.

 

function confirmation() { return confirm('Do you want to proceed?'); }

 

Message Edited by bob_buzzard on 10-19-2009 01:16 PM

All Answers

bob_buzzardbob_buzzard

Well that's very strange - I'm doing much the same thing in one of my pages with no problem.

 

I take it that you aren't seeing any JavaScript errors on the page, or when you click the button? 

Dman100Dman100

I do not get any javascript errors when the page loads or when I click the button.  When I try to use confirm in the following way, it will not work:

 

<apex:commandButton action="{!updateAccountsOpportunities}" value="Update" status="AccountsOpportunitiesLoadingStatus" rerender="AccountsOpportunitiesPageBlock, Msg" onclick="return confirm('Are you sure you want to proceed?');" />

 

However, if I negate the confirm, it will work:

 

<apex:commandButton action="{!updateAccountsOpportunities}" value="Update" status="AccountsOpportunitiesLoadingStatus" rerender="AccountsOpportunitiesPageBlock, Msg" onclick="if(!confirm('Are you sure you want to proceed?'))return false;" />

 

Why? 

 

Also, if I try and abstract the confirm into a function using the above method, again, it will not work?

 

function confirmation() { if (!confirm('Do you want to proceed?')) return false; } </script>

 

<apex:commandButton action="{!updateChildAccountOwnership}" value="Update" status="ChildAccountsLoadingStatus" rerender="ChildAccountsPageBlock, Msg" onclick="confirmation();" />

 

This does not work? Can someone explain what is going on here?

 

I need to abstract the the confirm into a function so I can include variables that display the number of records that will be updated...i.e. 'You are about to update ' + number_records + ' accounts'

 

Thanks.

bob_buzzardbob_buzzard

I think that its to do with the fact that this is a command button.  If the onclick returns anything, that is the end of the form submission.  Therefore when you move the return inside the if, onclick doesn't return if the user okays the submission.

 

Try altering your abstracted solution to :

 

 

<apex:commandButton action="{!updateChildAccountOwnership}" value="Update" status="ChildAccountsLoadingStatus" rerender="ChildAccountsPageBlock, Msg" onclick="if (!confirmation()) return false;" />

 

 

Dman100Dman100

I tried modifying my abstracted solution the following two ways and neither worked:

 

Attempt #1:

 

<script> function confirmation() { confirm('Do you want to proceed?'); } </script>

 

<apex:commandButton action="{!updateChildAccountOwnership}" value="Update" status="ChildAccountsLoadingStatus" rerender="ChildAccountsPageBlock, Msg" onclick="if (!confirmation()) return false;" />

Attempt #2:

 

 

function confirmation() { if (!confirm('Do you want to proceed?')) return false; } </script>

 

<apex:commandButton action="{!updateChildAccountOwnership}" value="Update" status="ChildAccountsLoadingStatus" rerender="ChildAccountsPageBlock, Msg" onclick="if (!confirmation()) return false;" />

 

I didn't expect the second option to work, but I tried it anyway.

 

I'm not sure why the first attempt does not work?  Abstracting the confirm into a function just bombs out?

 

Any ideas?

bob_buzzardbob_buzzard

In your second attempt, try returning the result of the confirm from the confirmation function.  I suspect that as it doesn't return a value when the user clicks yes may be causing the evaluation of the if some grief.

 

E.g.

 

function confirmation() { return confirm('Do you want to proceed?'); }

 

Message Edited by bob_buzzard on 10-19-2009 01:16 PM
This was selected as the best answer
BenPBenP

I have a similar problem, but slightly different.  When I click save I only want a confirmation if an array's size is less than one.  The array is named TempWODDetailArray.

 

Right now, based on this thread, I have this which prompts every time, but I'd like to only prompt if needed:

<script>
function confirmation() {

	return confirm('Do you want to enter parts?');
}
</script>


<apex:commandButton value="Save" id="theSubmitButton" action="{!SaveWorkOrder}" status="counterStatus" onclick="if (!confirmation()) return false;"/>

 

NikhilMoreNikhilMore
Hi There,

I improvised Bob's syntax by adding return false to onclick!

<apex:commandLink style="color:blue;" value="Delete" action="{!del}"  onclick="if(!confirm('Are you sure?')){return false};">

In my case the records were getting deleted even after clicking 'Cancel' on the confirm box. Returning false prevents the record from getting deleted.