Search Sources: Paginate is not working when following the example

Dan142
Giga Expert

https://docs.servicenow.com/bundle/newyork-servicenow-platform/page/build/service-portal/task/pagina...

 

When following the above example, the code below does not display any results:

(function(query, queryLocation, count) {
  var results = [];
  /* Calculate your results here. */
  var userGR = new GlideRecord('sys_user');
	userGR.addQuery('active', true);
	userGR.addQuery('name', 'CONTAINS', query);
	userGR.query();
	
	userGR.setLocation(queryLocation - 1);
	
	var resultCount = 0;
	while(userGR.next() && resultCount < count + 1){
		var obj = {};
		obj.sys_id = userGR.getUniqueValue();
		obj.primary = userGR.getDisplayValue();
		obj.query_location = userGR.getLocation();
		obj.label = userGR.getDisplayValue();	
		results.push(obj);
		resultCount++;
	}
	
	if(results.length == 0)
		return results;

	if(results.length > count) {
		results.pop();
	} else {
		// In order to indicate that a result in the result
		// set is the final result (that there are
		// no more results to be fetched), add this property
		// to the final element in your result set.
		results[results.length - 1].isLastResult = true;
	}
  return results;
})(query, queryLocation, count, facets);

 

The typeahead works but the results page does not.

Why is it not working?

1 ACCEPTED SOLUTION

The search facet needed to be enabled.


https://docs.servicenow.com/bundle/newyork-servicenow-platform/page/build/service-portal/task/enable-facets.html

View solution in original post

6 REPLIES 6

Plissken
Tera Expert

Hello,

Did you manage to get this working with a scripted query? If so, what was the outcome.

Cheers

I'm running into the same issue, using the script debugger it seems like the value for queryLocation never changes. Would like to know if anyone finds a solution!

The search facet needed to be enabled.


https://docs.servicenow.com/bundle/newyork-servicenow-platform/page/build/service-portal/task/enable-facets.html

Nick1110
Giga Contributor

After further investigation, I found that in the faceted search widget there is a point where the value for 'obj.query_location' gets coerced into a string yet treated like a number. If you are having an issue where the pagination keeps loading the same results, this can be fixed by cloning the widget and coercing the query_location value into a number. In the client script, pass last.query_location into the parseInt function: 

function generatePagination(results, previousIndex) {
			var last = results[results.length-1];
			$scope.showPagination = true;
			$scope.showLoadMore= last && !last.isLastResult;

		if(last && last.query_location != undefined) {
				$scope.query_start_location = parseInt(last.query_location) + 1;
			} else {
				$scope.query_start_location = 0;
			}

		  if(previousIndex > 0) {
				setFocusNextItem(previousIndex);
			}
	}