Catalog Task, Mandatory Variables on Close

Punahele Tanne1
Tera Guru

Our Service Catalog has a number of processes that require fulfillers to enter information into editable Variables in their Catalog Task before they can close it.  Typically, that information is used by the next group to complete the next phase of the process.  Quick example: Field Services goes to a location with a network fluke to gather jack information, like the jack id on the wall plate and the data jack's switch from the fluke.  That information is then entered into editable Variable fields in the Field Service technician's Catalog Task.  That information will then be used by the Network Support team to configure the data jack on the switch.  The problem was that if the Field Service technician did not enter the data into their Catalog Task, the Network Support team cannot complete their Catalog Task.  The existing solution was to put a check in the Catalog Task closing UI Action to make specific fields on specific forms mandatory before allowing the Catalog Task to close.  That solution is not scalable as it would require continually updating the UI Action with new forms and processes.

 

As many of the editable Variables are housed in Variable Sets, we have begun to roll back the previous development method of putting conditions in the UI Action for closing the Catalog Task and moving towards using onSubmit Client Scripts on common field Variable Sets.  The functionality now lean on onSubmit client scripts and guard classes checking for which button was pushed (g_form.getActionName({ui_action_name}) and what stage the process is in.  This has been found to be scalable and automatically stacks with other Variable Sets and Catalog Item onSubmit scripts.

 

Here's the client script.

 

 

function onSubmit() {

	if (g_form.getActionName() != 'close_sctask') { return; }	// exit, if not Close button

	// get RITM stage without getReference
	var ritmStage = g_form.getValue('request_item.stage').toLowerCase();
	if (ritmStage != 'assessment') { return; }					// exit, if not the desired process stage

	// array of variable names
	// recommended to list variable names from the bottom of the form up,
	// so the top most form field that is empty will get initial focus
	var varFields = [			
		'vs_set_variable_5',
		'vs_set_variable_4',
		'vs_set_variable_3',
		'vs_set_variable_2',
		'vs_set_variable_1'
	];
	var focusField = '';				// used as trigger, and for identifying top most variable to focus

	varFields.forEach(function(mfield){							// iterate through each array entry
		if (g_form.resolveNameMap(mfield)==mfield) { return; }	// exit if no entry in NameMap (not on page)
	
		g_form.setMandatory(mfield,true);						// ensure mandatory
	
		if (g_form.getValue(mfield)=='') {						// if no value
			focusField = mfield;								// previously mandatory field name
		}
	});

	if (focusField != '') {		// if there are blank fields
		setTimeout(function(){	// setting timeout works better than without, for some reason

			var formtab = g_tabs2Sections.findTabIndexByName('details');	// get Details form tab object
			g_tabs2Sections.setActive(formtab);							// ensure tab is active
			var nmField = g_form.resolveNameMap(focusField);			// get the true dom id for the field
			if (g_form.getElement('sys_display.'+nmField)) {			// if reference field dom object exists
				// https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0755305
				g_form.getElement(nmField).parentElement.lastElementChild.firstChild.focus();
			} else {
				g_form.getElement(nmField).focus();							// non-Reference field focus
			}

		}, 0);
		return false;		// stop form submit
	}

}

 

 

This functionality was strictly developed for use on Catalog Tasks.  It has not been tested on the Service Portal, or other ServiceNow applications.

 

All comments are appreciated.  Thank you.

1 REPLY 1

Punahele Tanne1
Tera Guru

The above script was updated with the latest development that includes multiple Variable Editor field checking, and an improvement that determines if the Variable field being given focus is a Reference, requiring focus() be initiated on a sibling element.

Thank you!