简体   繁体   English

如何获取 NetSuite 上的所有项目列表?

[英]How to get all items list on NetSuite?

I am just starting with NetSuite and trying to pull all items with details using Restlet.我刚开始使用 NetSuite,并尝试使用 Restlet 提取所有包含详细信息的项目。 With some research, I am able to pull all the items but the way I am doing now is not straightforward.通过一些研究,我能够拉出所有项目,但我现在做的方式并不简单。 I first pull the all ids of item using nlapiSearchRecord and loop through each id to get details of each item using nlapiLoadRecord and added to array.我首先使用 nlapiSearchRecord 提取项目的所有 id 并循环遍历每个 id 以使用 nlapiLoadRecord 获取每个项目的详细信息并添加到数组中。 This way, it is taking to much time.这样,它需要很多时间。 Is there other way to pull all items with their details?有没有其他方法可以拉出所有项目的详细信息? Below is my code.下面是我的代码。

function getAllIDs() {
    return nlapiSearchRecord('item', null, null, null);
 }

function getRecord() {
    var all_IDs = getAllIDs();
    var len=all_IDs.length;
    var result =new Array();

  for(var i=0;i<all_IDs.length;i++) {
    if(all_IDs[i].getRecordType()==="inventoryitem")
        result[i]=nlapiLoadRecord(all_IDs[i].getRecordType(),all_IDs[i].id)
    }
    return result;
}

You can use what @Krypton suggested but you will always get 1000 results at max.您可以使用@Krypton 建议的内容,但最多始终会获得 1000 个结果。

Try following if you have requirement to get more than 1000 (using Suitescript 2.0):如果您需要获得超过 1000 个(使用 Suitescript 2.0),请尝试以下操作:

    var columns = [];
    var filters = [['isinactive', 'is', 'F']];
    columns.push(search.createColumn({ name: "itemid"}));
    columns.push(search.createColumn({ name: "displayname"}));
    columns.push(search.createColumn({ name: "salesdescription"}));
    columns.push(search.createColumn({ name: "baseprice"}));
    var inventoryitemSearch = search.create({
        type: search.Type.INVENTORY_ITEM, //Change the type as per your requirement
        filters: filters,
        columns: columns
    });
    var arrResults = [];
    var count = 1000;
    var startIndex = 0;
    var endIndex = 1000;
    var resultSet= inventoryitemSearch.run();
    while (count == 1000) {
        var results = resultSet.getRange(startIndex, endIndex);
        arrResults = arrResults.concat(results);
        startIndex = endIndex;
        endIndex += 1000;
        count = results.length;
    }
    log.debug({title: 'arrResults ', details: arrResults });

You can include the details you want in the search.您可以在搜索中包含所需的详细信息。 So, for example, you can include an nlobjSearchFilter so that the search only returns inventory items, and add an nlobjSearchColumn for each field you want to see in the details.因此,例如,您可以包含一个nlobjSearchFilter以便搜索仅返回库存项目,并为您希望在详细信息中查看的每个字段添加一个nlobjSearchColumn This way all the details you want to see are returned with the search and you can loop through the results to do what you want with them without loading every record individually - which will be where most of the performance hit is happening.通过这种方式,您想查看的所有详细信息都将与搜索一起返回,您可以遍历结果以使用它们执行您想要的操作,而无需单独加载每条记录 - 这将是大部分性能损失发生的地方。

An example:一个例子:

var inventoryitemSearch = nlapiSearchRecord("inventoryitem",null,
[                                  
   ["type","anyof","InvtPart"]
], 
[
   new nlobjSearchColumn("itemid",null,null).setSort(false), 
   new nlobjSearchColumn("displayname",null,null), 
   new nlobjSearchColumn("salesdescription",null,null), 
   new nlobjSearchColumn("baseprice",null,null)
]
);

Then you can loop through the results to get details:然后您可以遍历结果以获取详细信息:

var name, displayName, description, price;
for ( var i = 0; inventoryitemSearch != null && i < searchresults.length; i++ ) {
    var searchresult = inventoryitemSearch[ i ];
    name = searchresult.getValue( 'itemid' ); 
    displayName = searchresult.getValue( 'displayname' ); 
    description = searchresult.getValue( 'salesdescription' ); 
    price = searchresult.getValue( 'baseprice' ); 
}

There is a lot to learn about scripted searches in NetSuite, so I'd recommend starting here (NetSuite login required) and follow the links and keep reading / experimenting until your eyes glaze over.关于 NetSuite 中的脚本搜索,有很多东西需要学习,所以我建议从这里开始(需要 NetSuite 登录)并按照链接继续阅读/试验,直到你的眼睛呆滞为止。

I just like to use a generic function that accepts a search object...我只是喜欢使用接受搜索对象的通用函数......

const getAllResults = searchObj => {
try {
  const Resultset = searchObj.run()
  const maxResults = searchObj.runPaged().count
  let ResultSubSet = null
  let index = 0
  const maxSearchReturn = 1000
  let AllSearchResults = []

  do {
    let start = index
    let end = index + maxSearchReturn
    if (maxResults && maxResults <= end) {
      end = maxResults
    }
    ResultSubSet = Resultset.getRange(start, end)

    if (ResultSubSet.length === 0) {
      break
    }
    // we could intriduce a record processor to lighetn up the load
    AllSearchResults = AllSearchResults.concat(ResultSubSet)
    index = index + ResultSubSet.length

    if (maxResults && maxResults == index) {
      break
    }
  } while (ResultSubSet.length >= maxSearchReturn)

  return AllSearchResults

  } catch (e) {
    log.error(`getAllResults()`, `error : ${e}`)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM