forked from jeffdonthemic/node-red-contrib-salesforce
-
Notifications
You must be signed in to change notification settings - Fork 17
/
dml.js
80 lines (75 loc) · 2.35 KB
/
dml.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const actionHelper = require('./lib/action_helper');
/**
* Executes a DML operation on a single object
*
* @param {node-red-node} node the current node
* @param {msg} msg the incoming message
*/
const handleInput = (node, msg) => {
const config = node.config;
const realAction = (org, payload, nforce) => {
return new Promise((resolve, reject) => {
// check for overriding message properties
// action and object overwrite the configured ones
const theAction = msg.action ? msg.action : config.action;
const theObject = msg.object ? msg.object : config.object;
const sobj = nforce.force.createSObject(theObject, msg.payload);
Object.assign(payload, {
sobject: sobj
});
// Headers determine some of the behavior - we pass them on
nforce.extractHeaders(payload, msg);
let dmlResult;
switch (theAction) {
case 'insert':
dmlResult = org.insert(payload);
break;
case 'update':
dmlResult = org.update(payload);
break;
case 'upsert':
if (msg.hasOwnProperty('externalId')) {
sobj.setExternalId(msg.externalId.field, msg.externalId.value);
}
dmlResult = org.upsert(payload);
break;
case 'delete':
dmlResult = org.delete(payload);
break;
default:
// eslint-disable-next-line no-case-declarations
const err = new Error('Unknown method:' + theAction);
reject(err);
}
dmlResult
.then((sfdcResult) => {
// Find the best id
let id = msg.payload.id;
if (sfdcResult.id) {
id = sfdcResult.id;
} else if (msg.externalId) {
id = msg.externalId;
}
let result = {
success: true,
object: theObject.toLowerCase(),
action: theAction,
id: id
};
resolve(result);
})
.catch((err) => reject(err));
});
};
actionHelper.inputToSFAction(node, msg, realAction);
};
module.exports = function (RED) {
function Dml(config) {
const node = this;
RED.nodes.createNode(node, config);
node.connection = RED.nodes.getNode(config.connection);
node.config = config;
node.on('input', (msg) => handleInput(node, msg));
}
RED.nodes.registerType('dml', Dml);
};