Catalog Items: Mapping Request Item Variables to Fields

Joe Webster
Giga Contributor

Hey all, I'm trying to write a script to work in place of a "map to field" option, which ServiceNow seems to have omitted capability from when creating Requests/RITM/Tasks. I already realize this functionality is available with Record Producer's, but I am trying to follow ITIL & ITSM best practice.

I am attempting to grab the value of a variable (input by user) which could be a string, OR a reference. If the user has a valid account, the type will be a reference. If the user has an invalidated account, the type will be a string. Either way, I need a script to grab the value of said variable, and place it in the 'requested_for' field on the Request/RITM/Task. We're primarily working with RITM's if that helps. 

Below is my current script. I've housed it within a UI Policy and it should be executing, but I believe I'm just not querying correctly, or get() the right value. I have the check boxes, 'Applies on Catalog Tasks', & 'Applies on Request Items' checked. I do not have 'Applies on a Catalog Item' checked. On load is also checked.

Here is my script: 

function onCondition() {
	var ID = g_form.getValue('user_id');
	var gr = new GlideRecord('sys_user');
	
	// if the user_id is empty, set to guest
	if (ID == '') {
		gr.addQuery('user_name','=','guest'); // in the sys_user table, user_name = ID
		gr.query();
		if (gr.next()) {
			// need to return user 'sys_id'
			g_form.setValue('requested_for', gr.get('sys_id'));
		}		
	}
	// else if the user_id is of type string, lookup the sys_id of given string in the sys_user table
	else if (typeof ID === 'string') {
		gr.addQuery('user_name','=', ID); // in the sys_user table, user_name = ID
		gr.query();
		if (gr.next()) {
			// need to return user 'sys_id'
			g_form.setValue('requested_for', gr.get('sys_id'));
		}		
	}
	// else the value must be the correct type and value, so set the value of requested_for to user_id
	else {
		g_form.setValue('requested_for', ID);
	}

}

 

1 ACCEPTED SOLUTION

Joe Webster
Giga Contributor

Hey Brian,

I figured out how to do this by using calling the addQuery() function more than once. I also chose not to use the ID. Instead of querying by ID, I chose to query by the user_name, first_name, & last_name. By doing this, I have a very inclusive AND gate and return only the necessary user from sys_user.

Here is the code which worked for me:

	var gr = new GlideRecord('sys_user');
	gr.addQuery('user_name','CONTAINS', 'guest');
	gr.addQuery('first_name', '=', 'guest');
	gr.addQuery('last_name', '=', 'user');
	gr.query();
	
	if (gr.next()) {
		g_form.setValue('requested_for', gr.sys_id);
	}
	g_form.setReadOnly('requested_for', true);

View solution in original post

2 REPLIES 2

Brian Lancaster
Tera Sage

I would suggest doing this in the workflow in a run script activity right after the start of the workflow.  I'm note sure a UI policy is the best place is do a script like this.

Joe Webster
Giga Contributor

Hey Brian,

I figured out how to do this by using calling the addQuery() function more than once. I also chose not to use the ID. Instead of querying by ID, I chose to query by the user_name, first_name, & last_name. By doing this, I have a very inclusive AND gate and return only the necessary user from sys_user.

Here is the code which worked for me:

	var gr = new GlideRecord('sys_user');
	gr.addQuery('user_name','CONTAINS', 'guest');
	gr.addQuery('first_name', '=', 'guest');
	gr.addQuery('last_name', '=', 'user');
	gr.query();
	
	if (gr.next()) {
		g_form.setValue('requested_for', gr.sys_id);
	}
	g_form.setReadOnly('requested_for', true);