我使用以下 JSON RESTlet 脚本来导出一些数据。由于限制,它的上限为 1000 行,远低于我需要导出的总数。我遇到过几种不同的解决方案,但 JSON/RESTlet 对我来说相当陌生,所以我正在寻找一些关于如何调整我的代码以循环遍历所有结果的反馈。
function GetSearchResult(){
//array container for search results
var output = new Array();
//get search results
var results = nlapiSearchRecord('transaction','customsearchid',null,null);
var columns = results[0].getAllColumns();
//loop through the search results
for(var i in results){
//create placeholder object place holder
var obj = new searchRow(
//set the values of the object with the values of the appropriate columns
results[i].getValue(columns[0]),
results[i].getValue(columns[1]),
results[i].getValue(columns[2]),
results[i].getValue(columns[3]),
results[i].getValue(columns[4]),
results[i].getValue(columns[5]),
results[i].getValue(columns[6]),
results[i].getValue(columns[7]),
results[i].getValue(columns[8]),
results[i].getValue(columns[9]),
results[i].getValue(columns[10]),
results[i].getValue(columns[11]),
results[i].getValue(columns[12])
);
//add the object to the array of results
output.push(obj);
}
//return the array of search objects
return output;
}
//Object to serve a place holder for each search row
function searchRow(internalid,lineid,subsidiaryid,locationid,departmentid,accountid,date,name,memo,amount,uniqueid,product,period){
this.internalid = internalid;
this.lineid = lineid;
this.subsidiaryid = subsidiaryid;
this.locationid = locationid;
this.departmentid = departmentid;
this.accountid = accountid;
this.date = date;
this.name = name;
this.memo = memo;
this.amount = amount;
this.uniqueid = uniqueid;
this.product = product;
this.period = period;
}
这是我试图遵循但无济于事的示例:
var types = ["Estimate","Opprtnty","SalesOrd","PurchOrd","CustInvc","CashSale"];
var filters = new Array(); //define filters of the search
filters[0] = new nlobjSearchFilter('type',null,'anyof',types);
filters[1] = new nlobjSearchFilter('mainline',null,'is','T');
var columns = new Array();
columns[0] = new nlobjSearchColumn('internalid').setSort(); //include internal id in the returned columns and sort for reference
var results = nlapiSearchRecord('transaction',null,filters,columns); //perform search
var completeResultSet = results; //container of the complete result set
while(results.length == 1000){ //re-run the search if limit has been reached
var lastId = results[999].getValue('internalid'); //note the last record retrieved
filters[2] = new nlobjSearchFilter('internalidnumber',null,'greaterthan',lastId); //create new filter to restrict the next search based on the last record returned
results = nlapiSearchRecord('transaction',null,filters,columns);
completeResultSet = completeResultSet.concat(results); //add the result to the complete result set
}
谢谢你的帮助!
最佳答案
这是同一脚本的 reSTLets 2.0 版本。将此添加到 Netsuite:
检查脚本是否已启用:选择“设置”>“公司”>“设置任务”>“启用功能”。单击 SuiteCloud 子选项卡。确保勾选客户端和服务器 SuiteScript。
文档。从左侧的文件夹中选择一个位置(例如 Suitescripts),然后单击“添加文件”。
编辑以下内容并将其保存为 mySavedSearch.js,然后从上面的站点上传。
自定义 > 脚本 > 脚本 > 新建。选择 mySavedSearch.js。填写名称和 ID 字段,保存。
点击“部署脚本”。将状态从“测试”更改为“已发布”,并适当设置权限。这将为您提供一个 URL 和一个外部 URL(类似于:https://[your-ID].reSTLets.api.netsuite.com/app/site/hosting/reSTLet.nl?script=1234&deploy=1)。
设置 Netsuite postman 集合后:(此处的操作方法:Where is the Netsuite Webservices Postman Collection?),它将被配置为发送正确的身份验证 header 。对您从上一步获得的 URL 执行获取请求,您将获得保存的搜索的 JSON。
/**
* @NApiVersion 2.x
* @NScriptType Restlet
* @NModuleScope SameAccount
*/
define(['N/search','N/record'],
function(search,record) {
/**
* Function called upon sending a GET request to the RESTlet.
*
* @param {Object} requestParams - Parameters from HTTP request URL; parameters will be passed into function as an Object (for all supported content types)
* @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
* @since 2015.1
*/
function doGet(requestParams) {
var results = [];
var slice = [];
var i = 0;
var mySearch = search.load({
id: 'customsearch12345' // change to the ID of your saved search
});
var resultSet = mySearch.run();
do {
slice = resultSet.getRange({ start: i, end: i + 1000 });
slice.forEach(function(row) {
var resultObj = {};
row.columns.forEach(function(column) {
resultObj[column.name] = row.getValue(column);
});
results.push(resultObj);
i++;
});
} while (slice.length >= 1000);
return JSON.stringify(results);
}
return {
'get': doGet,
};
});
https://stackoverflow.com/questions/43530943/