SS1.0 ``` //creates an array of nlobjsearchResults...
# suitescript
g
SS1.0
Copy code
//creates an array of nlobjsearchResults objects from a nlobjSearch object. This is useful to get 
//around the 1000 result limit from running a search. The search should be a nlobjSearch object 
//from running nlapiLoadSearch or nlapiCreateSearch
/**
 * @param {nlobjSearch} searchObj
 * @returns {Array}
 */
function createSearchResults(searchObj, context){
	var searchResults = searchObj.runSearch();
	var resultIndex = 0;
	var resultSlice;
	var results = [];
	do {
		nlapiLogExecution('DEBUG', 'getResults', 'start: ' + resultIndex + ' -- end: ' 
				+ (resultIndex + 1000))
		try {
			resultSlice = searchResults.getResults(resultIndex, resultIndex+1000);			
		} catch (e){
			if (e instanceof nlobjError){
				nlapiLogExecution('ERROR', e.getCode(), e.getDetails());
			} else {
				nlapiLogExecution('ERROR', 'Unexpected error', e.toString());
			}
			return;			
		}
		for (var rs in resultSlice){
			results.push(resultSlice[rs]);
			resultIndex++;
		}
		if (context){
			if (context.getRemainingUsage() < 50){
				nlapiLogExecution('DEBUG', 'EVENT', 'yielding on row' + resultIndex + 
						' after processing it');
				var state = nlapiYieldScript();
				if (state.status == 'FAILURE'){
					nlapiLogExecution('ERROR', 'Failed to create recovery point because of ' + 
							state.reason + ' / size ' + state.size);
					return;
				}
			}
		}
	} while (resultSlice.length >= 1000);
	return results;
}

//takes an array of nlobjSearchResult objects and parses them down into a simple javascript array
//the above function "createSearchResults" may prove useful for creating the searchResults array
/*
 * @param {nlobjSearchobj} resultsArray - The array of search results to parse into a JS array
 * @returns array[][] - 2D array containing the values from the search. Row 0 is the header row
 */
function arrayFromSearchResults(resultsArray){
	if (resultsArray && resultsArray.length !== 0){

		//array of nlobjColumns so we can get the headers
		var columns = resultsArray[0].getAllColumns();
		
		var numColumns = columns.length;
		var numRows = resultsArray.length;
		var outputArray = [];//2D array that will contain all of the data
		
		//initialize the 2D array, set length to numrows+1 so that it can hold the header as well
		for (var i = 0; i < numRows + 1; i++){
			outputArray.push([]);
		}
		//first row has all of the column headers
		for (var i = 0; i < numColumns; i++){
			outputArray[0][i] = columns[i].getLabel()
		}
		
		
		//append the search results
		for (var i = 0; i < numRows; i++){
			var resultSet = resultsArray[i];
			
			// Looping through each column and assign it to the temp array
			for (var y = 0; y < numColumns; y++) {
				//place at row i+1 since we filled row i=0 with the headers
				outputArray[i+1].push(resultSet.getValue(columns[y]));
			}
		}

		return outputArray;
	} else {
		return null;
	}
}