-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretrieveSalesforceObject.js
58 lines (49 loc) · 1.7 KB
/
retrieveSalesforceObject.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
/**
* Retrieves the specified fields from a Salesforce object based
* on a specific field value (i.e. Account.Id)
* @param {string} objectName Salesforce object, i.e. 'Account'
* @param {string[]} targetFields SF API names of the fields to retrieve
* @param {string} lookupField Field to use for the lookup, i.e. 'Id'
* @param {string} lookupValue Value to check in `lookupField`
* @returns {object | undefined}
*/
function retrieveSalesforceObject(objectName, targetFields, lookupField, lookupValue) {
if (!objectName || !lookupField || !lookupValue || !targetFields) {
return undefined
}
var responseVars = ''
var rso = ''
var tfl = targetFields.length
rso += "\%\%[\n";
rso += "set @crmval = ''\n";
rso += "set @rso = RetrieveSalesforceObjects('" + objectName + "','" + targetFields.join(',') + "','" + lookupField + "','=','" + lookupValue + "')\n";
rso += "set @rc = RowCount(@rso)\n";
rso += "IF @rc > 0 THEN\n";
rso += "set @row = ROW(@rso,1)\n";
for (var i = 0; i < tfl; i++) {
var cf = '@crmval' + [i];
rso += "set " + cf + " = FIELD(@row,'" + targetFields[i] + "')\n";
if (i !== tfl-1) {
responseVars += cf + ",'|',";
} else {
responseVars += cf;
}
}
rso += "ENDIF\n";
rso += "output(Concat(" + responseVars + "))\n";
rso += "]\%\%";
var retrieved = Platform.Function.TreatAsContent(rso)
if (retrieved && retrieved.length > 0) {
var crmObject = {}
var responseValues = retrieved.split('|')
for (var j = 0; j < tfl; j++) {
var cv = responseValues[j]
if (cv === 'true') cv = true
if (cv === 'false') cv = false
crmObject[targetFields[j]] = cv
}
return crmObject
} else {
return undefined
}
}