Does anyone have a sample script to combine two sa...
# suitescript
l
Does anyone have a sample script to combine two saved search results? Thank you!
e
what do you mean by "to combine two saved search results"? • you mean you run the same SavedSearch twice and you want to concatenate the results from the 1st run with the 2nd run? • or you mean you run 2 different SavedSearches, with different columns/results...and then you want to...join(maybe?) the results of the two? - but then by what key?... Maybe you can provide more details about the case/context you refer, to be able to be provided with a more precise reply.
s
NFT should be able to do so with a single line of code, however I'd probably recommend trying a single SuiteQL search instead.
l
To give a concrete example, Source transaction: Sub A Amount 100 Saved Search A results: Formula Text (custom label Subsidiary): 'Sub A' Formula Currency (custom label Credit): {debit} * 0.2 Saved Search B results: Formula Text (custom label Subsidiary): 'Sub B' Formula Currency (custom label Debit): {credit} * 0.2
The results shall be combined: Subsidiary Debit Credit Sub A (null) 200 Sub B 200 (null)
Combine the results of two transaction saved searches. Thanks!
s
I haven't done this in a while but I think with NFT it would be
LazySearch.from(search.load(searchid1)).concat(LazySearch.from(search.load(searchid2))
. Without NFT you're going to need to create concatenated results more manually. You'll also have to consider the overall result set to determine of it fits into memory. If the total result size is small, building up an array in memory is one approach.
e
yeah, so w/o NFT, you can have a function, which will do a .runPaged (for the 2 loaded SavedSearches, already defined) and return an array with all the results:
Copy code
/**
	 * Searches all (>1000) results (CLASSIC - w/ runPaged)
	 */
	 
	//var objSavedSearch1 = search.load({
	//	id: 'custsearch123'
	//});
	function searchAll(objSavedSearch1,objSavedSearch2) {
        var maxSearchReturn = 1000; //(max no of results per page is 1000)

		// Data
		var arrReturnSearchResults = [];
		
		var objPagedData1 = objSavedSearch1.runPaged({
			pageSize: maxSearchReturn 
		});
		objPagedData1.pageRanges.forEach(function(pageRange){
			var objPage = objPagedData1.fetch({index: pageRange.index}).data;
			arrReturnSearchResults = arrReturnSearchResults.concat(objPage);
		});

        var objPagedData2 = objSavedSearch2.runPaged({
			pageSize: maxSearchReturn 
		});
		objPagedData2.pageRanges.forEach(function(pageRange){
			var objPage = objPagedData2.fetch({index: pageRange.index}).data;
			arrReturnSearchResults = arrReturnSearchResults.concat(objPage);
		});
		
		return arrReturnSearchResults;
    }
(of course, the memory limitation shall be considered...but I guess if you have only 3 columns, as you described, should be no problem to hold an array for 100k+ results) hope it helps
l
Thank you! I'll take at look at these.
j
Hi @Era I'm trying to combine 2 different transaction search (PO & Item Receipt) using lineuniquekey, but I don't want to use runPaged on each search due to governance issue. Will NFT work in this case? Thank you
s
I'd recommend trying SuiteQL to create a single search. NFT uses
runPaged()
under the covers because that's what NS makes available. Note however that it executes lazily - i.e. only pulls subsequent pages of results as needed during processing.