-
Notifications
You must be signed in to change notification settings - Fork 0
/
server1.js
379 lines (333 loc) · 13.4 KB
/
server1.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
const { Console } = require('console');
var express = require( 'express' ); //Adding Expess
var http = require( 'http' ); //Adding http
var jsforce = require('jsforce'); //Adding JsForce
var httpClient = require('request');
var app = express();
//app.set( 'port', process.env.PORT || 3000 );
//Common variabbles
//Price Rule
let fieldNameValueMapCPQ1Map = new Map();
let fieldNameValueMapCPQ2Map = new Map();
let fieldNameValueMapCPQ2 = [];
//Missing Keys and Values
let missingKeyValueMapCPQ1 = new Map();
let diffFieldInCPQ1 = new Map();
let diffFieldInCPQ2 = new Map();//For Update
let diffFieldInCPQ2Obj = {};//For Update
//Price Conditions
let priceConditionsCPQ2Map = new Map();
let priceConditionsCPQObj = {};
let priceConditionsCPQObjArr = [];
//Price Actions
let priceActionsCPQ2Map = new Map();
app.get('/', function (req, res) {
/*******
* Assumption: Source:CPQ2
* Dest : CPQ1
* Filter Condition: Production_ID__c in CPQ2 has CPQ1's Record Id
*/
//***************** CPQ 2 - Connn2
var conn2 = new jsforce.Connection({
// you can change loginUrl to connect to sandbox or prerelease env.
loginUrl : 'https://nteligroup-dev-ed.my.salesforce.com'
});
var username2 = '[email protected]';
var password2 = 'cpq12345!872br6x3x3cAcxyRkpYVHjyJ';//872br6x3x3cAcxyRkpYVHjyJ
conn2.login(username2, password2, function(err, userInfo) {
if (err) { return console.error(err); }
// Now you can get the access token and instance URL information.
// Save them to establish connection next time.
//console.log(conn2.accessToken);
//console.log('Conn1 Instance URL:',conn2.instanceUrl);
//Get the CPQ 2 data
const Production_ID__c = 'a0Y4W00000HHL3YUAX';//In CPQ2
let PriceRuleIdCPQ2 = '';
conn2.sobject("SBQQ__PriceRule__c")
.find({ Production_ID__c: {$eq:Production_ID__c }}, '*')
.limit(2)
.execute(function(err, records2) {
if (err) { return console.error(err); }
try
{
for(key2 in records2)
{
let jsonStr = JSON.stringify(records2[key2]);
const obj = JSON.parse(jsonStr,function(key,value){
//console.log(' key',key);
//console.log(' Value',value);
if(key != "attributes")
{
if(key == 'Id')
{
PriceRuleIdCPQ2 = value;
}
fieldNameValueMapCPQ2Map.set(key,value);
}
});
}
// console.log('fieldNameValueMapCPQ1Map : ', fieldNameValueMapCPQ2Map );
}
catch(err) {
console.error('Error:',err);
}
console.log('fieldNameValueMapCPQ2Map:',fieldNameValueMapCPQ2Map);
console.log('PriceRuleIdCPQ2:',PriceRuleIdCPQ2);
});
//*******************CPQ2 - Get Price Conditions */
console.log('PriceRuleIdCPQ2.length:',PriceRuleIdCPQ2.length);
console.log('PriceRuleIdCPQ2.size:',PriceRuleIdCPQ2.size);
//if(PriceRuleIdCPQ2 != '')
//{
console.log('In Price Condition .....');
conn2.sobject("SBQQ__PriceCondition__c")
// .find({ SBQQ__Rule__c:'a0Y4W00000GxX21UAF'}, '*')
.find({ SBQQ__Rule__c: {$eq:this.PriceRuleIdCPQ2}}, '*')
//.select('*') // asterisk means all fields in specified level are target.
// .where("SBQQ__Rule__c = \'"+ PriceRuleIdCPQ2+"\'") // conditions in raw SOQL
//.where("SBQQ__Rule__c = \'a0Y4W00000GxX21UAF\'")
// .limit(10)
//.find({ SBQQ__Rule__c : {$eq:PriceRuleIdCPQ2 }},'*')
//.orderby("CreatedDate", "DESC")
.limit(2)
.execute(function(err, recordsPC) {
if (err) { return console.error(err); }
try
{
console.log('Price Condition count:',recordsPC.length);
for(keyPC in recordsPC)
{
let jsonStrPC = JSON.stringify(recordsPC[keyPC]);
const objPC = JSON.parse(jsonStrPC,function(key,value){
if(key != "attributes")
{
//console.log('PC Key:',key);
//console.log('PC Value:',value);
fieldNameValueMapCPQ2Map.set(key,value);
if(key != 'Id' && key.indexOf('__c') > 0 )
priceConditionsCPQObj[key] = value;
}
});
priceConditionsCPQObjArr.push(priceConditionsCPQObj);
}
console.log('priceConditionsCPQObj :',priceConditionsCPQObj);
console.log('priceConditionsCPQObj Size:',priceConditionsCPQObj.length);
console.log('priceConditionsCPQObjArr Size:',priceConditionsCPQObjArr.length);
console.log('priceConditionsCPQObjArr :',priceConditionsCPQObjArr);
//console.log('recordsPC.PriceAction records priceConditionsCPQObj:',priceConditionsCPQObj);//.records2.length);
/* let priceConditionsArr = [];
priceConditionsArr.push(record.SBQQ__PriceConditions__r.records);
console.log('record.SBQQ__PriceConditions__r.records:',record.SBQQ__PriceConditions__r.records);
console.log('priceConditions Arr count:',priceConditionsArr.length);
//priceConditionsCPQ2Map = priceConditionsArr.map(i => [i.key, i.val]);
priceConditionsCPQ2Map = priceConditionsArr.reduce(function(map, obj) {
map[obj.key] = obj.val;
priceConditionsCPQObj[obj.key] = obj.val;
return map;
}, {});
console.log('Price Conditions CPQ2Map count:',priceConditionsCPQ2Map.size);
console.log('Price Conditions CPQ2Map count:',priceConditionsCPQ2Map);
console.log('Price Conditions priceConditionsCPQObj:',priceConditionsCPQObj);*/
/*if (record.SBQQ__PriceActions__r) {
console.log('records2.PriceAction Fetch :',record.SBQQ__PriceActions__r);
console.log('records2.PriceAction Total Size :',record.SBQQ__PriceActions__r.totalSize);
console.log('records2.PriceAction Records count:',record.SBQQ__PriceActions__r.records.length);//.records2.length);
let priceActionsArr = [];
priceActionsArr.push(record.SBQQ__PriceActions__r.records);
console.log('priceActionsCPQ2Map count:',priceActionsArr.length);
console.log('priceActions Array:',priceActionsArr);
//priceActionsCPQ2Map = priceActionsArr.map(j => [j.key, j.val]);
var mapResult = priceActionsArr.reduce(function(map, obj) {
map[obj.key] = obj.val;
return map;
}, {});
console.log('PriceActions CPQ2 Array count:',mapResult.size);
console.log('PriceActions CPQ2 Array count:',mapResult);
}
}*/
//Insert Price Condition
}
catch(err) {
console.error('Error:',err);
}
console.log('fieldNameValueMapCPQ2Map:',fieldNameValueMapCPQ2Map);
});
//}
//***************** CPQ 2 - Connn2 ENDS */
/****************** CPQ 1 - Conn1 STARTS */
var conn1 = new jsforce.Connection({
// you can change loginUrl to connect to sandbox or prerelease env.
loginUrl : 'https://nteli-dev-ed.my.salesforce.com',
instanceUrl : 'https://nteli-dev-ed.my.salesforce.com'
});
var username = '[email protected]';
var password = 'cpq12345!n3OhqOlTqqiMfZso9RdgRDpXR';//n3OhqOlTqqiMfZso9RdgRDpXR
conn1.login(username, password, function(err, userInfo) {
if (err) { return console.error(err); }
// Now you can get the access token and instance URL information.
// Save them to establish connection next time.
console.log(conn1.accessToken);
console.log('Conn1 Instance URL:',conn1.instanceUrl);
// logged in user property
console.log("User ID: " + userInfo.id);
console.log("Org ID: " + userInfo.organizationId);
//Get all field name value pair - CPQ1.
conn1.sobject("SBQQ__PriceRule__c")
.find({ Id: {$eq:Production_ID__c} }, '*') // fields in asterisk, means wildcard.
.limit(2)
.execute(function(err, records1) {
if (err) { return console.error(err); }
try
{
for(key1 in records1)
{
let jsonStr = JSON.stringify(records1[key1]);
const obj = JSON.parse(jsonStr,function(key,value){
// console.log(' key',key);
//console.log(' Value',value);
if(key != "attributes" )
{
fieldNameValueMapCPQ1Map.set(key,value);
}
});
}
//console.log('fieldNameValueMapCPQ1Map : ', fieldNameValueMapCPQ1Map );
}
catch(err) {
console.error('Error:',err);
}
//console.log('fieldNameValueMapCPQ1:',fieldNameValueMapCPQ1);
});
});
console.log('Outside CPQ1:',fieldNameValueMapCPQ1Map.size);
console.log('Outside CPQ2:',fieldNameValueMapCPQ2Map.size);
/************** Field Level and Data Level Comaparison
try{
if(fieldNameValueMapCPQ2Map.size >0 && fieldNameValueMapCPQ1Map.size > 0)
{
console.log('In fieldNameValueMapCPQ2Map.size:',fieldNameValueMapCPQ2Map.size);
for (const [key, value] of fieldNameValueMapCPQ2Map.entries())
{
console.log(key, value);
//console.log('Keys Length:',fieldNameValueMapCPQ1Map.keys.length);
if(fieldNameValueMapCPQ1Map.has(key))
{
//Foe Update Query - Add the Id
if(!diffFieldInCPQ1.has('Id'))
{
// let idValueCPQ1 = fieldNameValueMapCPQ1Map.get('Id');
//diffFieldInCPQ1.set('Id',idValueCPQ1);
//fieldNameValueMapCPQ1.push('Id' +' : \''+idValueCPQ1+'\'');
diffFieldInCPQ2.set('Id',Production_ID__c);
diffFieldInCPQ2Obj['Id'] = Production_ID__c;
}
//Filter for Custom Fields
if(key.indexOf('__c') > 0 )
{
if(key != 'Production_ID__c' )//To DO: Remove this condition
{
console.log('Key found in CPQ1:',key);
let keyValueCPQ1 = fieldNameValueMapCPQ1Map.get(key);
let keyValueCPQ2 = fieldNameValueMapCPQ2Map.get(key);
if(keyValueCPQ1 != keyValueCPQ2)
{
//console.log('keyValueCPQ1 == keyValueCPQ2',keyValueCPQ1 == keyValueCPQ2);
diffFieldInCPQ1.set(key,keyValueCPQ1);
diffFieldInCPQ2.set(key,keyValueCPQ2);
diffFieldInCPQ2Obj[key] = keyValueCPQ2;
// if(typeof diffFieldInCPQ1 !== "number")
//fieldNameValueArrCPQ1.push(key +' : \''+keyValueCPQ2 + '\'');
fieldNameValueArrCPQ1.push(key +' : '+keyValueCPQ2 );
//else fieldNameValueArrCPQ1.push(key + ' : '+keyValueCPQ2 );
//console.log('diffFieldInCPQ1:',diffFieldInCPQ1 );
}
}
}
}
else
{
console.log('Key NOT found in CPQ1:',key);
let keyValue = fieldNameValueMapCPQ2Map.get(key);//.keys;
console.log('In keyValue:',keyValue);
missingKeyValueMapCPQ1.set(key,keyValue);
}
}
console.log('------------------------------------------------------');
console.log('-----------------Result--------------')
console.log('----------------- Missing Fields In CPQ1 --------------')
console.log(missingKeyValueMapCPQ1);
console.log(' ');
console.log('------------------------------------------------------');
console.log('----------------- Field Values in CPQ1 which is different from CPQ2 --------------')
console.log('CPQ1:',diffFieldInCPQ1);
console.log('CPQ2:',diffFieldInCPQ2);
/************** Field Level and Data Level Comaparison ENDS */
/************** Update the records in CPQ1(for now)
if(diffFieldInCPQ1.size > 0)
{
console.log('diffFieldInCPQ2Obj :',diffFieldInCPQ2Obj);
//Update CPQ1 with conn1
//conn1.sobject("SBQQ__PriceRule__c").update({Id : Production_ID__c, record}
//conn1.sobject("SBQQ__PriceRule__c").update({Id : Production_ID__c, record : {$eq:record}}
//conn1.sobject("SBQQ__PriceRule__c").update({Id : Production_ID__c,SBQQ__EvaluationEvent__c : 'Before Calculate;On Calculate' }
//conn1.sobject("SBQQ__PriceRule__c").update({Id : Production_ID__c,$set : setValue}
// conn1.sobject("SBQQ__PriceRule__c").update({Id : Production_ID__c}, {$set : {diffFieldInCPQ2}}
//var priceRuleObj = new SObject("SBQQ__PriceRule__c");
// priceRuleObj.Id = Production_ID__c;
// priceRuleObj. = "Closed Won";
//update the opp
//esult = sforce.connection.update([opp]);
//conn1.sobject("SBQQ__PriceRule__c").update(fieldNameValueArrCPQ1
conn1.sobject("SBQQ__PriceRule__c").update(diffFieldInCPQ2Obj
,function(err,rets)
{
if(err)
return console.error(err);
for(var i = 0; i<= rets.size;i++)
{
if(rets[i].success)
{
console.log('Updated Successfully : ' + rets[i].id);
}
}
});
}
/************** Update the records in CPQ1(for now) ENDS *******/
/************** Price Condition and Price Actions ************ */
//Get the Price Condition and Price Action for CPQ2
//Insert it into CPQ 1
console.log('if(priceConditionsCPQObjArr.length >0):',priceConditionsCPQObjArr.length);
if(priceConditionsCPQObjArr.length >0)
{
try{
console.log('In Price Condition Insert ........')
conn1.sobject("SBQQ__PriceCondition__c")
.create(priceConditionsCPQObjArr,
function(err, rets) {
if (err) { return console.error(err); }
for (var i=0; i < rets.length; i++) {
if (rets[i].success) {
console.log("Created record id : " + rets[i].id);
}
}
// ...
});
}
catch(err) {
console.error('Error:',err);
}
}
/************** Price Condition and Price Actions ENDS ************
}
}
catch(err) {
console.error('Error:',err);
}
*/
});
//conn1.timeout = 6000;
//conn2.timeout = 6000;
/*http.createServer( app ).listen( app.get( 'port' ), function (){
console.log( 'Express server listening on port ' + app.get( 'port' ));*/
});
module.exports = app;