NEQTO Docs
  • Languages iconEnglish
    • 日本語
  • Docs
  • Region API
  • Global API
  • FAQ

›neqto.js Snippets

Getting Started

  • NEQTO Hello World!
  • Step 1. Console Settings
  • Step 2. Device Setting & Start Service

    • When using NEQTO Bridge
    • When using Spresense
  • Step 3. Using Sensors

NEQTO

  • NEQTO Account Registration
  • API Usage
  • Batch Registration
  • Support Guidelines

NEQTO Console

  • Introduction
  • Fundamentals
  • Administrative Actions
  • Device Management
  • Scripts
  • Actions and Contacts
  • NEQTO Apps
  • Machine Driver
  • Recommended Browsers
  • Billing Information

SPRESENSE

    Hardware Specifications

    • 01. About Spresense

    Software Specifications

    • 01. Operational Flow
    • 02. Initial Installation
    • 03. Spresense Wi-Fi Initial Setup
    • 05. Debug Log Acquisition
    • 06. System LED Indications
    • 07. Event Messages
    • 08. Updating Firmware

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 10. RTC
    • 12. GPIO
    • 13. UART
    • 15. I2C
    • 17. Camera
    • 18. nqSpresense
    • 19. nqService
    • 20. nqMqtt
    • 21. nqFOTA
    • 22. nqWiFi

NEQTO Bridge Series

    Hardware Specifications

    • 01. NEQTO Bridge Module
    • 02. NEQTO Bridge Wi-Fi Module
    • 03. NEQTO Bridge LTE-1 Module
    • 04. NEQTO Bridge LTE-M/NB Module
    • 05. NEQTO Bridge IO Board
    • 06. NEQTO Bridge Digital IO Board

    Software Specifications

    • 01. Operational Flow
    • 02. NEQTO Bridge Wi-Fi Module Initial Setup
    • 03. NEQTO Bridge LTE Module Initial Setup
    • 04. Debug Log Acquisition
    • 05. System LED Indications
    • 06. Event Messages
    • 07. Updating Firmware

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. Sleep
    • 10. RTC
    • 11. UserSW
    • 12. GPIO
    • 13. UART
    • 14. SPI
    • 15. I2C
    • 16. ADC
    • 17. BLE
    • 18. nqBridge
    • 19. nqService
    • 20. nqMqtt
    • 21. nqFOTA
    • 22. nqWiFi
    • 23. nqLte
    • 24. nqLAN
    • 25. nqEx

neqto.js Libraries

    I2C

    • LIS2DW12 v2 Accelerometer
    • HTS221 v2 Temperature and Humidity Sensor
    • [Archive] LIS2DW12 Accelerometer
    • [Archive] HTS221 Temperature and Humidity Sensor

    Integration

    • AWS IoT Core v2 Library
    • AWS S3 v2 Library
    • Azure IoT v2 Library
    • GCP IoT Core Library
    • [Archive] AWS S3 Library
    • [Archive] AWS IoT Core Library

neqto.js Snippets

  • DataDog Snippet
  • Dropbox Snippet
  • Google Sheets Snippet
  • InfluxDB Snippet
  • Oracle Cloud Object Storage Snippet
  • Salesforce Snippet
  • SAP Cloud Platform Internet of Things Snippet
  • Splunk Snippet
  • Niagara Snippet

Release Notes

  • NEQTO Console Updates
  • NEQTO Firmware (Bridge Wi-Fi/LTE Module) Releases
  • NEQTO Firmware (Spresense Wi-Fi) Releases

Salesforce Snippet

This snippet can be used by copy-pasting to the neqto.js script.

NEQTO BridgeSPRESENSE
v00.00.30+v01.00.00+

This snippet provides a function to send IoT data to Salesforce.

Details

The send_salesforce function can be used to POST the passed object 'payload' to the specified 'object' on Salesforce using the Session ID, over HTTPS. The Session ID can be retrieved by the getSessionId function using the user credentials. The result (error/response) is then passed to the callback function.

