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
KerryTKerryT 

Needed: Javascript to submit Checkbox

Hi
 
I need to use the terminology 'Email Opt In' with the checkbox on my Web-to-Lead form, but I want the form to submit the correct information to the Email Opt Out field in Salesforce.
 
This means I need the form to send the opposite value to Salesforce (ie 1 instead of 0) when the box is not checked.
 
I have been told by support that this can be done with a bit of javascript. Our webmaster has not been able to help.
 
I really need to get this sorted as soon as possible. Please can someone help?
Best Answer chosen by Admin (Salesforce Developers) 
CaptainObviousCaptainObvious
Let's say you have the following code on your form, where "00N30000000xxxx" is the generated html code for "email Opt-in":

<input type="checkbox" name="00N30000000xxxx" value="1" id="00N30000000xxxx" />
<label for="00N30000000xxxx">Yes, I'd like to be added to your e-mail list.</label>

In the submitform() function, add the following code, where "f" is the name of the form:
 
Code:
 if (document.getElementsByName('00N30000000xxxx')[0].checked == false) {
  optOut = document.createElement("input");
  optOut.setAttribute("type", "hidden");
  optOut.setAttribute("name", "emailOptOut");
  optOut.setAttribute("id", "emailOptOut");
  optOut.setAttribute("value", "1");
  document.f.appendChild(optOut);
 }

Here's what *should* happen:
 
If the email Opt-in checkbox is unchecked, an "opt-out" tag will be created and submitted with form:
 
<input type=hidden name="emailOptOut" id="emailOptOut" value="1">

However, if the email Opt-in checkbox is checked, then the "opt-out" tag will not be created!
 
I haven't tested it on a real form, but it's worth a shot! :smileyvery-happy:

All Answers

bryanobryano
All uou just need to do is check to see if your Email Opt In is checked.  If it is, set the Email Opt Out field to false.  If it's not, set the Email Opt Out field to true.  That's assuming the Email Opt Out field in salesforce is a boolean.


KerryTKerryT

Hi

I have have tried to do this but without any luck.  

Please can you post an example of the code that I would need to do this?

 

Greg HGreg H
There are a number of ways to do this but here is one suggestion:
Code:
if (document.formName.elementName.checked==true) { //if the checkbox is checked (true)
   var variableToPassBack = 0; //assign a new variable to 0 and use this variable to update salesforce
} else { //otherwise the checkbox is not checked
   var variableToPassBack = 1; //assign a new variable to 1 and pass back
}
If you post your code we would probably have better luck at assisting you with your issue.
-greg
TCAdminTCAdmin
KerryT,
 
I think this link goes over the same issue you are trying to resolve.
 
CaptainObviousCaptainObvious
Let's say you have the following code on your form, where "00N30000000xxxx" is the generated html code for "email Opt-in":

<input type="checkbox" name="00N30000000xxxx" value="1" id="00N30000000xxxx" />
<label for="00N30000000xxxx">Yes, I'd like to be added to your e-mail list.</label>

In the submitform() function, add the following code, where "f" is the name of the form:
 
Code:
 if (document.getElementsByName('00N30000000xxxx')[0].checked == false) {
  optOut = document.createElement("input");
  optOut.setAttribute("type", "hidden");
  optOut.setAttribute("name", "emailOptOut");
  optOut.setAttribute("id", "emailOptOut");
  optOut.setAttribute("value", "1");
  document.f.appendChild(optOut);
 }

Here's what *should* happen:
 
If the email Opt-in checkbox is unchecked, an "opt-out" tag will be created and submitted with form:
 
<input type=hidden name="emailOptOut" id="emailOptOut" value="1">

However, if the email Opt-in checkbox is checked, then the "opt-out" tag will not be created!
 
I haven't tested it on a real form, but it's worth a shot! :smileyvery-happy:
This was selected as the best answer
KerryTKerryT

Hi

Sorry I have not replied earlier. This bit of code as just what I was looking for. I have added an else statement so that a 0 value is also submitted. The code javascript now looks like this:

function optin(){

if (document.getElementsByName('emailOptIn')[0].checked == false) {

 optOut = document.createElement("input");

 optOut.setAttribute("type", "hidden");

 optOut.setAttribute("name", "emailOptOut");

 optOut.setAttribute("id", "emailOptOut");

 optOut.setAttribute("value", "1");

 document.form.appendChild(optOut);

 }

else {

   optOut = document.createElement("input");

 optOut.setAttribute("type", "hidden");

 optOut.setAttribute("name", "emailOptOut");

 optOut.setAttribute("id", "emailOptOut");

 optOut.setAttribute("value", "0");

 document.form.appendChild(optOut);

 }

}

This code is used in the form to create the actual checkbox

<input  value="1" type="checkbox" id="emailOptIn" name="emailOptIn" />

Where form is the name of the form on the Webpage

Thanks for your help

 

KerryTKerryT

Hi CaptinObvious

You helped me before by providing a piece of JavaScript code which updated my Email Opt Out checkbox. I am now encounter another problem on the same form. We need to validate the information entered on our webforms before it is submited to Salesforce.

 

We have fields like First Name, Last name and Industry (Salesforce Standard fields) which we have not had a problem finding JavaScript validation code which seems to happily to sit along side the code you suggested. We have a problems when we also try to validate a custom picklist field called Region that we need to make compulsory which we are finding difficult as the script doesn't appear to accept the name/ID if it starts with 2 zeros. We seem to be able to either validate the information in the form or have the email opt out working but at the moment we can't get the 2 to work together.

 

Do you have any suggestions?

CaptainObviousCaptainObvious

Hi Kerry,

Try the following:

Code:
function submitForm( form ) {

 var msg = "";
   
 if( document.f.first_name.value == "" ) { msg += "The field 'First Name' is required.\n"; }
 if( document.f.last_name.value == "" ) {  msg += "The field 'Last Name' is required.\n"; }   

 //replace the "2 zeros" id below with the id on your form:
 var region = document.f.elements['00N30000000xxxx'];
 var value = region.options[region.selectedIndex].value
 if( value == "" ) {  msg += "The field 'Region' is required.\n"; }

 if( msg == "" ) {
  //Once the form has been validated, add the hidden field to the form:
  optin();
  return true;

 } else {
  alert( "There are some problems with your submission:\n\n" + msg )
  return false;
 }

}

function optin(){

 if (document.getElementsByName('emailOptIn')[0].checked == false) {
  optOut = document.createElement("input");
  optOut.setAttribute("type", "hidden");
  optOut.setAttribute("name", "emailOptOut");
  optOut.setAttribute("id", "emailOptOut");
  optOut.setAttribute("value", "1");
  document.f.appendChild(optOut);
 } else {
  optOut = document.createElement("input");
  optOut.setAttribute("type", "hidden");
  optOut.setAttribute("name", "emailOptOut");
  optOut.setAttribute("id", "emailOptOut");
  optOut.setAttribute("value", "0");
  document.f.appendChild(optOut);
 }

}


Remember that "f" is the name of your form. See if that works for you.



Message Edited by CaptainObvious on 02-19-2008 10:21 AM