Salesforce
このスニペットは、SalesforceにIoTデータを送信する機能を提供します。
Resources used: HTTPS x 1
Details
send_salesforce
関数は、HTTPS経由でセッションIDを使用して、渡されたオブジェクト「payload」をSalesforceの指定された「object」にPOSTできます。セッションIDは、ユーザーの資格情報を使用して getSessionId
関数で取得できます。結果(エラー/レスポンス)は、コールバック関数に渡されます。
このスニペットを使用するには、 USERNAME
(Salesforceのユーザーネーム)、 PASSWORD
(Salesforceのパスワード)、 REST_VERSION
(Salesforce REST APIのバージョン)、 SOAP_VERSION
(Salesforce SOAP APIのバージョン)と CA
をユーザーが設定する必要があります。
注意: 提供されている関数では、最大4KBまでのデータを取り扱うことができます。より大きなサイズのデータを取り扱う場合は、neqto.jsドキュメントのhttpsオブジェクト、分割書き込みを参考にしてください。
CAの取得方法はこちら。
var CA = "-----BEGIN CERTIFICATE-----\n...<CA>...\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 - Information about the session, 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
次のスクリプト例では、send_salesforce
関数を使用して、Salesforceに一定間隔で継続的にデータを送信します。又、セッションIDの有効期限が切れた場合、getSessionID
関数を使用して再取得を行います。
/*
<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
}
}
};
上記に記載されている会社名、製品名は、各社の登録商標または商標です。
Updated: 2023-04-14