-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_suitelet_assmbly_overflow.js
123 lines (123 loc) · 5.26 KB
/
spec_suitelet_assmbly_overflow.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
define(['N/record', 'N/search', 'N/runtime', 'N/redirect'], function(record, search, runtime, redirect){
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @appliedtorecord lotnumberedassemblyitem
*/
/**
* <code>onRequest</code> event handler
* @gov 0
*
* @param request
* {Object}
* @param response
* {String}
*
* @return {void}
*
* @static
* @function onRequest
*/
function onRequest(context) {
var i = parseInt(context.request.parameters.index); //gets parameters from assembly suitelet
var searchAssembliesString = context.request.parameters.searchAssembliesString;
const searchAssemblies = JSON.parse(searchAssembliesString);
i = i + 1;
var overflow = false;
for (index = i; index < searchAssemblies.length; index++){ //executes for all bom assemblies, starting after the last one that was used
var assemblySearch = search.load({ //finds and loads assembly record
id: 'customsearchbom_assembly'
});
var idFilter = search.createFilter({
name: 'internalid',
operator: search.Operator.IS,
values: [searchAssemblies[index]]
});
assemblySearch.filters.push(idFilter);
var isLotItem = true;
assemblySearch.run().each(function(result){
isLotItem = result.getValue({
name: "islotitem"
});
});
if(isLotItem == true || isLotItem == 'T' || isLotItem == "true" || isLotItem == "Yes" || isLotItem == "yes"){ //loads with correct record type
var assemblySearchRecord = record.load({
type: record.Type.LOT_NUMBERED_ASSEMBLY_ITEM,
id: searchAssemblies[index]
});
}
else{
var assemblySearchRecord = record.load({
type: record.Type.ASSEMBLY_ITEM,
id: searchAssemblies[index]
});
}
bom_id = assemblySearchRecord.getSublistValue({ //gets bom data from assembly; used for bom search
sublistId: 'billofmaterials',
fieldId: 'billofmaterials',
line: 0
});
bom_name = assemblySearchRecord.getSublistValue({
sublistId: 'billofmaterials',
fieldId: 'currentrevision',
line: 0
});
if(bom_id != '' && bom_name != '' && bom_id != null && bom_name != null){
var bomSearch = search.load({ //searches for components of bom that are also assemblies
id: 'customsearchbom_only_wip'
});
var bomFilter = search.createFilter({
name: 'billofmaterials',
operator: search.Operator.IS,
values: [bom_id]
});
var nameFilter = search.createFilter({
name: 'name',
operator: search.Operator.IS,
values: [bom_name]
});
bomSearch.filters.push(bomFilter);
bomSearch.filters.push(nameFilter);
bomSearch.run().each(function(result){
if(result != null && result != ''){
var assembly_id = result.getValue({ //adds the each assembly's internal id to array
name: "item",
join: "component"
});
searchAssemblies.push(assembly_id);
}
return true;
});
var scriptObj = runtime.getCurrentScript(); //checks usage to see if there is enough to keep looping
var unitsRemaining = scriptObj.getRemainingUsage();
if (unitsRemaining < 100){
overflow = true;
break; //breaks from loop and calls overflow suitelet if not enough usage left
}
}
}
var searchAssembliesString = JSON.stringify(searchAssemblies);
if (overflow == true){ //executes if overflow assembly suitelet needed
redirect.toSuitelet({ //calls overflow suitelet with array and current index as parameters
scriptId: 'customscriptspec_suitelet_overflow',
deploymentId: 'customdeployspec_suitelet_overflow',
parameters: {
'searchAssembliesString': searchAssembliesString,
'index': index
}
});
}
else{ //executes if overflow assembly suitelet not needed
redirect.toSuitelet({ //calls bom suitelet with array as parameter
scriptId: 'customscriptspec_suitelet_boms',
deploymentId: 'customdeployspec_suitelet_boms',
parameters: {
'searchAssembliesString': searchAssembliesString
}
});
}
}
return {
onRequest: onRequest
}
});