近期在做服务支持的过程中,发现NetSuite终端用户在处理数据时,费了很多功夫,做重复的数据处理工作。是可忍,孰不可忍?减少浪费,是我们NetSuite知识会的基本理念。文后附直播课录像链接,来专门谈谈这个话题。
我们就从数据批量导出、批量更改、批量删除,三个典型业务场景来做个探讨。
1.批量导出
近期的一个典型业务场景是,用户希望将一个工单中200行物料一键导出。如何处理?
2.批量更改
CSV批量导入可以实现记录的批量更改。有没有更简单的方法,来达成批量更改的目的?答案是Mass Update!
3.批量删除
在需要清理环境时,我们可以通过脚本来做记录清除,但是相当的危险。作为终端用户是不可能触及的。但是如果需要批量删除时,如何做到?
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
define(['N/ui/serverWidget', 'N/search', 'N/url', 'N/record'],
function(serverWidget, search, url, record) {
/**
* 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) {
var form = serverWidget.createForm({
title : 'Simple Form'
});
if(context.request.method == 'GET'){
var mySearch = search.load({
type: search.Type.TRANSACTION,
id: 'customsearch_mass_deletion_results'
});
var mySearchRS = mySearch.run();
var results = mySearchRS.getRange({
start: 0,
end: 1000
});
var sublist = form.addSublist({
id : 'custpage_transaction_list',
type : serverWidget.SublistType.LIST,
label : 'Inline Editor Sublist'
});
var transactionArray = new Array();
if (results != null) {
sublist.addField({
id: 'delete',
type: serverWidget.FieldType.CHECKBOX,
label: 'Delete'
});
// Add hidden columns for the Internal ID and for the Record type.
// These fields are necessary for the nlapiDeleteRecord function.
sublist.addField({
id: 'internalid',
type: serverWidget.FieldType.TEXT,
label: 'InternalID'
}).updateDisplayType({
displayType : serverWidget.FieldDisplayType.HIDDEN
});
sublist.addField({
id: 'recordtype',
type: serverWidget.FieldType.TEXT,
label: 'Record Type'
}).updateDisplayType({
displayType : serverWidget.FieldDisplayType.HIDDEN
});
// Add a column for the Internal ID link
sublist.addField({
id: 'internalidlink',
type: serverWidget.FieldType.TEXT,
label: 'Internal ID'
});
// Get the the search result columns
var columns = results[0].columns;
// Add the search columns to the sublist
for (var i = 0; i < columns.length; i++) {
sublist.addField({
id: columns[i].name,
type: serverWidget.FieldType.TEXT,
label: columns[i].label
});
}
for (var i = 0; i < results.length; i++) {
var transaction = new Object();
// Set the Delete column to False
sublist.setSublistValue({
id: 'delete',
line: i,
value: 'F'
});
// Set the hidden internal ID field
sublist.setSublistValue({
id: 'internalid',
line: i,
value: results[i].id
});
// Set the hidden record type field
sublist.setSublistValue({
id: 'recordtype',
line: i,
value: results[i].recordType
});
// Create a link so users can navigate from the list of transactions to a specific transaction
var recUrl = url.resolveRecord({
recordType: results[i].recordType,
recordId: results[i].id,
isEditMode: false
});
internalIdLink = " " + results[i].id + " ";
// Set the link
sublist.setSublistValue({
id: 'internalidlink',
line: i,
value: internalIdLink
});
// set the row values to the transaction object
try{
for (var j = 0; j < columns.length; j++) {
sublist.setSublistValue({
id: columns[j].name,
line: i,
value: results[i].getValue(columns[j].name)
});
}}catch(ex){
log.audit('error', ex);
}
}
}
sublist.addMarkAllButtons();
form.addSubmitButton({
label: 'Submit'
});
context.response.writePage({
pageObject: form
});
}else{
// Check how many lines in the sublist
var count = context.request.getLineCount({
group: 'custpage_transaction_list'
});
log.debug('count',count);
// This variable will keep track of how many records are deleted.
var num = 0;//for each line in the sublist
log.debug('num',num);
for (var i = 0; i < count; i++) {
//get the value of the Delete checkbox
var deleteTransaction = context.request.getSublistValue({
group: 'custpage_transaction_list',
name: 'delete',
line: i
});
log.debug('deleteTransaction',deleteTransaction);
// If it's checked, delete the transaction
if (deleteTransaction == 'T') {
// Get the transaction internal ID
var internalId = context.request.getSublistValue({
group: 'custpage_transaction_list',
name: 'internalid',
line: i
});
log.debug('internalId',internalId);
// Get the transaction type
var recordType = context.request.getSublistValue({
group: 'custpage_transaction_list',
name: 'recordtype',
line: i
});
log.debug('recordType',recordType);
try {
// Delete the transaction
var salesOrderRecord = record.delete({
type: recordType,
id: internalId,
});
log.debug('salesOrderRecord',salesOrderRecord);
num++;
}
// Errors will be logged in the Execution Log
catch (ex) {
log.audit('Error', 'Transaction ID ' + internalId + ': ' + ex);
}
}
}
// Show how many records were deleted.
form.addField({
id : 'custpage_transaction_total',
type : serverWidget.FieldType.TEXT,
label : 'transactions deleted'
}).updateDisplayType({
displayType : serverWidget.FieldDisplayType.INLINE
}).defaultValue = num + " ";
context.response.writePage(form);
}
}
return {
onRequest: onRequest
};
});
NetSuite知识会第1谈-数据批量处理
如果有任何关于NetSuite的问题,欢迎来谈。邮箱:service@truston.group