To start using this snippet, USERNAME (Salesforce username), PASSWORD (Salesforce password), REST_VERSION (Salesforce REST API version), SOAP_VERSION (Salesforce SOAP API version), and CA are required to be configured by the user.

NOTE: The provided function only allows payloads up to size of 4KB. For bigger sized payloads, please refer to the sample for divided writing in neqto.js docs for NEQTO Bridge and Sony Spresense.

The following CA can be used (as of 2020-07-30),

/* DigiCert Global Root CA - 083be056904246b1a1756ac95991c74a */
var CA = "-----BEGIN CERTIFICATE-----\nMIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9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nPnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\nYSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\nCAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n-----END CERTIFICATE-----";
//=================================================================
// SALESFORCE SNIPPET
//=================================================================

//=================================================================
// The following configuration are MANDATORY. Set by user.
//=================================================================
// The Username for authorization.
var USERNAME = '<YOUR_USERNAME>';

// The Password for authorization.
var PASSWORD = '<YOUR_PASSWORD>';

// Salesforce REST API version.
// Find Salesforce Edition and API version - https://help.salesforce.com/articleView?siteLang=en_us&id=000334996&language=en_us&type=1&mode=1
// eg. xx.x
var REST_VERSION = '<REST_API_VERSION>';

// Salesforce SOAP API version.
// eg. xx.x
var SOAP_VERSION = '<SOAP_API_VERSION>';

// Public certificate of the certificate authority that signed the Salesforce server certificate for SSL/TLS handshake.  
// eg. '-----BEGIN CERTIFICATE-----\n...<CA>...\n-----END CERTIFICATE-----'
var CA = '<YOUR_CA>';
//=================================================================

/**
 * Get Session ID and Hostname from Salesforce using user credentials.
 * @function getSessionId
 * @param {object} sessionInfo - Session info object to hold a session's information, as an Object.
 * @param {function} callback - User callback to return the result (error/response).
 * @returns {undefined}
 */
var getSessionId = function (sessionInfo, callback) {
    var body = '<?xml version="1.0" encoding="utf-8" ?> ' +
                   '<env:Envelope ' +
                            'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                            'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                            'xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" ' +
                            'xmlns:urn="urn:partner.soap.sforce.com"> ' +
                        '<env:Header> ' +
                            '<urn:CallOptions> ' +
                                '<urn:client>RestForce</urn:client> ' +
                                '<urn:defaultNamespace>sf</urn:defaultNamespace> ' +
                            '</urn:CallOptions> ' +
                        '</env:Header> ' +
                        '<env:Body> ' +
                            '<n1:login xmlns:n1="urn:partner.soap.sforce.com"> ' +
                                '<n1:username>' + USERNAME + '</n1:username> ' +
                                '<n1:password>' + PASSWORD + '</n1:password> ' +
                            '</n1:login> ' +
                        '</env:Body> ' +
                   '</env:Envelope>';
    var options = {
        "method": 'POST',
        "host": 'login.salesforce.com',
        "path": `/services/Soap/u/${SOAP_VERSION}`,
        "headers": {
            "Content-Type": 'text/xml',
            "Content-Length": body.length.toString(),
            "SOAPAction": '""'
        },
        "ca": CA
    };
    var request = https.request(options, function (response) {
        response.on('end', function () {
            var resBody = response.read();
            if (response.statusCode == 200) { // 200 - OK
                var session = resBody.match(/<sessionId>([^<]+)<\/sessionId>/);
                sessionInfo.sessionId = session && session[1];
                sessionInfo.host = resBody.match("<serverUrl>(.*?)<\/serverUrl>")[1].split('/')[2];
            }
            callback(null, { "statusCode": response.statusCode, "statusMessage": response.statusMessage, "body": resBody });
        });
    });
    request.on('error', function () {
        callback({ "errCode": request.errCode }, null);
    });
    request.end(body.toString(), function () {
        print("[request] SUCCESS");
    });
};

