Netsuite 获取子列表行数据
如何从窗体的子列表中检索所有数据?也就是说,理想情况下,将子列表中的所有行检索为对象的Netsuite 获取子列表行数据,netsuite,suitescript,Netsuite,Suitescript,如何从窗体的子列表中检索所有数据?也就是说,理想情况下,将子列表中的所有行检索为对象的数组 /** * @NApiVersion 2.x * @NScriptType Suitelet * @NModuleScope SameAccount */ define(['N/ui/serverWidget', 'N/email', 'N/runtime', 'N/search', 'N/file', 'N
数组
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
define(['N/ui/serverWidget',
'N/email',
'N/runtime',
'N/search',
'N/file',
'N/log'],
/**
* @param {ui} ui
* @param {email} email
* @param {runtime} runtime
* @param {search} search
* @param {file} file
* @param {log} log
*/
function(ui, email, runtime, search, file, log) {
function onRequest(context) {
// On GET I create a form and add a sublist inline editor to it.
if (context.request.method === 'POST') {
var sublistData = context.request.parameters.sublistdata;
// sublistData is not an array its funny string. See below:
// 2017-5-16\u000111\u00012017-5-19\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u00011\u0001\u0001Me\u0001\u0001\u0001\u0001\u00012\u0001\u0001F\u0001\u0001\u0001INSERT\u00011\u0001F\u0001\u0001\u0001\u0001\u0001\u00022017-5-22\u000111122122\u00012017-5-12\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u00011\u0001\u0001Me\u0001\u0001\u0001\u0001\u00012\u0001\u0001F\u0001\u0001\u0001INSERT\u00011\u0001F\u0001\u0001\u0001\u0001\u0001
//How can I get the sublist row data in a better format?
}
}
return {
onRequest: onRequest
};
});
所发生的情况是,NetSuite通过不可打印的Unicode控制字符
\u0001
(字段之间)和\u0002
(行之间)来分隔请求对象中的子列表值
您可以使用request.getLineCount()
和request.getSublistValue()
检索结果
var lines = context.request.getLineCount({ group: "sublist" });
for(var i = 0; i < lines; i++) {
var field1 = context.request.getSublistValue({ group: 'sublist', name: 'field1', line: i });
var field2 = context.request.getSublistValue({ group: 'sublist', name: 'field2', line: i });
}
var lines=context.request.getLineCount({group:“sublist”});
对于(变量i=0;i
既然SuiteScript2.1已进入测试版并支持ES6,您可以按如下方式填充整个记录。首先,在脚本中使用NApiVersion 2.1
* @NApiVersion 2.1
然后确保您需要N/record模块
define(['N/record'], function (record) {
// ... your code in here ...
}
然后在代码的某个地方,定义这个函数
function buildRecordData() {
// load the record you want
const rec = r.load({
id: '1234', // your record's internal id here
type: 'salesorder' // your record's type here
})
let data = {}
const sublistIds = rec.getSublists()
// loop through all sublists for this record type
for (let sublistId of sublistIds) {
// add a property to the object for each sublistId
data[sublistId] = []
// get the columns of the sublist
let sublistFields = rec.getSublistFields({ sublistId })
let count = rec.getLineCount({ sublistId })
// loop through the lines of the sublist and build an object for each
for(let line = 0; line < count; line++) {
let x = {}
for (let fieldId of sublistFields) {
x[fieldId] = rec.getSublistValue({ sublistId, fieldId, line })
}
data[sublistId].push(x)
}
}
return data
}
函数buildRecordData(){
//加载所需的记录
const rec=r.load({
id:'1234',//此处是记录的内部id
键入:“salesorder”//此处记录的类型
})
让数据={}
const sublistIds=rec.getSublists()
//循环遍历此记录类型的所有子列表
for(让子列表中的子列表){
//为每个子列表ID向对象添加属性
数据[子列表]=[]
//获取子列表的列
让sublistFields=rec.getSublistFields({sublistId})
let count=rec.getLineCount({sublistId})
//循环遍历子列表的行,并为每个行构建一个对象
for(让line=0;line
当您调用此脚本时,它将为您提供一个对象。返回对象的属性对应于记录的子列表。每个子列表是一个对象数组,数组的每个元素表示子列表的一行
显然,这可能有点过头了。如果您只需要一个子列表并且知道它的id,或者如果您只需要行中的几个字段并且知道它们的id。但您可以修改此代码以返回较小的数据集