Hi All, Search is taking too much time and script ...
# suitescript
s
Hi All, Search is taking too much time and script is throwing timeout exception in Restlet there are 2275 timesheets in the search results can the code be optimized further? JSON Request->{"RequestDate": "04/02/2023", "ToDate": "04/02/2023"} code->
Copy code
var d_Current_Date;
		if(d_Request_ToDate){
			d_Current_Date = nlapiDateToString(d_Request_ToDate);
			d_Current_Date =nlapiStringToDate(d_Current_Date);
		}
		else{
			d_Current_Date = nlapiDateToString(new Date());
			d_Current_Date =nlapiStringToDate(d_Current_Date);
		}
		nlapiLogExecution('DEBUG', 'd_Request_Date in funtion ==', d_Request_Date);
		nlapiLogExecution('DEBUG', 'd_Current_Date==', d_Current_Date);
		var arr_Function_Response = [];
		var obj_Function_response = {};
		var arr_TS_Filter =[
		 				   [["timebill.datecreated","within",d_Request_Date,d_Current_Date],
	"OR",["timebill.lastmodified","within",d_Request_Date,d_Current_Date]],
							 "AND", 
								["approvalstatus","noneof","1"]
								];
		var arr_Ts_Columns = [
		   				   new nlobjSearchColumn("internalid",null,"GROUP"), 
						   new nlobjSearchColumn("employee",null,"GROUP"), 
						   new nlobjSearchColumn("startdate",null,"GROUP"), 
						   new nlobjSearchColumn("enddate",null,"GROUP"), 
						   new nlobjSearchColumn("email","employee","GROUP")
						];
		var obj_TS_Search =  searchRecord('timesheet',null,arr_TS_Filter,arr_Ts_Columns);
		if(obj_TS_Search)
			{
				var i_Length_TS_Count = obj_TS_Search.length;
				nlapiLogExecution('DEBUG', 'i_Lenght_TS_Count==', i_Length_TS_Count);
				var i_Internal_Id_Ts = '';
				var s_Employee_name = '';
				var d_Start_Date = '';
				var d_End_Date = '';
				for(var i_Index_loop = 0 ; i_Index_loop < i_Length_TS_Count ; i_Index_loop++ )
					{
						i_Internal_Id_Ts = obj_TS_Search[i_Index_loop].getValue("internalid",null,"GROUP");
						s_Employee_name = obj_TS_Search[i_Index_loop].getText("employee",null,"GROUP");
						d_Start_Date = obj_TS_Search[i_Index_loop].getValue("startdate",null,"GROUP");
						d_End_Date = obj_TS_Search[i_Index_loop].getValue("enddate",null,"GROUP");
						obj_Function_response = {
								Internal_Id : i_Internal_Id_Ts,
								Employee : s_Employee_name,
								Start_Date : d_Start_Date,
								End_Date : d_End_Date
						}
						arr_Function_Response.push(obj_Function_response);
					} ///// for(var i_Index_loop = 0 ; i_Index_loop < i_Length_TS_Count ; i_Index_loop++ )	
				
				
			} ///// if(obj_TS_Search)
		nlapiLogExecution('DEBUG','arr_Function_Response==', JSON.stringify(arr_Function_Response))
		return arr_Function_Response
	}
function searchRecord(recordType, savedSearch, arrFilters, arrColumns,
        filterExpression)
{

	try {
		var search = null;

		// if a saved search is provided, load it and add the filters and
		// columns
		if (isNotEmpty(savedSearch)) {
			search = nlapiLoadSearch(recordType, savedSearch);

			if (isArrayNotEmpty(arrFilters)) {
				search.addFilters(arrFilters);
			}

			if (isArrayNotEmpty(arrColumns)) {
				search.addColumns(arrColumns);
			}

			if (isArrayNotEmpty(filterExpression)) {
				search.setFilterExpression(filterExpression);
			}
		}
		// create a new search
		else {
			search = nlapiCreateSearch(recordType, arrFilters, arrColumns);
		}

		// run search
		var resultSet = search.runSearch();

		// iterate through the search and get all data 1000 at a time
		var searchResultCount = 0;
		var resultSlice = null;
		var searchResult = [];

		do {
			resultSlice = resultSet.getResults(searchResultCount,
			        searchResultCount + 1000);

			if (resultSlice) {

				resultSlice.forEach(function(result) {

					searchResult.push(result);
					searchResultCount++;
				});
			}
		} while (isArrayNotEmpty(resultSlice) && resultSlice.length >= 1000);

		return searchResult;
	} catch (err) {
		nlapiLogExecution('ERROR', 'searchRecord', err);
		throw err;
	}
}
b
probably want to add logs to the code to figure out where its taking too long
getting 2275 search results usually doesnt cause a timeout
especially not if the restlet only does a search
r
@battk Search is on timesheet. But filter is date created and date modified of timebill. Since the system has to filter out on so many timebills, could that be the reason?
s
yes i think it is taking time as we are adding filter on timebills But can we optimize the searchrecord function further or its optimized?
b
you really only need to reduce the search code to
Copy code
var search = nlapiCreateSearch(recordType, arrFilters, arrColumns);
var resultSet = search.runSearch();
var resultSlice0 = resultSet.getResults(0,1000);
var resultSlice1 = resultSet.getResults(1000, 2000);
var resultSlice2 = resultSet.getResults(2000, 3000);

return resultSlice0.concat(resultSlice1, resultSlice2 );
and time how long it takes to get each slice
if getting those 3 slices is less than the timeout time, then one of the things you have told us is false