Skip to content

Instantly share code, notes, and snippets.

@cafeasp
Created July 24, 2019 10:11
Show Gist options
  • Save cafeasp/799b623af48a9b57ece9886f59fa4596 to your computer and use it in GitHub Desktop.
Save cafeasp/799b623af48a9b57ece9886f59fa4596 to your computer and use it in GitHub Desktop.
NetSuite-Suitelet Suite Script Close Sales Order
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
define(['N/file', 'N/record', 'N/redirect', 'N/search', 'N/ui/serverWidget', 'N/runtime', 'N/email'],
/**
* @param {file} file
* @param {record} record
* @param {redirect} redirect
* @param {search} search
* @param {serverWidget} serverWidget
*/
function (file, record, redirect, search, serverWidget, runtime, email) {
/**
* Definition of the Suitelet script trigger point.
*
* @param {Object} context
* @param {ServerRequest} context.request - Encapsulation of the incoming request
* @param {ServerResponse} context.response - Encapsulation of the Suitelet response
* @Since 2015.2
*/
function onRequest(context) {
if (context.request.method === 'GET') {
//build form
var form = buildForm(serverWidget);
context.response.writePage(form);
} else {
//process
var currentScript = runtime.getCurrentScript();
var params = {};
var oc_area_list = context.request.parameters.custpage_oc_area_list;
var oc_array = oc_area_list.split('\r\n');
var fileTimeStamp = new Date();
var csvFile = file.create({
name: fileTimeStamp.getTime() + '.csv',
contents: 'process,status\n',
folder: 123,
fileType: 'CSV'
});
oc_array.forEach(function (row) {
var searchResult = getSalesOrderInternalId(row);
var ocInternalId = searchResult.getValue({
name: 'internalid'
});
log.debug('OC_Number and id ', row + ' ' + ocInternalId);
if (closeOrder(ocInternalId, record) == true) {
csvFile.appendLine({
value: row + ',closed\n'
});
} else {
csvFile.appendLine({
value: row + ',fail\n'
});
}
});
var saveCSV = csvFile.save();
var f = file.load({ id: saveCSV });
email.send({
author: 123,
recipients: runtime.getCurrentUser().id,
subject: 'Close order result file',
body: 'See attach.',
attachments: [f]
});
//log.debug('csv log', saveCSV);
params['csv'] = saveCSV;
// redirect.toSuitelet({
// scriptId: currentScript.id,
// deploymentId: currentScript.deploymentId,
// parameters: params
// });
redirect.toSuitelet({
scriptId: 'customscript123456',
deploymentId: 'customdeploy1',
parameters: params
});
return;
}
}
function closeOrder(ocInternalId, record) {
try {
var order = record.load({
id: ocInternalId,
type: record.Type.SALES_ORDER,
isDynamic: true
});
//log.debug('order', order);
var itemIndex = 0;
var itemCount = order.getLineCount({
"sublistId": "item"
});
log.debug('item count', itemCount);
while (itemIndex < itemCount) {
order.selectLine({
"sublistId": "item",
"line": itemIndex
});
order.setCurrentSublistValue({
"sublistId": "item",
"fieldId": "isclosed",
"value": true
});
order.commitLine({
"sublistId": "item"
});
itemIndex++;
}
var recordId = order.save({
enableSourcing: false,
ignoreMandatoryFields: false
});
log.debug('updated oc', recordId);
return true;
}
catch (error) {
return false;
}
}
function getSalesOrderInternalId(oc_number) {
var s = search.create({
'type': 'salesorder',
'filters': [
search.createFilter({
name: 'tranid',
operator: search.Operator.IS,
values: [oc_number]
})
],
'columns': ['internalid']
}).run().getRange({
start: 0,
end: 1
});
//log.debug('result set', s);
return s[0];
}
function buildForm(formWidget) {
var form = formWidget.createForm({
title: 'Close Order Confirmation'
});
var textArea = form.addField({
id: 'custpage_oc_area_list',
type: serverWidget.FieldType.TEXTAREA,
label: 'Sales order list, one per row.'
});
textArea.isMandatory = true;
form.addSubmitButton({
label: 'Process'
});
return form;
}
return {
onRequest: onRequest
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment