-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcen_jlo_mr_etailid_pmt_datafix.js
More file actions
151 lines (135 loc) · 5.09 KB
/
cen_jlo_mr_etailid_pmt_datafix.js
File metadata and controls
151 lines (135 loc) · 5.09 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
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
/**
* @NApiVersion 2.1
* @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_etail_pmt_start'});
const endDate = scriptObj.getParameter({name: 'custscript_etail_pmt_end'});
// 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 suiteQL = `
select t.id, tl.custcolcustcol_shpfy_orgnl_order, t.custbody_jlo_etail_link_pmt, t.custbody_cen_jlo_digital_pmt_ord
from transaction t, transactionline tl
where t.custbody_jlo_etail_link_pmt is null
and trandate >= to_date(?,'MM/DD/YYYY')
and trandate <= to_date(?,'MM/DD/YYYY')
and recordtype = 'invoice'
and t.id = tl.transaction
and tl.custcolcustcol_shpfy_orgnl_order is not null
--and t.id = 4067139
order by t.id desc
`;
return {
type: 'suiteql',
query: suiteQL,
params: [formattedStartDate,formattedEndDate]
};
} 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);
//log.debug("map",searchResult);
const invId = searchResult.values[0];
const originalOrder = searchResult.values[1];
log.debug("map2",invId + ":" + originalOrder);
context.write({
key: invId,
value: {
invId: invId,
etailId: originalOrder
}
});
} 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]);
log.debug("tran",data.invId+":"+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: record.Type.INVOICE,
id: data.invId,
values: {
'custbody_jlo_etail_link_pmt': 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
};
});