Skip to content

Instantly share code, notes, and snippets.

@torpio
Created March 27, 2014 12:30

Revisions

  1. torpio revised this gist Aug 7, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions FLG 360 Webhook Update.js
    Original file line number Diff line number Diff line change
    @@ -57,9 +57,9 @@ if (!validFieldMappings) {
    // Update the lead
    service.post('flg360', 'APILeadCreateUpdate.php', input, function (error, status, result) {
    if (error) {
    log.error("Couldn't update the lead.", 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.", result);
    log.error("Couldn't update the lead.", input, result);
    } else {
    log.info("Updated the lead.", result);
    }
  2. torpio revised this gist Mar 28, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions FLG 360 Webhook Update.js
    Original file line number Diff line number Diff line change
    @@ -39,11 +39,11 @@ if (!validFieldMappings) {
    var data = JSON.parse(helper.template(settings.field_mappings || '{}', request.body));
    data.id = request.body.id;

    //////////////////// MANIPULATE data HERE IF NECESSARY ////////////////////
    //////////////////// MANIPULATE 'data' HERE IF NECESSARY ////////////////////



    ///////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////

    // Build the lead update in FLG 360
    var input = "<data><lead>";
  3. torpio revised this gist Mar 28, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion FLG 360 Webhook Update.js
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ if (!validFieldMappings) {



    //////////////////// MANIPULATE data HERE IF NECESSARY ////////////////////
    ///////////////////////////////////////////////////////////////////////////

    // Build the lead update in FLG 360
    var input = "<data><lead>";
  4. torpio revised this gist Mar 28, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions FLG 360 Webhook Update.js
    Original file line number Diff line number Diff line change
    @@ -39,6 +39,12 @@ if (!validFieldMappings) {
    var data = JSON.parse(helper.template(settings.field_mappings || '{}', request.body));
    data.id = request.body.id;

    //////////////////// MANIPULATE data HERE IF NECESSARY ////////////////////



    //////////////////// MANIPULATE data HERE IF NECESSARY ////////////////////

    // Build the lead update in FLG 360
    var input = "<data><lead>";

  5. torpio revised this gist Mar 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion FLG 360 Webhook Update.js
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ be any value received by the webhook (broadly the same as those available throug
    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 = "x"
    settings.field_mappings = ""

    try {
    JSON.parse(settings.field_mappings || '{}');
  6. torpio created this gist Mar 27, 2014.
    64 changes: 64 additions & 0 deletions FLG 360 Webhook Update.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    /*
    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 = "x"

    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;

    // 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.", error);
    } else if (status >= 500 || result.indexOf('<status>0</status>') === -1) {
    log.error("Couldn't update the lead.", result);
    } else {
    log.info("Updated the lead.", result);
    }
    });

    }

    // Install with #torpio at https://torpio.com/app/scripts/shared/view/33276