Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

jdshepard commented Jan 13, 2016

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

This comment has been minimized.

Copy link

rwheaton commented Jan 22, 2016

@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

This comment has been minimized.

Copy link

creatyvtype commented Sep 15, 2016

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

@dchinyee

This comment has been minimized.

Copy link

dchinyee commented Aug 22, 2017

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
You can’t perform that action at this time.