Skip to content

Instantly share code, notes, and snippets.

@torpio
Created March 27, 2014 12:30
Show Gist options
  • Save torpio/9806477 to your computer and use it in GitHub Desktop.
Save torpio/9806477 to your computer and use it in GitHub Desktop.
Takes a webhook from FLG 360 and updates the same lead.
/*
This script receives a webhook from FLG 360 and updates the same lead based on the values received.
For example, to update the field ```data1``` with the date/time of the webhook, you might use a field mapping like this:
{
"data1": "{{eventdatetime}}"
}
The field can be any of those available in FLG 360's
[Lead Create & Update API](http://flg360.uservoice.com/knowledgebase/articles/332566-api-documentation). The template values can
be any value received by the webhook (broadly the same as those available through the API), plus:
- eventtype
- eventdatetime
*/
var settings = {};
// A simple JSON object of field mappings. e.g. { "field": "value" }. You can use curly bracket templating to insert any {{value}} from the FLG 360 webhook, for example {{data1}}.
settings.field_mappings = ""
try {
JSON.parse(settings.field_mappings || '{}');
var validFieldMappings = true;
} catch (error) {
var validFieldMappings = false;
}
if (!validFieldMappings) {
log.error("The field mappings doesn't seem to be valid JSON. Have you enclosed all properties in quotes?");
} else if (!request.body) {
log.error("You should call this script from a FLG 360 webhook. We didn't receive any data.");
} else {
// Create the data object from the template
var data = JSON.parse(helper.template(settings.field_mappings || '{}', request.body));
data.id = request.body.id;
//////////////////// MANIPULATE 'data' HERE IF NECESSARY ////////////////////
/////////////////////////////////////////////////////////////////////////////
// Build the lead update in FLG 360
var input = "<data><lead>";
_.each(data, function (value, key) {
input += "<" + key + ">" + XML.safe(value) + "</" + key + ">";
});
input += "</lead></data>";
// Update the lead
service.post('flg360', 'APILeadCreateUpdate.php', input, function (error, status, result) {
if (error) {
log.error("Couldn't update the lead.", input, error);
} else if (status >= 500 || result.indexOf('<status>0</status>') === -1) {
log.error("Couldn't update the lead.", input, result);
} else {
log.info("Updated the lead.", result);
}
});
}
// Install with #torpio at https://torpio.com/app/scripts/shared/view/33276
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment