Skip to content

Instantly share code, notes, and snippets.

@torpio
Created July 24, 2014 10:25
Show Gist options
  • Save torpio/3ce56634483a32c89f6f to your computer and use it in GitHub Desktop.
Save torpio/3ce56634483a32c89f6f to your computer and use it in GitHub Desktop.
Takes a webhook from FLG 360 and duplicates the lead.
/*
This script receives a webhook from FLG 360 and copies the lead (usually into another lead group).
By default the copied lead is identical, but you can change how data is saved using 'Extra Field Mappings'.
For example, to save ```data1``` into ```data2``` on the copied lead, use:
{
"data2": "{{data1}}"
}
The fields can be any of those available in FLG 360's
[Lead Create & Update API](http://flg360.uservoice.com/knowledgebase/articles/332566-api-documentation).
*/
var settings = {};
// The lead group ID that the lead should be copied into.
settings.LEAD_GROUP_ID = ''
// A simple JSON object of extra 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.extra_field_mappings = "{\n \"data1\": \"\",\n \"data2\": \"\",\n \"data3\": \"\",\n \"data4\": \"\",\n \"data5\": \"\",\n \"data6\": \"\",\n \"data7\": \"\",\n \"data8\": \"\",\n \"data9\": \"\",\n \"data10\": \"\",\n \"data11\": \"\",\n \"data12\": \"\",\n \"data13\": \"\",\n \"data14\": \"\",\n \"data15\": \"\",\n \"data16\": \"\",\n \"data17\": \"\",\n \"data18\": \"\",\n \"data19\": \"\",\n \"data20\": \"\",\n \"data21\": \"\",\n \"data22\": \"\",\n \"data23\": \"\",\n \"data24\": \"\",\n \"data25\": \"\",\n \"data26\": \"\",\n \"data27\": \"\",\n \"data28\": \"\",\n \"data29\": \"\",\n \"data30\": \"\",\n \"data31\": \"\",\n \"data32\": \"\",\n \"data33\": \"\",\n \"data34\": \"\",\n \"data35\": \"\",\n \"data36\": \"\",\n \"data37\": \"\",\n \"data38\": \"\",\n \"data39\": \"\",\n \"data40\": \"\",\n \"data41\": \"\",\n \"data42\": \"\",\n \"data43\": \"\",\n \"data44\": \"\",\n \"data45\": \"\",\n \"data46\": \"\",\n \"data47\": \"\",\n \"data48\": \"\",\n \"data49\": \"\",\n \"data50\": \"\"\n}"
try {
JSON.parse(settings.extra_field_mappings || '{}');
var validExtraFieldMappings = true;
} catch (error) {
var validExtraFieldMappings = false;
}
if (!validExtraFieldMappings) {
log.error("The extra field mappings doesn't seem to be valid JSON. Have you enclosed all properties in quotes?");
} else if (!settings.LEAD_GROUP_ID.match(/^\d+$/)) {
log.error("The lead group ID should be numeric.");
} else if (!request.body) {
log.error("You should call this script from a FLG 360 webhook. We didn't receive any data.");
} else {
// Transform some fields from the webhook
request.body.site = request.body.siteid;
request.body.user = request.body.username;
request.body.introducer = request.body.introducerid
delete request.body.id;
delete request.body.leadgroupid;
// Add the extra field mappings if necessary
var data = _.extend(request.body, JSON.parse(helper.template(settings.extra_field_mappings || '{}', request.body)));
// Set the lead group ID
data.leadgroup = settings.LEAD_GROUP_ID;
// Build the lead create in FLG 360
var input = "<data><lead>\n";
_.each(data, function (value, key) {
input += " <" + key + ">" + XML.safe(value) + "</" + key + ">\n";
});
input += "</lead></data>\n";
// Update the lead
service.post('flg360', 'APILeadCreateUpdate.php', input, function (error, status, result) {
if (error) {
log.error("Couldn't create the lead.", error);
} else if (status >= 500 || result.indexOf('<status>0</status>') === -1) {
log.error("Couldn't create the lead.", result);
} else {
log.info("Successfully copied the lead.", result);
}
});
}
// Install with #torpio at https://torpio.com/app/scripts/shared/view/33892
/*
This script receives a webhook from FLG 360 and copies the lead (usually into another lead group).
By default the copied lead is identical, but you can change how data is saved using 'Extra Field Mappings'.
For example, to save ```data1``` into ```data2``` on the copied lead, use:
{
"data2": "{{data1}}"
}
The fields can be any of those available in FLG 360's
[Lead Create & Update API](http://flg360.uservoice.com/knowledgebase/articles/332566-api-documentation).
*/
var settings = {};
// The lead group ID that the lead should be copied into.
settings.LEAD_GROUP_ID = ''
// A simple JSON object of extra 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.extra_field_mappings = "{\n \"status\": \"New\",\n \"data1\": \"\",\n \"data2\": \"\",\n \"data3\": \"\",\n \"data4\": \"\",\n \"data5\": \"\",\n \"data6\": \"\",\n \"data7\": \"\",\n \"data8\": \"\",\n \"data9\": \"\",\n \"data10\": \"\",\n \"data11\": \"\",\n \"data12\": \"\",\n \"data13\": \"\",\n \"data14\": \"\",\n \"data15\": \"\",\n \"data16\": \"\",\n \"data17\": \"\",\n \"data18\": \"\",\n \"data19\": \"\",\n \"data20\": \"\",\n \"data21\": \"\",\n \"data22\": \"\",\n \"data23\": \"\",\n \"data24\": \"\",\n \"data25\": \"\",\n \"data26\": \"\",\n \"data27\": \"\",\n \"data28\": \"\",\n \"data29\": \"\",\n \"data30\": \"\",\n \"data31\": \"\",\n \"data32\": \"\",\n \"data33\": \"\",\n \"data34\": \"\",\n \"data35\": \"\",\n \"data36\": \"\",\n \"data37\": \"\",\n \"data38\": \"\",\n \"data39\": \"\",\n \"data40\": \"\",\n \"data41\": \"\",\n \"data42\": \"\",\n \"data43\": \"\",\n \"data44\": \"\",\n \"data45\": \"\",\n \"data46\": \"\",\n \"data47\": \"\",\n \"data48\": \"\",\n \"data49\": \"\",\n \"data50\": \"\"\n}"
try {
JSON.parse(settings.extra_field_mappings || '{}');
var validExtraFieldMappings = true;
} catch (error) {
var validExtraFieldMappings = false;
}
if (!validExtraFieldMappings) {
log.error("The extra field mappings doesn't seem to be valid JSON. Have you enclosed all properties in quotes?");
} else if (!settings.LEAD_GROUP_ID.match(/^\d+$/)) {
log.error("The lead group ID should be numeric.");
} else if (!request.body) {
log.error("You should call this script from a FLG 360 webhook. We didn't receive any data.");
} else {
// Transform some fields from the webhook
request.body.site = request.body.siteid;
request.body.user = request.body.username;
request.body.introducer = request.body.introducerid
delete request.body.id;
delete request.body.leadgroupid;
// Add the extra field mappings if necessary
var data = _.extend(request.body, JSON.parse(helper.template(settings.extra_field_mappings || '{}', request.body)));
// Set the lead group ID
data.leadgroup = settings.LEAD_GROUP_ID;
// Build the lead create in FLG 360
var input = "<data><lead>\n";
_.each(data, function (value, key) {
input += " <" + key + ">" + XML.safe(value) + "</" + key + ">\n";
});
input += "</lead></data>\n";
// Update the lead
service.post('flg360', 'APILeadCreateUpdate.php', input, function (error, status, result) {
if (error) {
log.error("Couldn't create the lead.", input, error);
} else if (status >= 500 || result.indexOf('<status>0</status>') === -1) {
log.error("Couldn't create the lead.", input, result);
} else {
log.info("Successfully copied the lead.", result);
}
});
}
// Install with #torpio at https://torpio.com/app/scripts/shared/view/33892
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment