+ Start a Discussion
Prince PranavPrince Pranav 

I have created a confirm popup dialog box in VisualForce page, On clicking on Ok button i want to save the record. Can anyone suggest me the solution for it.

VisualForce Page Code:

<apex:page standardController="Contact" extensions="contactExtension" standardStylesheets="true" sidebar="false">
<apex:sectionHeader title="Insert Realted Contact Deatil in Account" subtitle="{!Contact.Name}" help="/help/doc/user_ed.jsp?loc=help" /> 

<script>
function myFunction() {
  var txt;
  if (confirm("Press a button!")) {
    txt = "You pressed OK!";
  } else {
    txt = "You pressed Cancel!";
  }
  document.getElementById("demo").innerHTML = txt;
}
</script>

<apex:form >

<apex:pageBlock title="Select Account for Inserting Realted Contact Record" mode="edit">

<apex:pageBlockButtons >
<apex:commandButton action="{!save}" onclick="myFunction()" value="Save"/> 
<apex:commandButton action="{!cancel}" value="Cancel"/> 
</apex:pageBlockButtons>

<apex:pageBlockSection title="Account Detail" columns="1" >

<apex:pageBlockSectionItem >
<apex:outputLabel value="Account Name" for="accts" />  <!-- for - The ID of the component with which the label should be associated.  -->
<apex:selectList id="accts" value="{!Contact.AccountId}" size="1" title="Account">
<apex:selectOptions value="{!accts}" />
</apex:selectList>
</apex:pageBlockSectionItem>

</apex:pageBlockSection>

<apex:pageBlockSection title="Contact Deatil" columns="2">
<apex:inputField value="{!Contact.FirstName}" />
<apex:inputField value="{!Contact.LastName}" />
<apex:inputField value="{!Contact.Department}" />
<apex:inputField value="{!Contact.Phone}" />
<apex:inputField value="{!Contact.Email}" />
<apex:inputField value="{!Contact.Birthdate}" />
</apex:pageBlockSection>

</apex:pageBlock>

</apex:form>
</apex:page>


Controller Code:

public class contactExtension {

//Use a StandardController when defining an extension for a standard controller.

private final Contact c; 

// The extension constructor initializes the private member
// variable c by using the getRecord method from the standard
// controller.

public contactExtension(ApexPages.StandardController stdController) {
this.c = (Contact)stdController.getRecord();
}


public List<selectOption> getaccts() {
List<selectOption> options = new List<selectOption>(); 

options.add(new selectOption('', '- None -')); 

for (Account account : [SELECT Id, Name FROM Account]) { 

options.add(new selectOption(account.id, account.Name)); 

}
return options; 
}
}
Best Answer chosen by Prince Pranav
Maharajan CMaharajan C
Hi Prince,

Please try the below changes in VF:

<apex:page standardController="Contact" extensions="contactExtension" standardStylesheets="true" sidebar="false">
<apex:sectionHeader title="Insert Realted Contact Deatil in Account" subtitle="{!Contact.Name}" help="/help/doc/user_ed.jsp?loc=help" /> 

<script>
function myFunction() {
  var txt;
  var ret = false;
  if (confirm("Press a button!")) {
    txt = "You pressed OK!";
    ret = true;
  } else {
    txt = "You pressed Cancel!";
  }
  document.getElementById("demo").innerHTML = txt;
  return ret;

}
</script>

<apex:form >

<div id="demo" style="color:red; text-align:center"></div>

<apex:pageBlock title="Select Account for Inserting Realted Contact Record" mode="edit">

<apex:pageBlockButtons >
<apex:commandButton action="{!save}" onclick="return myFunction();" value="Save"/> 
<apex:commandButton action="{!cancel}" value="Cancel"/> 
</apex:pageBlockButtons>

<apex:pageBlockSection title="Account Detail" columns="1" >

<apex:pageBlockSectionItem >
<apex:outputLabel value="Account Name" for="accts" />  <!-- for - The ID of the component with which the label should be associated.  -->
<apex:selectList id="accts" value="{!Contact.AccountId}" size="1" title="Account">
<apex:selectOptions value="{!accts}" />
</apex:selectList>
</apex:pageBlockSectionItem>

</apex:pageBlockSection>

<apex:pageBlockSection title="Contact Deatil" columns="2">
<apex:inputField value="{!Contact.FirstName}" />
<apex:inputField value="{!Contact.LastName}" />
<apex:inputField value="{!Contact.Department}" />
<apex:inputField value="{!Contact.Phone}" />
<apex:inputField value="{!Contact.Email}" />
<apex:inputField value="{!Contact.Birthdate}" />
</apex:pageBlockSection>

</apex:pageBlock>

</apex:form>
</apex:page>

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Prince,

Please try the below changes in VF:

<apex:page standardController="Contact" extensions="contactExtension" standardStylesheets="true" sidebar="false">
<apex:sectionHeader title="Insert Realted Contact Deatil in Account" subtitle="{!Contact.Name}" help="/help/doc/user_ed.jsp?loc=help" /> 

<script>
function myFunction() {
  var txt;
  var ret = false;
  if (confirm("Press a button!")) {
    txt = "You pressed OK!";
    ret = true;
  } else {
    txt = "You pressed Cancel!";
  }
  document.getElementById("demo").innerHTML = txt;
  return ret;

}
</script>

<apex:form >

<div id="demo" style="color:red; text-align:center"></div>

<apex:pageBlock title="Select Account for Inserting Realted Contact Record" mode="edit">

<apex:pageBlockButtons >
<apex:commandButton action="{!save}" onclick="return myFunction();" value="Save"/> 
<apex:commandButton action="{!cancel}" value="Cancel"/> 
</apex:pageBlockButtons>

<apex:pageBlockSection title="Account Detail" columns="1" >

<apex:pageBlockSectionItem >
<apex:outputLabel value="Account Name" for="accts" />  <!-- for - The ID of the component with which the label should be associated.  -->
<apex:selectList id="accts" value="{!Contact.AccountId}" size="1" title="Account">
<apex:selectOptions value="{!accts}" />
</apex:selectList>
</apex:pageBlockSectionItem>

</apex:pageBlockSection>

<apex:pageBlockSection title="Contact Deatil" columns="2">
<apex:inputField value="{!Contact.FirstName}" />
<apex:inputField value="{!Contact.LastName}" />
<apex:inputField value="{!Contact.Department}" />
<apex:inputField value="{!Contact.Phone}" />
<apex:inputField value="{!Contact.Email}" />
<apex:inputField value="{!Contact.Birthdate}" />
</apex:pageBlockSection>

</apex:pageBlock>

</apex:form>
</apex:page>

Thanks,
Maharajan.C
This was selected as the best answer
Prince PranavPrince Pranav
Thanks alot Maharajan, it's working fine.