-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcen_jlo_mr_etailid_datafix.js
173 lines (156 loc) · 6.06 KB
/
cen_jlo_mr_etailid_datafix.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
/**
* @NApiVersion 2.x
* @NScriptType MapReduceScript
*/
define(['N/search', 'N/record', 'N/runtime', 'N/format', 'N/error'], function(search, record, runtime, format, error) {
function getInputData() {
try {
const scriptObj = runtime.getCurrentScript();
const startDate = scriptObj.getParameter({name: 'custscript_start_date'});
const endDate = scriptObj.getParameter({name: 'custscript_end_date'});
// Format start date
var startDateObj = new Date(startDate);
var formattedStartDate = (startDateObj.getMonth() + 1) + '/' + startDateObj.getDate() + '/' + startDateObj.getFullYear();
// Format end date
var endDateObj = new Date(endDate);
var formattedEndDate = (endDateObj.getMonth() + 1) + '/' + endDateObj.getDate() + '/' + endDateObj.getFullYear();
log.debug('Date Parameters', {
rawStartDate: startDate,
formattedStartDate: formattedStartDate,
rawEndDate: endDate,
formattedEndDate: formattedEndDate
});
if (!formattedStartDate || !formattedEndDate) {
throw error.create({
name: 'MISSING_PARAMETER',
message: 'Both start and end date parameters are required'
});
}
var soSearchObj = search.create({
type: "transaction",
settings:[{"name":"consolidationtype","value":"ACCTTYPE"},{"name":"includeperiodendtransactions","value":"F"}],
filters:
[
//["type","anyof","SalesOrd","CustInvc"],
//["type","anyof","SalesOrd"],
//'AND',
//['mainline', 'is', 'T'],
//'AND',
//['trandate', 'onorafter', formattedStartDate],
//'AND',
//['trandate', 'onorbefore', formattedEndDate],
//'AND',
//['custbody_jlo_etail_order_id', 'isempty','']
["type","anyof","SalesOrd","CustInvc"],
"AND",
["trandate","within",formattedStartDate,formattedEndDate],
"AND",
["custbody_jlo_etail_order_id","isempty",""],
"AND",
["mainline","is","T"],
"AND",
["custbody_celigo_etail_order_id","isnotempty",""]
],
columns: [
search.createColumn({name: 'internalid'}),
search.createColumn({name: 'type'}),
search.createColumn({name: 'custbody_celigo_etail_order_id'}),
search.createColumn({name: 'trandate'})
]
});
log.debug('soSearchObj', soSearchObj);
return soSearchObj;
} catch (e) {
log.error('getInputData Error', {
message: e.message,
stack: e.stack,
params: {
startDate: startDate,
endDate: endDate
}
});
throw e;
}
}
function map(context) {
try {
const searchResult = JSON.parse(context.value);
const soId = searchResult.values.internalid.value;
const tranType = searchResult.values.type.value;
const etailId = searchResult.values.custbody_celigo_etail_order_id;
context.write({
key: soId,
value: {
soId: soId,
tranType: tranType,
etailId: etailId || ''
}
});
} catch (e) {
log.error('Map Error', {
message: e.message,
stack: e.stack,
context: context.value
});
}
}
function reduce(context) {
try {
const data = JSON.parse(context.values[0]);
// Load and update the sales order in chunks to handle governance
if (runtime.getCurrentScript().getRemainingUsage() < 100) {
return;
}
log.debug("tran",data.tranType+":"+data.soId+":"+data.etailId);
var recType = null;
if (data.tranType === "SalesOrd") {
recType = record.Type.SALES_ORDER;
} else if (data.tranType === 'CustInvc') {
recType = record.Type.INVOICE;
}
record.submitFields({
//type: record.Type.SALES_ORDER,
type: recType,
id: data.soId,
values: {
'custbody_jlo_etail_order_id': data.etailId
},
options: {
enableSourcing: false,
ignoreMandatoryFields: true
}
});
} catch (e) {
log.error('Reduce Error', {
message: e.message,
stack: e.stack,
key: context.key,
values: context.values
});
}
}
function summarize(summary) {
try {
log.audit('Script Summary',
'Total Records Processed:' + summary.reduceSummary.keys.length +
', Total Records with Errors:' + summary.reduceSummary.errors.length
);
// Log all errors
summary.reduceSummary.errors.iterator().each(function(key, error) {
log.error('Reduce Error for Sales Order ' + key, error);
return true;
});
} catch (e) {
log.error('Summarize Error', {
message: e.message,
stack: e.stack
});
}
}
return {
getInputData: getInputData,
map: map,
reduce: reduce,
summarize: summarize
};
});