1/**
2 * Created on 9/27/16.
3 */
4export function onAfterCalculate(quote, lines, conn) {
5 if (lines.length) {
6 var codes = [];
7 lines.forEach(function(line) {
8 var code = line.record['SBQQ__ProductCode__c'];
9 if (code) {
10 codes.push(code);
11 }
12 });
13 if (codes.length) {
14 var conditions = {
15 SBQQ__Category__c: {$in: codes}
16 };
17 var fields = ['Id', 'Name', 'SBQQ__Category__c', 'SBQQ__Value__c'];
18 /*
19 * Queries can also be constructed in a method-chaining style.
20 */
21 return conn.sobject('SBQQ__LookupData__c')
22 .find(conditions, fields)
23 .execute(function(err, records) {
24 if (err) {
25 return Promise.reject(err);
26 } else {
27 var valuesByCategory = {};
28 records.forEach(function(record) {
29 valuesByCategory[record.SBQQ__Category__c] = record.SBQQ__Value__c;
30 });
31 lines.forEach(function(line) {
32 if (line.record['SBQQ__ProductCode__c']) {
33 line.record['SBQQ__Description__c'] = valuesByCategory[line.record['SBQQ__ProductCode__c']] || '';
34 }
35 });
36 }
37 });
38 }
39 }
40 return Promise.resolve();
41}