Skip to content

Instantly share code, notes, and snippets.

@rwheaton
Forked from mobyjames/salesforce.js
Last active April 4, 2019 15:33
Show Gist options
  • Save rwheaton/50d669b85fbd9548ed53 to your computer and use it in GitHub Desktop.
Save rwheaton/50d669b85fbd9548ed53 to your computer and use it in GitHub Desktop.
Squarespace Forms Integration for Salesforce
// add this salesforce.js file to your scripts dir after enabling developer mode
// this gets included via entries in the site.region file
Y.namespace('Template').Salesforce = Class.create({
/*
baseUrl
oid
sqsFormSubmit
*/
initialize: function (config) {
this.config = config;
},
submit: function () {
var formData = this.getFormData(this.config.sqsFormSubmit);
phoneArr = new Array(formData['phone-area-code'], formData['phone-local-prefix'], formData['phone-local-suffix']);
var phoneNumber = phoneArr.join('-');
var description = '';
for (key in formData) {
var value = formData[key];
if (Array.isArray(value)) {
value = value.join(' ');
}
description += key + ': ' + value + '\n';
}
// add data from form values pulled below in getFormData().
// replace the keys for title, company, and address with those
// that are generated for your form
var params = {
first_name: formData['fname'],
last_name: formData['lname'],
email: formData['email'],
phone: phoneNumber,
title: formData['your-yui-field-id'],
company: formData['your-yui-field-id'],
address: formData['your-yui-field-id'],
lead_source: formData['sqf_lead_source'] || 'Contact Form',
description: description,
oid: this.config.oid
};
$.ajax({
url: this.config.baseUrl,
data: params,
type: 'GET',
dataType: 'jsonp',
jsonp: false,
complete: function(data) {
debugger;
console.log('done');
}
});
},
getFormData: function (formSubmit) {
var data = {};
Y.all('input,textarea,select,button').each(function(item) {
var key = null;
var $element = $(this);
// this builds an array of input name -> value entered
// in the sqsp forms, fields outside of name and email
// don't have names and instead use random YUI ids.
// jquery is included to pull in some extra data for the
// phone number fields. you need to find the ids for your
// extra form fields and add them to params above.
if (item.get('name')) {
key = item.get('name');
} else if ($element.attr('x-autocompletetype')) {
key = $element.attr('x-autocompletetype');
} else {
key = item.get('id');
}
data[key] = item.get('value');
});
console.log(data);
return data;
}
});
// add this to the top of your scripts/site.js file after enabling developer mode and pulling down your site.
Y.on('domready', function() {
Y.use('event', 'node', function(Y) {
var submitbuttons = Y.all('input[type=submit]');
submitbuttons.on("click", function() {
var formSubmit = $('form').serializeArray();
// Submit to sales force
var salesforce = new Y.Template.Salesforce({
baseUrl: "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
oid: "00DE0000000IPLv", // replace with your OID from Salesforce
sqsFormSubmit: formSubmit
});
salesforce.submit();
});
});
//include at the bottom near where other scripts are loaded to enable custom js & jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="scripts/jquery-2.0.0.min.js"><\/script>')</script>
<squarespace:script src="salesforce.js" combo="true" />
@kmclaugh
Copy link

kmclaugh commented Apr 4, 2019

Has anyone received a cross origin error with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment