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
crikketlangcrikketlang 

Button in Visualforce page to both execute JavaScript and Apex "Save"

So we have a visualforce page which has a standard Apex button which calls the native "Save" event. After the users save the "form" so to speak, they are taken to another page which is an overview of what they just did with the options to edit or submit (which triggers an email to an internal department)

 

 

Since our sales reps aren't the brightest bunch, they would like a "pop up" to remind them to submit after they have saved. So in actullity im just looking to attach a JavaScirpt listener to that button that when its clicked, the save event is still initiated but there is a pop up which just displays a message stating "Please do not forget to submit after saving". Its almost like a confirm popup but i dont know if i can attach an apex command to the confirmation.

 

 

Does anyone have any help or advice on this one? i can post a little bit of the Visualforce page if needed. Any help is appreciated.

 

Thanks,

Dan

Best Answer chosen by Admin (Salesforce Developers) 
crikketlangcrikketlang

Sorry for the delay in getting back. We had some Exchange issues that i had to get on. I was unable to get the button to execute the JavaScript just using the onlclick event and then putting the code directly in the quotes. I ended up having to declare a function earlier in the code and then just reference it with the onclick event. ive posted the code below for anyone who may have ran into this odd issue. This ended up working perfectly.

 

<apex:page label="Insert New Client" standardController="Client__c" extensions="NewClient" tabStyle="Client__c">
<script type="text/javascript">
function displaymessage()
{
alert("Remember to Submit changes that you have made!");
}
</script>

<apex:sectionHeader title="Client Edit" subtitle="{!Client__c.LIS_Account_Name__c}"/>

Please remember to submit any changes for processing<br/>
after saving... And remember to relate needed Contacts<br/>
and Contact Types to this Client...<br/><br/>
<apex:pageMessages />
<apex:form >
<apex:pageBlock title="New Client">
   <apex:pageBlockButtons >
      <apex:commandButton value="Save" action="{!SaveClient}" onclick="displaymessage()" />

 

 

All Answers

bob_buzzardbob_buzzard

When you say standard apex button, I'm assuming that is a commandbutton?

 

That being the case, you should be able to simply add an onclick handler to it.  Something like:

 

 

<apex:commandButton value="Save" action="{!save}" onclick="alert('Please don't forget to submit!');"/>

 

As long as an onclick handler doesn't return false, the button action will continue once the user has closed the alert.

 

crikketlangcrikketlang

i actually tried this already almost exactly as you posted it but for whatever reason it never works. If i click edit on the Client record (custom object) and am taken to the edit page (the visualforce page in question), i cannot get the pop up to occur under any circumstances. i figured there is just something i am doing wrong and thought to possibly use another method of javascript instead. I guess i just dont understand why its not working and dont really know what to do from here

 

 

<apex:pageBlock title="New Client">
   <apex:pageBlockButtons >
      <apex:commandButton value="Save" action="{!SaveClient}" onclick="alert('Please don't forget to submit your changes!');"/>
      <apex:commandButton action="{!Cancel}" value="Cancel"/>      
   </apex:pageBlockButtons>

 


As far as im aware i just type the Javascript alert code in the quotes just as i would in an html page, it just doesnt seem to be working. I also notice that as i type, its pulling up the component references. Is it not recognizing the command as Javascript?

bob_buzzardbob_buzzard

Weird.  I've just tried this in my dev org and its worked fine - I get the alert, and then it continues on to submit the form.  Not sure what else to suggest tbh.  I guess you could view the source of the page to see what the rendered HTML looks like - that might give you a clue.

bob_buzzardbob_buzzard

One thing does spring to mind - if you've got some other javascript earlier in the page that is causing an error, that seems to break all javascript from that point on. 

crikketlangcrikketlang

Sorry for the delay in getting back. We had some Exchange issues that i had to get on. I was unable to get the button to execute the JavaScript just using the onlclick event and then putting the code directly in the quotes. I ended up having to declare a function earlier in the code and then just reference it with the onclick event. ive posted the code below for anyone who may have ran into this odd issue. This ended up working perfectly.

 

<apex:page label="Insert New Client" standardController="Client__c" extensions="NewClient" tabStyle="Client__c">
<script type="text/javascript">
function displaymessage()
{
alert("Remember to Submit changes that you have made!");
}
</script>

<apex:sectionHeader title="Client Edit" subtitle="{!Client__c.LIS_Account_Name__c}"/>

Please remember to submit any changes for processing<br/>
after saving... And remember to relate needed Contacts<br/>
and Contact Types to this Client...<br/><br/>
<apex:pageMessages />
<apex:form >
<apex:pageBlock title="New Client">
   <apex:pageBlockButtons >
      <apex:commandButton value="Save" action="{!SaveClient}" onclick="displaymessage()" />

 

 

This was selected as the best answer
mejoemejoe

You may need to escape the quotes in the javascript expression:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_javascript_intro.htm

SV MSV M
Hi,

I want to disalay the name of th eaccount as alert. Can you tell me what to include in alert.User-added image
I want to display name of the account in the alert. Please help me doing this..