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
LaurenP6777LaurenP6777 

Invoking Javascript from Apex:CommanButton

I am trying to add a command button to my visualforce page that clones an opportunity IF certain criteria is met- if it is not met, I would like an error message to appear. This works fine via javascript in the standard UI but when I add it to my visualforce page it acts weird. 

 

Instead of firing "onclick" of the command button it fires as soon as the page loads (and if I click the button). Can someone help me? THank you. 

 

 

Here is my code:

 

<apex:commandButton value="New Integrated Opportunity" onclick="mouse();"/>
<script>
function mouse(){

if("{!Opportunity.Integrated_Opportunity2__c}"=='No'){

alert('Please select Yes for Integrated Opportunity?');
}
else
{
window.location="/{!Opportunity.Id}/e?clone=1&retURL=%2f{!Opportunity.Id}&00Nc0000000QhbH=1&00NC00000050hhz={!Opportunity.Id}";}}

mouse();
</script>

sfdcfoxsfdcfox

The last line you have will cause it to run inline. Don't use mouse() there; instead, place it on the commandButton's onclick command. It works on a normal page because the page uses an onclick handler to activate the button.

LaurenP6777LaurenP6777

That did fix my issue - the function only fires when the button is clicked. I have a new problem though....

 

The Alert is firing properly when the field value is "no" but when the field value is "yes", the user is not directed to the URL.

 

Can someone help- i can't stand to spend anymore time on this. :-(

 

<apex:commandButton value="New Integrated Opportunity" onclick="mouse();"/>
<script>
function mouse(){

if("{!Opportunity.Integrated_Opportunity2__c}"=='No'){

alert('Please select Yes for Integrated Opportunity?');
}
else
{
window.location="/{!Opportunity.Id}/e?clone=1&retURL=%2f{!Opportunity.Id}&00Nc0000000QhbH=1&00NC00000050hhz={!Opportunity.Id}";}}

</script>

MagulanDuraipandianMagulanDuraipandian

http://infallibletechie.blogspot.in/2012/10/calling-controller-method-using.html

 

Check this....

 

 

Regards,

Magulan D

Salesforce.com certified Force.com Developer.

SFDC Blog

SFDC Site

If this post is your solution, kindly mark this as the solution and give Kudos.

LaurenP6777LaurenP6777

I think I may not be clear in the question I am asking. I am able to create a criteria based alert via javascript (i.e. my original message). I am also able to create a criteria based URL redirect via VF controller (ie. the link you provided).

 

My issue is that I can not figure out how to get them to work together in ether javasript OR a vf contoller. I would like the button to redirect the user ONLY if a certain criteria is met- if not, i would like the alert to tell them that they can't move forward.

 

Does anyone know anyway, I can do both of those functions on one button? THanks!