Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retrieve session timeout from Salesforce via Metadata API in Node.js
var jsforce = require('jsforce');
var unzip = require('unzip');
var xml2js = require('xml2js');
var sessionTimeoutSeconds = {
FifteenMinutes : 900,
ThirtyMinutes : 1800,
SixtyMinutes : 3600,
TwoHours : 7200,
FourHours : 14400,
EightHours : 28800,
TwelveHours : 43200
};
var conn = new jsforce.Connection({
oauth2 : {
clientId : process.env.CLIENT_ID,
clientSecret : process.env.CLIENT_Secret
}
});
conn.login(process.env.USERNAME, process.env.PASSWORD, function(err, userInfo) {
if (err) { return console.error(err); }
console.log("Authenticated as "+process.env.USERNAME);
// Retrive the zipped metadata resource
conn.metadata.retrieve({
apiVersion: '34.0',
singlePackage: false,
unpackaged: {
types : {
members: 'Security',
name: 'Settings'
},
version : '34.0'
}}).stream()
// Unzip it
.pipe(unzip.Parse())
.on('entry', function (entry) {
// Find the security settings
if (entry.path === "unpackaged/settings/Security.settings") {
var settings = '';
entry.on('data', function(chunk) {
settings += chunk;
});
entry.on('end', function() {
// Parse the XML and extract the session timeout
xml2js.parseString(settings, function (err, result) {
var sessionTimeout = result.SecuritySettings.sessionSettings[0].sessionTimeout[0];
console.log(sessionTimeoutSeconds[sessionTimeout]);
});
});
} else {
entry.autodrain();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment