timesync
9/15/2016 - 3:27 PM

Salesforce IQ Get Accounts (Built.io Flow Activity) This activity gets all accounts from SalesforceIQ

Salesforce IQ Get Accounts (Built.io Flow Activity) This activity gets all accounts from SalesforceIQ

/**
 * salesforceiq-get-accounts.js
 *
 * @author Kurt Collins (twitter/github: @timesync)
 *
 * @platform Built.io Flow
 *
 * @description Node.js script to create an activity for the SalesforceIQ Get Accounts call.
 *      This endpoint fetches all Accounts. Responses are paginated, max of 200 at a time.
 *
 * @input
 *      apiKey: The API Key for the SalesforceIQ app.
 *      apiSecret: The API Secret for the SalesforceIQ app.
 *      _ids: An optional, comma separated list of Account identifiers.
 *      _start: An optional starting point for the returned page of records (defaults to 0). 
 *          If you start at 50 you will retrieve records starting at the index 51 to your 
 *          _limit.
 *      _limit: An optional page size for the returned result (defaults to 50, max size is 
 *          200).
 *      modifiedDate: Fetch all accounts modified at or after this timestamp (in milliseconds 
 *          since epoch).
 *
 * @output
 *      objects: Array of JSON objects representing each account. 
 */

/**
 * Imports
 */
const request = require('request');

/**
 * Module configuration variables
 */
const activityId = 'salesforceiq-get-accounts';                             // Activity ID
const activityLabel = 'SalesforceIQ Get Accounts';                          // Activity Label
const activityHelp = 'Given Accounts from SalesforceIQ.';                   // Help Text

const activityInputSchema = {
    title: activityLabel,
    type: 'object',
    properties: {
        apiKey: {
            title: 'API Key',
            type: 'string',
            description: 'The API Key for the SalesforceIQ app.',
            propertyOrder: 1,
            minLength: 1
        },
        apiSecret: {
            title: 'API Key',
            type: 'string',
            description: 'The API Secret for the SalesforceIQ app.',
            propertyOrder: 2,
            minLength: 1
        },
        _ids: {
            title: 'Account IDs',
            type: 'string',
            description: 'An optional, comma separated list of Account identifiers.',
            propertyOrder: 3
        },
        _start: {
            title: 'Start',
            type: 'number',
            description: 'An optional starting point for the returned page of records (defaults to 0). ' +
                'If you start at 50 you will retrieve records starting at the index 51 to your limit.',
            propertyOrder: 4
        },
        _limit: {
            title: 'Limit',
            type: 'number',
            description: 'An optional page size for the returned result (defaults to 50, max size is 200).',
            propertyOrder: 5
        },
        modifiedDate: {
            title: 'Modified Date',
            type: 'number',
            description: 'Fetch all accounts modified at or after this timestamp (in milliseconds since epoch).',
            propertyOrder: 6
        }
    }
};

const activityOutputSchema = {
    title:'Output',
    type: 'object',
    properties: {
        objects: {
            type: 'array',
            title: 'objects',
            items: {
                title: 'items',
                type: 'object',
                properties: {
                    id: {
                        type: 'string',
                        title: 'id'
                    },
                    modifiedDate: {
                        type: 'number',
                        title: 'modifiedDate'
                    },
                    name: {
                        type: 'string',
                        title: 'name'
                    },
                    fieldValues: {
                        type: 'any',
                        title: 'fieldValues'
                    },
                    address: {
                        type: 'any',
                        title: 'address'
                    },
                    address_city: {
                        type: 'any',
                        title: 'address_city'
                    },
                    address_state: {
                        type: 'any',
                        title: 'address_state'
                    },
                    address_country: {
                        type: 'any',
                        title: 'address_country'
                    },
                    address_postal_code: {
                        type: 'any',
                        title: 'address_postal_code'
                    },
                    primary_contact: {
                        type: 'any',
                        title: 'primary_contact'
                    },
                    primary_ids: {
                        type: 'any',
                        title: 'primary_ids'
                    }
                }
            }
        }
    }
};

/**
 * executeActivity()
 *
 * @description This function performs the API call and any necessary manipulation.
 */
function executeActivity(input, output) {
    // Go on and perform the request.
    request({
        method: 'GET',
        auth: {
            user: input.apiKey,
            pass: input.apiSecret,
            sendImmediately: true
        },
        url: 'https://api.salesforceiq.com/v2/accounts'
    }, function optionalCallback(err, response, body) {
        if (err) {
            return output(err);
        } else {
            var returnedResponse = body;

            if (response.statusCode && response.statusCode >= 200 && response.statusCode < 400) {
                if (typeof(returnedResponse) === 'string') {
                    returnedResponse = JSON.parse(returnedResponse);
                }
                return output(null, returnedResponse);
            }
            return output(returnedResponse);
        }
    });
}

/**
 * Built.io activity creation happens here.
 */
module.exports = function() {
    this.id = activityId;
    this.label = activityLabel;
    this.help = activityHelp;
    this.input = activityInputSchema;
    this.output = activityOutputSchema;
    this.execute = executeActivity;
};