-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexposeImageNames.js
130 lines (130 loc) · 7.2 KB
/
exposeImageNames.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
///<reference path='c:\development\Netsuite\SuiteScriptAPITS.d.ts'/>
var kotnShadowImageNames;
(function (kotnShadowImageNames) {
/**
* copy standard store images allow image names in feeds
* @param {string} type event type
* @return {void}
*/
function itemBeforeSubmit(type) {
if (type == 'edit' || type == 'create') {
var imageName = nlapiGetFieldText('storedisplayimage');
if (imageName && imageName != nlapiGetFieldValue('custitem_kotn_image_name'))
nlapiSetFieldValue('custitem_kotn_image_name', imageName);
var thumbnailName = nlapiGetFieldText('storedisplaythumbnail');
if (thumbnailName && thumbnailName != nlapiGetFieldValue('custitem_kotn_thumbnail_name'))
nlapiSetFieldValue('custitem_kotn_thumbnail_name', thumbnailName);
}
}
kotnShadowImageNames.itemBeforeSubmit = itemBeforeSubmit;
function itemAfterSubmit(type) {
nlapiLogExecution('DEBUG', type + ' after submit with matrix type: ' + nlapiGetFieldValue('matrixtype'));
if (type == 'edit' || type == 'create' || type == 'xedit') {
if (!nlapiGetContext().getFeature('MATRIXITEMS'))
return;
try {
var oldRec = nlapiGetOldRecord();
var imageName = nlapiGetFieldValue('custitem_kotn_image_name');
if (imageName && oldRec && oldRec.getFieldValue('custitem_kotn_image_name') == imageName)
imageName = null;
var thumbnailName = nlapiGetFieldValue('custitem_kotn_thumbnail_name');
if (thumbnailName && oldRec && oldRec.getFieldValue('custitem_kotn_thumbnail_name') == thumbnailName)
thumbnailName = null;
if (nlapiGetRecordId() && (imageName || thumbnailName)) {
var childItems = nlapiSearchRecord('item', null, [
new nlobjSearchFilter('parent', null, 'anyof', [nlapiGetRecordId()]),
new nlobjSearchFilter('matrixchild', null, 'is', 'T'),
new nlobjSearchFilter('isinactive', null, 'is', 'F')
], [
new nlobjSearchColumn('custitem_kotn_image_name'),
new nlobjSearchColumn('custitem_kotn_thumbnail_name')
]);
if (childItems) {
var updateFields = [], updateValues = [];
if (imageName) {
updateFields.push('custitem_kotn_image_name');
updateValues.push(imageName);
}
if (thumbnailName) {
updateFields.push('custitem_kotn_thumbnail_name');
updateValues.push(thumbnailName);
}
nlapiLogExecution("DEBUG", "Updating " + childItems.length + " for image names");
childItems.forEach(function (c) {
if (c.getValue('custitem_kotn_image_name') == imageName && c.getValue('custitem_kotn_thumbnail_name') == thumbnailName)
return;
nlapiSubmitField(c.getRecordType(), c.getId(), updateFields, updateValues, { disabletriggers: true, enablesourcing: false });
});
}
}
}
catch (e) {
nlapiLogExecution('ERROR', 'updating child items', (e.message || e.toString()) + (e.getStackTrace ? (' \n \n' + e.getStackTrace().join(' \n')) : ''));
}
}
}
kotnShadowImageNames.itemAfterSubmit = itemAfterSubmit;
/**
* prefill these fields after install. You'll need a deployment
* for each type of item. e.g., Inventory Item, Non-Inventory Item, Serialized Item
* @param {string} recType Record type supplied by the mass update runner
* @param {string|number} recId Item internal id supplied by the mass update runner
*/
function massImages(recType, recId) {
var item = nlapiLoadRecord(recType, recId);
var needSave = false;
var imageName = item.getFieldText('storedisplayimage') || '';
if (imageName && imageName != item.getFieldValue('custitem_kotn_image_name')) {
item.setFieldValue('custitem_kotn_image_name', imageName);
needSave = true;
}
var thumbnailName = item.getFieldText('storedisplaythumbnail');
if (thumbnailName && thumbnailName != item.getFieldValue('custitem_kotn_thumbnail_name')) {
item.setFieldValue('custitem_kotn_thumbnail_name', thumbnailName);
needSave = true;
}
if (needSave) {
nlapiSubmitRecord(item, { disabletriggers: true, ignoremandatoryfields: true, enablesourcing: false });
}
if (!nlapiGetContext().getFeature('MATRIXITEMS'))
return;
var filters = [
new nlobjSearchFilter('parent', null, 'anyof', [recId]),
new nlobjSearchFilter('matrixchild', null, 'is', 'T'),
new nlobjSearchFilter('isinactive', null, 'is', 'F')
];
// var imageClause = imageName ? "when '"+ imageName +"' != {custitem_kotn_image_name} then 1 " : '';
// var thumbClause = thumbnailName ? "when '"+ thumbnailName +"' != {custitem_kotn_thumbnail_name} then 1 " : '';
// if(imageClause || thumbClause){
// filters.push(new nlobjSearchFilter('formulanumeric', null, 'equalto', '1').
// setFormula("case "+ imageClause +" "+ thumbClause +" else 0 end"));
//
// nlapiLogExecution('DEBUG', 'using formula to find children to update', filters.slice(-1)[0].getFormula());
// }
var childItems = nlapiSearchRecord('item', null, filters, [new nlobjSearchColumn('custitem_kotn_image_name'), new nlobjSearchColumn('custitem_kotn_thumbnail_name')]);
if (childItems) {
var updateFields = [], updateValues = [];
if (imageName) {
updateFields.push('custitem_kotn_image_name');
updateValues.push(imageName);
}
if (thumbnailName) {
updateFields.push('custitem_kotn_thumbnail_name');
updateValues.push(thumbnailName);
}
nlapiLogExecution('DEBUG', 'applying image update to ' + childItems.length, JSON.stringify(updateFields) + '\n\twith\n' + JSON.stringify(updateValues));
try {
childItems.forEach(function (c) {
if (c.getValue('custitem_kotn_image_name') == imageName && c.getValue('custitem_kotn_thumbnail_name') == thumbnailName)
return;
nlapiSubmitField(c.getRecordType(), c.getId(), updateFields, updateValues, { disabletriggers: true, enablesourcing: false });
});
}
catch (e) {
// probably governance.
nlapiLogExecution('ERROR', 'updating child items', (e.message || e.toString()) + (e.getStackTrace ? (' \n \n' + e.getStackTrace().join(' \n')) : ''));
}
}
}
kotnShadowImageNames.massImages = massImages;
})(kotnShadowImageNames || (kotnShadowImageNames = {}));