/**
 * Post data to Salesforce using Session ID.
 * @function send_salesforce
 * @param {object} sessionInfo - Information about the session, as an Object.
 * @param {string} object - Name of the object to be put, as a String.
 * @param {string} payload - Data/meta-data to be sent to Salesforce, as an Object.
 * @param {function} callback - User callback to return the result (error/response).
 * @returns {undefined}
 */
var send_salesforce = function (sessionInfo, object, payload, callback) {
    var body = JSON.stringify(payload);
    var options = {
        "method": 'POST',
        "host": sessionInfo.host,
        "path": `/services/data/v${REST_VERSION}/sobjects/${object}`,
        "headers": {
            "Content-Type": 'application/json',
            'Content-Length': body.length.toString(),
            'Authorization': `Bearer ${sessionInfo.sessionId}`
        },
        "ca": CA
    };
    var request = https.request(options, function (response) {
        response.on('end', function () {
            callback(null, { "statusCode": response.statusCode, "statusMessage": response.statusMessage, "body": response.read() });
        });
    });
    request.on('error', function () {
        callback({ "errCode": request.errCode }, null);
    });
    request.end(body.toString(), function () {
        print("[request] SUCCESS");
    });
}

Function Usage Example

The following example script uses the send_salesforce function to continuously send data to Salesforce at regular intervals, and re-fetches the Session ID using the getSessionID function when it expires.

/*
<INSERT ABOVE SNIPPET HERE WITH SET CONFIGURATIONS>
*/

//=================================================================
log.setLevel(-1);       //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE
log.printLevel(2);      //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
//=================================================================
// MAIN SCENARIO
//=================================================================

/**
 * Callback to fetch error/response from the request.
 * @function callback
 * @param {object} err - Error returned in case of a failed request. Has one property - `errCode`.
 * @param {object} data - Response returned by a successfully completed request. Has three properties - `statusCode`, `statusMessage`, and `body`.
 */
var callback = function (err, data) {
    if (err) {
        print("[error]", err.errCode);
    } else {
        print("[status]", data.statusCode, data.statusMessage);
        print("[response]", data.body);
    }
}

/**
 * Object to store any session related information.
 * @enum sessionInfo
 * @property {string} host - Hostname of the server.
 * @property {string} sessionId - Session ID to authorize API requests.
 */
var sessionInfo = {
    "host": '',
    "sessionId": ''
};
var object = "test__c";
var payload = {
    "Component__r": {
        "external_id__c": 'node-component'
    },
    "Value__c": 123,
    "Thing__r": {
        "external_id__c": 'node'
    },
    "Code__c": 'ERR-01',
    "Priority__c": 'High',
    "Timestamp__c": new Date().getTime()
};

// A mutex to limit the active HTTP requests.
var lock = false;

while (1) {
    if (!lock) { // wait for the lock to release
        lock = true; // lock
        if (sessionInfo.sessionId) {
            print("[info] send_salesforce()");
            send_salesforce(sessionInfo, object, payload, function (err, data) {
                callback(err, data);
                if (data.statusCode == 401) sessionInfo.sessionId = ''; // 401 - Unauthorized
                lock = false; // unlock
            });
            setTimeout(60000).wait(); // wait for 60 seconds
        }
        else {
            print("[info] getSessionId()");
            getSessionId(sessionInfo, function (err, data) {
                callback(err, data);
                lock = false; // unlock
            });
            setTimeout(10000).wait(); // wait for 10 seconds
        }
    }
};
The company names and product names mentioned above are registered trademarks or trademarks of their respective companies.

Updated: 2021-02-19
← Oracle Cloud Object Storage SnippetSAP Cloud Platform Internet of Things Snippet →
  • Details
  • Function Usage Example
AboutNewsProductsFAQPrivacy Policy}
NEQTO Console
IntroductionFundamentalsAdministrative ActionsDevice Management NEQTO Apps
NEQTO Bridge Series
NEQTO Bridge ModuleNEQTO Bridge Wi-Fi ModuleNEQTO Bridge LTE-1 ModuleError Logging Event Messages
API Documentation
API UsageGlobal APIRegional APIAPI Terms of Service
Jigsaw, Inc.
© 2021 JIG-SAW INC.