Created
July 8, 2015 18:15
-
-
Save mobyjames/43216e78f5517b599979 to your computer and use it in GitHub Desktop.
Squarespace Forms Integration for Salesforce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); | |
}); | |
}); |
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
@jdshepard I got this to work in non-developer mode. Note my comments!
My forked solution