-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcen_jlo_ue_if_class_update.js
153 lines (128 loc) · 6.89 KB
/
cen_jlo_ue_if_class_update.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
/**
*@NApiVersion 2.1
*@NScriptType UserEventScript
/= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\
* Purpose:
* VER DATE AUTHOR CHANGES
* 1.0 November 6, 2023 Centric Consulting(Pradeep) Initial Version
*
* This should only be needed to update historical records, new IFs should pick up
* these values from the sales order.
*
* Also note: none of the Shopify items (Shopify Line Discount, etc) come through to the IF, since they
* are not fulfilled. Location is also set so that the items can be picked/shipped.
*
* Class is also not available at the header level on an IF.
*
* Based on the above points, we only need to set class at the line level for fulfilled items.
*
\= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
define(['N/record','N/search', 'N/query', 'N/runtime'], function (record, search, query, runtime) {
function afterSubmitSetClass(scriptContext) {
try {
log.debug("enter before submit",scriptContext.type);
var continuityClass = runtime.getCurrentScript().getParameter({name: 'custscript_cen_jlo_continuity2'});
var instagramClass = runtime.getCurrentScript().getParameter({name: 'custscript_cen_jlo_insta2'});
var entryClass = runtime.getCurrentScript().getParameter({name: 'custscript_cen_jlo_entry2'});
var shopifyChannel = runtime.getCurrentScript().getParameter({name: 'custscript_cen_jlo_channel'});
// only run for EDIT and In Line Edit (XEDIT - MASS UPDATE), create should default from the
// Sales Order
if (scriptContext.type === scriptContext.UserEventType.EDIT ||
scriptContext.type === scriptContext.UserEventType.XEDIT ) {
var newRecord = record.load({
type: record.Type.ITEM_FULFILLMENT,
id: scriptContext.newRecord.id,
isDynamic: false
});
log.debug("new rec",newRecord);
var customerId = newRecord.getValue({ fieldId: 'entity'});
log.debug("Customer",customerId);
if (customerId) {
var custClass = search.lookupFields({
type: search.Type.CUSTOMER,
id: customerId,
columns: ['custentityjlo_customer_class']
});
log.debug("customerClass",custClass);
// only if a class has been assigned at the customer level
if (custClass.custentityjlo_customer_class[0]) {
var custClassId = custClass.custentityjlo_customer_class[0].value;
log.debug("Customer Class",custClass.custentityjlo_customer_class[0].value);
}
// load fields from the sales order
var suiteQL = `
select distinct
iftl.transaction if_tran_id,
sotl.transaction so_tran_id,
sot.custbody_celigo_etail_channel,
sot.custbody_cen_shpfy_ordr_nts,
sot.custbodycustbody_shpfy_order_src
from PreviousTransactionLineLink ptll
,transactionline iftl
,transactionline sotl
,transaction sot
where
iftl.transaction = ?
and iftl.transaction = ptll.nextdoc
and iftl.id = ptll.nextline
and sotl.transaction = ptll.previousdoc
and sotl.id = ptll.previousline
and sotl.transaction = sot.id
`;
var results = query.runSuiteQL({
query: suiteQL,
params: [newRecord.id]
});
log.debug('results',results);
// check to make sure we returned rows
if (results.results.length == 0) {
throw new Error ("Results not found for transaction: " + newRecord.id);
}
var classValue = '';
var etailChannel = results.results[0].values[2];
var shopifyOrderNotes = results.results[0].values[3];
var shopifyOrderSource = results.results[0].values[4];
log.debug('etailChannel: ', etailChannel);
log.debug('shpifyOrderNotes: ', shopifyOrderNotes);
// determine the correct class to use based on the sales order transaction header attributes
//if there is a customer class, use it first
if (custClassId) {
log.debug("customer class");
classValue = custClassId;
// next, check for instagram. if instagram use it
} else if(shopifyOrderNotes != null && shopifyOrderNotes.indexOf("Instagram") >= 0) {
log.debug("instagram class");
classValue = instagramClass;
// next check for subcription: if eTail Channel = Shopify, and Shopify Order Source = 'subscription_contract'
} else if (etailChannel != null && etailChannel.toString() === shopifyChannel
&& shopifyOrderSource != null && shopifyOrderSource === 'subscription_contract') {
log.debug("continuity class");
classValue = continuityClass;
// otherwise if this is from the shopify channel, make the line Entry
} else if (etailChannel != null && etailChannel.toString() === shopifyChannel) {
log.debug("entry class");
classValue = entryClass;
}
log.debug('classValue: ', classValue);
// process the lines
var lineCount = newRecord.getLineCount({ sublistId: 'item' });
for (var i = 0; i < lineCount; i++) {
newRecord.setSublistValue({
sublistId: 'item',
fieldId: 'class',
line: i,
value: classValue
});
}
newRecord.save();
}
}
} catch (e) {
log.error(e,e.message);
}
}
// Register the beforeSubmit function
return {
afterSubmit: afterSubmitSetClass
};
});