forked from Stwissel/node-red-contrib-salesforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsosl.js
65 lines (58 loc) · 2.57 KB
/
sosl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module.exports = function(RED) {
const nforce = require('./nforce_wrapper');
function Query(config) {
RED.nodes.createNode(this, config);
this.connection = RED.nodes.getNode(config.connection);
const node = this;
this.on('input', function(msg) {
// show initial status of progress
node.status({ fill: 'green', shape: 'ring', text: 'connecting....' });
// use msg query if node's query is blank
var query = '';
if (msg.hasOwnProperty('query') && config.query === '') {
query = msg.query;
} else {
query = config.query;
}
// Check if credentials can be used from the msg object
if (config.allowMsgCredentials && msg.hasOwnProperty("sf")) {
//TODO: Do we really need to check for empty configuration values
// or is it OK to overwrite when sf values are present?
if (msg.sf.consumerKey && this.connection.consumerKey === '') {
this.connection.consumerKey = msg.sf.consumerKey;
}
if (msg.sf.consumerSecret && this.connection.consumerSecret === '') {
this.connection.consumerSecret = msg.sf.consumerSecret;
}
if (msg.sf.username && this.connection.username === '') {
this.connection.username = msg.sf.username;
}
if (msg.sf.password && this.connection.password === '') {
this.connection.password = msg.sf.password;
}
}
// create connection object
const org = nforce.createConnection(this.connection);
// auth and run query
org
.authenticate({ username: this.connection.username, password: this.connection.password })
.then(function() {
return org.search({ search: query });
})
.then(function(results) {
msg.payload = {
size: results.length,
records: results
};
msg.payload = results;
node.send(msg);
node.status({});
})
.error(function(err) {
node.status({ fill: 'red', shape: 'dot', text: 'Error:' + err.message + '!' });
node.error(err, msg);
});
});
}
RED.nodes.registerType('sosl', Query);
};