Auto-pick list collector option if there is only one option available.

Brian Bouchard
Mega Sage

I have a requirement for our Service Catalog that if there is only one option in a a list collector variable we want the system to automatically pick that option and hide the variable on the form.

 

I have not been able to find a way to determine how many options are available without rerunning the query that was run to generate the reference qualifier. Is there a better way to get this info?  It seems like a lot of overhead for something that may already be readily available.

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Yeah, this is specific to the native UI as that is the only one with an Available and Selected box.  I don't know of a way in Service Portal to determine how many results are returned from a reference qualifier without re-running the same query.

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

Hi Brian,

You can do something like this - make sure the Isolate script box remains unchecked:

 

function onLoad() {
	//List Collector is loaded after the form is loaded. Hence setting a delay of 3 secs.  Adjust based on size/complexity of reference qualifier
	setTimeout(function() {
		getList();
	}, 3000);
}

function getList() {
	var varName = 'v_list';
	var leftBucket = gel(varName + '_select_0');
	var rightBucket = gel(varName + '_select_1');
	var selectedOptions = leftBucket.options;

	if (selectedOptions.length == 1) {
		//Get an array of all option IDs to move
		var selectedIDs = new Array();
		var index = 0;
		for(var i = 0; i < selectedOptions.length; i++){
			selectedIDs[index] = i;
			index++;
		}
	
		//Move all returned options from left to right bucket and sort the results
		moveSelectedOptions(selectedIDs, leftBucket, rightBucket, '--None--');
		//Sort the resultant options in the left bucket
		sortSelect(leftBucket);
		g_form.setDisplay(varName, false);
	}
}

 

Brian Bouchard
Mega Sage

Thanks Brad - I tried this, but the code is failing on the line:

 

	var leftBucket = gel(varName + '_select_0');

 

 

with the following exception:

Exception while executing request: stream has already been operated upon or closed: com.glide.rest.util.RESTRuntimeException: Exception while executing request: stream has already been operated upon or closed: 	com.glide.rest.handler.impl.ServiceHandlerImpl.handleInvocationTargetException(ServiceHandlerImpl.java:76)	com.glide.rest.handler.impl.ServiceHandlerImpl.invokeService(ServiceHandlerImpl.java:49)
	com.glide.rest.processors.RESTAPIProcessor.process(RESTAPIProcessor.java:376)
	com.glide.processors.AProcessor.runProcessor(AProcessor.java:613)
	com.glide.processors.AProcessor.processTransaction(AProcessor.java:275)
	com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:184)
	com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:172)
	com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:51)
	com.glide.sys.Transaction.run(Transaction.java:2502)
	com.glide.ui.HTTPTransaction.run(HTTPTransaction.java:27)
	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	java.base/java.lang.Thread.run(Thread.java:829)

Are you using Service Portal, Employee Center,... or the native UI?  When and where are you seeing the error?

Brian Bouchard
Mega Sage

I see it in the system logs for both Service Portal and Employee Center page loads.

I confirmed the Isolate Script field is unchecked, and I updated varName to the name of the variable (entitlements).

I know it's failing on that specific line because I put displays before and after the line and only the one that comes before it is displaying.

I don't see the error or the display lines in the Native CMS view