-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path_sfOOBOB2CSitesUpdate.js
More file actions
98 lines (72 loc) · 3.52 KB
/
_sfOOBOB2CSitesUpdate.js
File metadata and controls
98 lines (72 loc) · 3.52 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
// Initialize required modules
const sfdx = require('sfdx-node/parallel');
/**
* @function _sfOOBOB2CSitesUpdate
* @description Attempts to update the OOBO Customer details for each of the B2C Sites associated using the customer details
*
* @param {Object} environmentDef Represents the already-validated environment details to use when performing the actions
* @param {Object} customerProfile Represents the customerProfile that will be applied to related B2C Sites
* @returns {Promise} Returns the results of the siteUpdate request re: the OOBO site-specific properties
*/
module.exports = (environmentDef, customerProfile) =>
new Promise(async (resolve, reject) => {
// Initialize local variables
let output;
// Initialize the output variable
output = {};
try {
// Retrieve the B2C CustomerList
output.b2cCustomerList = await sfdx.force.data.recordGet({
_quiet: true,
sobjecttype: 'B2C_CustomerList__c',
where: `Name='${customerProfile.customerListId}'`
});
// Exit early if the customerList isn't verified
if (!output.b2cCustomerList) {
// Capture the error resolving the customerList
throw new Error(`Unable to resolve B2C CustomerList: ${customerProfile.customerListId}`);
} else {
// Retrieve the sites associated to the current customerList
output.b2cSites = await sfdx.force.data.soqlQuery({
_quiet: true,
query: `SELECT Id, Name FROM B2C_Site__c WHERE Customer_List__c='${output.b2cCustomerList.Id}'`
});
// Was at least one site found?
if (output.b2cSites.totalSize > 0) {
// Create the updates elements
output.b2cSiteUpdates = {};
// If so, then let's default each of the b2cSites
for (let thisSite of output.b2cSites.records) {
// Initialize the siteUpdate output
output.b2cSiteUpdates[thisSite.Name] = {};
// Update the current B2C Site and reset the customerId
output.b2cSiteUpdates[thisSite.Name].customerId = await sfdx.force.data.recordUpdate({
_quiet: true,
sobjecttype: 'B2C_Site__c',
sobjectid: thisSite.Id,
values: `OOBO_Customer_ID__c='${customerProfile.customerId}'`
});
// Update the current B2C Site and reset the customerNo
output.b2cSiteUpdates[thisSite.Name].customerNo = await sfdx.force.data.recordUpdate({
_quiet: true,
sobjecttype: 'B2C_Site__c',
sobjectid: thisSite.Id,
values: `OOBO_Customer_Number__c='${customerProfile.customerNo}'`
});
}
}
// Flag the result as valid
output.success = true;
// Return the object representations
resolve(output);
}
} catch (e) {
// Flag the result as invalid
output.success = false;
output.error = e;
output.stack = e.stack;
// Reject the result
reject(output);
}
});