-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagreements.ts
305 lines (280 loc) · 7.52 KB
/
agreements.ts
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
import { EntryPoints } from 'N/types';
import * as search from 'N/search';
import * as file from 'N/file';
import * as record from 'N/record';
import * as serverWidget from 'N/ui/serverWidget';
import * as message from 'N/ui/message';
import { ServerRequest, ServerResponse } from 'N/https';
/**
* A Suitelet to attach MAP Agreements to customer records.
*/
export const onRequest: EntryPoints.Suitelet.onRequest = (
context: EntryPoints.Suitelet.onRequestContext
) => {
const request = context.request;
const response = context.response;
if (request.method === 'GET') {
onGet(response);
} else {
onPost(request, response);
}
};
/**
* Handles Get Request and loads the saved search
*/
const onGet = (response: ServerResponse) => {
const results = getFiles();
const page = createPage(results);
response.writePage(page);
};
/**
* Handles the Post Request
*/
const onPost = (request: ServerRequest, response: ServerResponse) => {
const folderID = 753;
const files = request.parameters.custpage_files;
const customerID = request.parameters.custpage_customer;
const fileIDs = files.split(',');
let body =
'<p>Moving file(s): ' +
files +
' and attaching to customer: ' +
customerID +
'</p>';
fileIDs.forEach((fileID: string) => {
// load and change dir
const fileObj = file.load({ id: Number(fileID) });
body += '<p>Moving file: ' + fileID + '</p>';
fileObj.folder = Number(folderID);
fileObj.save();
// attach to customer
body += '<p>Attaching file: ' + fileID + ' to ' + customerID + '</p>';
record.attach({
record: {
type: 'file',
id: Number(fileID),
},
to: {
type: 'customer',
id: Number(customerID),
},
});
});
body += '<p>DONE!</p>';
// updated
const form = serverWidget.createForm({ title: 'MAP Agremeents' });
form.addPageInitMessage({
type: message.Type.CONFIRMATION,
title: 'SUCCESS!',
message:
'File(s): ' +
files +
' have been attached to customer record (' +
customerID +
')',
});
form.addField({
id: 'custpage_main_body',
type: serverWidget.FieldType.INLINEHTML,
label: ' ',
}).defaultValue = body;
form.addPageLink({
type: serverWidget.FormPageLinkType.CROSSLINK,
title: 'Go Back',
url: '/app/site/hosting/scriptlet.nl?script=1062&deploy=1',
});
form.addPageLink({
type: serverWidget.FormPageLinkType.CROSSLINK,
title: 'Generate MAP Agreement',
url: '/app/site/hosting/scriptlet.nl?script=1065&deploy=1',
});
response.writePage(form);
};
const getFiles = () => {
const folder = 32814;
const fileSearch = search.create({
type: 'folder',
columns: [
search.createColumn({
name: 'internalid',
}),
search.createColumn({
name: 'name',
join: 'file',
}),
search.createColumn({
name: 'internalid',
join: 'file',
}),
search.createColumn({
name: 'url',
join: 'file',
}),
],
filters: [
search.createFilter({
name: 'internalid',
operator: search.Operator.IS,
values: folder,
}),
],
});
const pagedData = fileSearch.runPaged({
pageSize: 100,
});
const fileResults = [];
pagedData.pageRanges.forEach(function (pageRange) {
var page = pagedData.fetch({ index: pageRange.index });
page.data.forEach(function (result) {
if (result.getValue({ name: 'internalid', join: 'file' })) {
fileResults.push({
id: result.getValue({ name: 'internalid' }),
fileid: result.getValue({ name: 'internalid', join: 'file' }),
filename: result.getValue({ name: 'name', join: 'file' }),
url: result.getValue({ name: 'url', join: 'file' }),
});
}
});
});
if (fileResults.length > 0) {
return fileResults;
} else {
return false;
}
};
const createPage = (
results:
| {
id: string;
fileid: string;
filename: string;
url: string;
}[]
| false
) => {
const form = serverWidget.createForm({ title: 'MAP Agreements' });
form.addPageLink({
type: serverWidget.FormPageLinkType.CROSSLINK,
title: 'Generate MAP Agreement',
url: '/app/site/hosting/scriptlet.nl?script=1065&deploy=1',
});
if (results) {
form.clientScriptModulePath = 'SuiteScripts/agreements_client.js';
form.addSubmitButton({
label: 'Attach',
});
form
.addField({
id: 'custpage_message',
type: serverWidget.FieldType.INLINEHTML,
label: ' ',
})
.updateLayoutType({
layoutType: serverWidget.FieldLayoutType.OUTSIDEABOVE,
})
.updateBreakType({
breakType: serverWidget.FieldBreakType.STARTROW,
}).defaultValue =
'Please add the customers internal id number below and select the file(s) to attach.';
form
.addField({
id: 'custpage_customer',
label: 'Customer Internal ID',
type: serverWidget.FieldType.TEXT,
})
.updateLayoutType({
layoutType: serverWidget.FieldLayoutType.OUTSIDEABOVE,
})
.updateBreakType({
breakType: serverWidget.FieldBreakType.STARTROW,
})
.setHelpText({
help: 'You can grab the customers internal id by going to the customer record and getting the id from the url. (ex: id=1102)',
});
form
.addField({
id: 'custpage_files',
label: 'Files to Move & Attach',
type: serverWidget.FieldType.TEXT,
})
.updateDisplayType({
displayType: serverWidget.FieldDisplayType.HIDDEN,
});
form
.addField({
id: 'custpage_customer_found',
label: 'Customer Found',
type: serverWidget.FieldType.CHECKBOX,
})
.updateDisplayType({
displayType: serverWidget.FieldDisplayType.HIDDEN,
});
const sublist = form.addSublist({
id: 'custpage_agreements_sublist',
type: serverWidget.SublistType.LIST,
label: 'Sublist',
});
sublist.addField({
id: 'custpage_result_checkbox',
type: 'checkbox',
label: 'Select',
});
sublist.addField({
id: 'custpage_result_folderid',
type: 'text',
label: 'Folder ID',
});
// }).updateDisplayType({
// displayType: serverWidget.FieldDisplayType.ENTRY
// });
sublist.addField({
id: 'custpage_result_fileid',
type: 'text',
label: 'File ID',
});
sublist.addField({
id: 'custpage_result_filename',
type: 'text',
label: 'Filename',
});
sublist.addField({
id: 'custpage_result_url',
type: 'text',
label: 'Preview',
});
results.forEach(function (result, index) {
sublist.setSublistValue({
id: 'custpage_result_folderid',
line: index,
value: result.id,
});
sublist.setSublistValue({
id: 'custpage_result_fileid',
line: index,
value: result.fileid,
});
sublist.setSublistValue({
id: 'custpage_result_filename',
line: index,
value: result.filename,
});
sublist.setSublistValue({
id: 'custpage_result_url',
line: index,
value: '<a href="' + result.url + '" target="_blank">Preview</a>',
});
});
} else {
form.addField({
id: 'custpage_message',
type: serverWidget.FieldType.INLINEHTML,
label: ' ',
}).defaultValue = 'There are currently no MAP Agreements to attach.';
}
return form;
};