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
Gheorghe Sima 21Gheorghe Sima 21 

Popup message visualforce page

Hi,
I have a javascript code what shows me a popup message, but when i click cancel it execute me the action from the command button.
here is my code:
<script type="text/javascript">
        function Verif(emailAddress, phone) 
        {
            if(emailAddress == '' && phone == '')
                 return confirm('Are you sure?');
            return null;     
        }
   </script>

and here is the button declaration:
<apex:commandButton action="{!save}" value="Save" onclick="Verif(document.getElementById('{!$Component.pb.pbs.pbsie.email}').value,document.getElementById('{!$Component.pb.pbs.pbsip.phone}').value );"  />
User-added image
When I click Cancel on the popup window it execute me the action from the button and I don't want to do this. Can anyone help me?
Best Answer chosen by Gheorghe Sima 21
Abhishek BansalAbhishek Bansal
Hi Sima,

I have added a actionFunction in your VF page and also changed your JS function.
Please find the VF page below :
<apex:page>
	<script type="text/javascript">
        function Verif(emailAddress, phone){
            if(emailAddress == '' && phone == ''){
            	if(window.confirm('Are you sure ?')){
            		callSaveMethod();
            	}
            }
        }
   	</script>
   	<apex:form>
   		<apex:actionFunction name="callSaveMethod" action="{!save}">
   		<apex:commandButton value="Save" onclick="Verif(document.getElementById('{!$Component.pb.pbs.pbsie.email}').value,document.getElementById('{!$Component.pb.pbs.pbsip.phone}').value );" />
   	</apex:form>
</apex:page>
Please modify your JS function and add a actionFunction in your VF page by taking help from above code.
Let me know if you have any issue with the above code or if you want more help on this.

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi Sima,

I have added a actionFunction in your VF page and also changed your JS function.
Please find the VF page below :
<apex:page>
	<script type="text/javascript">
        function Verif(emailAddress, phone){
            if(emailAddress == '' && phone == ''){
            	if(window.confirm('Are you sure ?')){
            		callSaveMethod();
            	}
            }
        }
   	</script>
   	<apex:form>
   		<apex:actionFunction name="callSaveMethod" action="{!save}">
   		<apex:commandButton value="Save" onclick="Verif(document.getElementById('{!$Component.pb.pbs.pbsie.email}').value,document.getElementById('{!$Component.pb.pbs.pbsip.phone}').value );" />
   	</apex:form>
</apex:page>
Please modify your JS function and add a actionFunction in your VF page by taking help from above code.
Let me know if you have any issue with the above code or if you want more help on this.

Thanks,
Abhishek
This was selected as the best answer
Gheorghe Sima 21Gheorghe Sima 21
It works now. Thank you!
Gheorghe Sima 21Gheorghe Sima 21
It is a problem. When i click cancel it works fine, but when I click OK it don't execute the actionFunction...
Gheorghe Sima 21Gheorghe Sima 21
@Abhishek Bansal  can you help me with this problem?
Abhishek BansalAbhishek Bansal
Hi Sima,

First of all i apolozise for reaching you so late.
I am not able to figure out why it is not working for you.
It would be good if you can share your full code here so that i can help you.
In case of any emergency you can reach me at
Gmail : abhibansal2790@gmail.com
SkypeId : abhishek.bansal2790

Let me know if you need any other help.

Thanks,
Abhishek
Gheorghe SimaGheorghe Sima
Hi Bansal,

I have a validation rule on Contact which don't let me make a Contact without Email or Phone number, but I have some cases when the user can create an Contact without email and Phone. I have a visualforce page to create a new contact and when the user click Save and he don't put the email or phone I want to show a popup window and ask him if he want for sure to create a contact without email or phone. Can you explain me how to do this?
 
Gheorghe SimaGheorghe Sima
let me know if you want more information :D
Abhishek BansalAbhishek Bansal
Hi Sima,

I think this is something difeerent from the original question.
Please post this new question on another Developer Forum.
If you have any emergency please contact me personally on Gmail or Skype (I have already provided the Ids in above reply).

If you want solution on Developer Forum than Please create a new question and send me the link here.
I will definitely reach you out there and provide you all the possible help.

Thanks,
Abhishek
Gheorghe SimaGheorghe Sima
https://developer.salesforce.com/forums/ForumsMain?id=906F00000005Ie4#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F00000005J7uIAE

here is the link for the new question and below is my page:
<apex:page standardController="Lead_Contact__c" extensions="NewContactOnLeadController" recordSetVar="var">
   <script type="text/javascript">
        function Verif(emailAddress, phone){
            if(emailAddress == '' && phone == ''){
                  return confirm('Are you sure?');
             }
             return null;
         }
    </script>
<apex:sectionHeader title="New Contact"/>
<apex:pageMessages />
<apex:form >

<apex:pageBlock title="Contact Edit" id="pb">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" onclick="Verif(document.getElementById('{!$Component.pb.pbs.pbsie.email}').value,document.getElementById('{!$Component.pb.pbs.pbsip.phone}').value );" styleClass="btn" />
                <apex:commandButton action="{!cancel}" value="Cancel" styleClass="btn" />
            </apex:pageBlockButtons>
         <apex:pageBlockSection title="Contact Information" columns="2" id="pbs">
             <apex:pageBlockSectionItem >
                  <apex:outputLabel value="Salutation" for="salutation" />
                  <apex:inputField id="salutation" value="{!result.Salutation}" required="true" />
             </apex:pageBlockSectionItem>
               <apex:pageBlockSectionItem id="pbsip">
                  <apex:outputLabel value="Phone" for="phone" />
                  <apex:inputField id="phone" value="{!result.Phone}"/>
             </apex:pageBlockSectionItem>
             <apex:pageBlockSectionItem >
                  <apex:outputLabel value="First Name" for="firstname" />
                  <apex:inputField id="firstname" value="{!result.FirstName}" required="true" />
             </apex:pageBlockSectionItem>
             <apex:pageBlockSectionItem id="pbsie">
                  <apex:outputLabel value="Email" for="email" />
                  <apex:inputField id="email" value="{!result.Email}"/>
             </apex:pageBlockSectionItem>
            
             <apex:pageBlockSectionItem >
                  <apex:outputLabel value="Last Name" for="lastname" />
                  <apex:inputField id="lastname" value="{!result.LastName}" required="true" />
             </apex:pageBlockSectionItem>
             <apex:pageBlockSectionItem >
                  <apex:outputLabel value="Owner" for="owner" />
                  <apex:outputText id="owner" value="{!name}" />
             </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        <apex:pageBlockSection title="Relation Information">
            <apex:pageBlockSectionItem >
                  <apex:outputLabel value="Role" for="role" />
                  <apex:inputField id="role" value="{!myRelation.RoleOnAccount__c}"/>
             </apex:pageBlockSectionItem>
              <apex:pageBlockSectionItem >
                  <apex:outputLabel value="Start Date" for="startDate" />
                  <apex:inputField id="startDate" value="{!myRelation.Start_Date__c}"/>
             </apex:pageBlockSectionItem> 
             <apex:pageBlockSectionItem ></apex:pageBlockSectionItem>
             <apex:pageBlockSectionItem >
                  <apex:outputLabel value="End Date" for="endDate" />
                  <apex:inputField id="endDate" value="{!myRelation.End_Date__c}"/>
             </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>