Skip to content

Instantly share code, notes, and snippets.

@mobyjames
Created July 8, 2015 18:15
Show Gist options
  • Save mobyjames/43216e78f5517b599979 to your computer and use it in GitHub Desktop.
Save mobyjames/43216e78f5517b599979 to your computer and use it in GitHub Desktop.
Squarespace Forms Integration for Salesforce
Y.namespace('Template').Salesforce = Class.create({
/*
baseUrl
oid
sqsFormSubmit
*/
initialize: function (config) {
this.config = config;
},
submit: function () {
var formData = this.getFormData(this.config.sqsFormSubmit);
var phoneNumber = '';
if (formData['phone'] && formData['phone'].length >= 4) {
phoneNumber = formData['phone'].join('-');
}
var description = '';
for (key in formData) {
var value = formData[key];
if (Array.isArray(value)) {
value = value.join(' ');
}
description += key + ': ' + value + '\n';
}
var params = {
first_name: formData['name'][0],
last_name: formData['name'][1],
email: formData['email_address'],
phone: phoneNumber,
company: formData['company'],
lead_source: formData['sqf_lead_source'] || 'web',
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 = {};
formSubmit.get("formNode").all(".form-item").each(function(item) {
var key = null;
if (item.get('nodeName') == 'FIELDSET') {
key = item.get('id').split('-')[0];
} else {
var inputNode = item.one('.title');
if (inputNode) {
var text = inputNode.get('innerText');
key = text.replace(' *', '').replace(' ', "_").toLowerCase();
} else {
key = item._node.name.toLowerCase();
}
}
if (key) {
data[key] = formSubmit._getFieldData(item);
}
});
return data;
}
});
Y.on('domready', function() {
Y.all('form').each(function(n) {
// remove SQS form handler and get reference to form Id
var onSubmitValue = n.getAttribute('onsubmit');
var sqsFormId = onSubmitValue.match(/submit\('(.*)',/)[1];
n.setAttribute('onsubmit', '');
n.on('submit', function(e) {
e.preventDefault();
Y.use('squarespace-form-submit', 'node', function (Y) {
var formSubmit = new Y.Squarespace.FormSubmit({ formNode: n });
formSubmit._submitSuccess = function () {
// Submit to sales force
var salesforce = new Y.Template.Salesforce({
baseUrl: "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
oid: "#################", // replace with your OID from Salesforce
sqsFormSubmit: formSubmit
});
salesforce.submit();
// Show message
var formNode = this.get("formNode");
var submitText = formNode.one(".form-submission-text").cloneNode(!0);
var submitHtml = formNode.one(".form-submission-html").cloneNode(!0);
var submitHtmlData = submitHtml.getData("submission-html");
submitHtml.setHTML(submitHtmlData);
submitHtml.removeClass("hidden");
submitText.removeClass("hidden");
formNode.empty();
formNode.append(submitText).append(submitHtml);
};
formSubmit.submit(sqsFormId, sqsFormId);
});
});
});
});
@jdshepard
Copy link

i've tried putting this code in every possible place and combination on my squarespace site but can't get it to post a new lead in salesforce. i've updated site.js with my 'oid'. where should these scripts be placed? i'm not working in developer mode. thanks for your help.

@rwheaton
Copy link

@jdshepard i couldn't make it work out of the box either. i made some updates to get it working and posted a revision to this code along with a little more instruction. you'll probably want to enable developer mode... hth: https://gist.github.com/rwheaton/50d669b85fbd9548ed53

@creatyvtype
Copy link

@jdshepard I got this to work in non-developer mode. Note my comments!
My forked solution

@dchinyee
Copy link

For any developers having trouble getting this to work, try changing the regular expression pattern for sqsFormId to:
/submit\('([^']*)',/

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