user2795475
user2795475

Reputation: 55

Search not working on item record

I am not able to get the list of item.I am using a saved search and want to create a list of all the item record id from it. But it is not working.

my code

var loadSearch=nlapiLoadSearch('item','customsearch12');
var getData = loadSearch.runSearch();
var itemCol=new Array();
for(var i=0;i<getData.length;i++)
{
    itemCol.push(getData[i].getId());
}

Can somebody help me with this

Upvotes: 3

Views: 3580

Answers (4)

Uma Kanth
Uma Kanth

Reputation: 5629

You could get all the results one by one using a callback function till all the records are iterated.

var search = nlapiLoadSearch('item','customsearch12');
var resultSet = loadSearch.runSearch();
var itemCol=new Array();
resultSet.forEachResult(function (iterator) {

    itemCol.push(iterator.getId());
    return true;
});

or you could load few records of it using the getResults of the nlobjResultSet

    var search = nlapiLoadSearch('item', 'customsearch12');
    var resultSet = search.runSearch();
    var results = resultSet.getResults(0, 100);//get the results from 0 to 100 search results
    var itemCol = new Array();
    for(var i in results)
    {
    itemCol.push(results[i].getValue('id'));
    }

Upvotes: 0

Rha7
Rha7

Reputation: 103

Here's a sample of how to fetch over 1k of results in NetSuite/NetScript.

The search "customsearch_tel_tg_by_trunk_type" is not really relevant.

Complete sample of 1k+ results fetch in NetSuite/NetScript

Upvotes: 0

Amit
Amit

Reputation: 2565

The answer given above by Nitish is perfectly correct but you need to consider one more thing.

nlapiLoadSearch api returns 4000 records at a time.

The nlapiSearchRecord api returns only 1000 records at a time , so what if your saved search consists of more than 1000 records.

So you will miss those extra items.

so here is the code to have better results

  //Searching the items
    var searchResults = nlapiSearchRecord('item', 'customsearch12',null,null);
    var searchedItemId;
    var lastId;
    var completeResults = new Array();
    var arrNewFilters=[];

    if (searchResults != null) {
        completeResults = completeResults.concat(searchResults);
    }

    else {
        completeResults = null;
        nlapiLogExecution('Debug', 'No item found', 
            weight + ' Null result');
    }

    if (completeResults != null) {

        if (searchResults.length == 1000) {

            while (searchResults.length == 1000) {

                //Initialize variable
                lastId = "";
                //Get Record Id of Last record, 
                 //to search the item record again from that record
                lastId = searchResults[999].getId();

                //start after the last id searched
                arrNewFilters.push(new nlobjSearchFilter("internalidnumber", 
                   null, "greaterthan", lastId));

                //Lets search again
                var searchResults = nlapiSearchRecord('item', 'customsearch12',
                           arrNewFilters, null);

                if (searchResults != null) {

                    //Append the search result to the result present before
                    completeResults = completeResults.concat(searchResults);
                }
            }
        }


        for (var result = 0; result < completeResults.length; result++) {
         //Loop through the items      
         }

Hope you got me Nitish!!!

Upvotes: 5

Nitish
Nitish

Reputation: 796

try this code

var loadSearch=nlapiLoadSearch('item','customsearch12');
var getData = loadSearch.runSearch();
var itemCol=new Array();
getData.forEachResult(function (searchRow) {

    itemCol.push(searchRow.getId());
    return true;
});

when you use a nlapiLoadSearch, the result is nlobjSearchResultSet and not an array like in the case of nlapiSearchRecord.

if you use nlapiSearchRecord, then you can loop through the result like you were trying in your code, i.e. using index

Upvotes: 6

Related Questions