Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scripting with catalog variables on tasks (sc_item_option_mtom) table

nmartinez
Kilo Contributor

I am familiar with accessing [sc_item_option] and [sc_item_option_mtom] to query, modify and even create new catalog variables for request items via scripting; however I am trying to figure out how to associate catalog variables with newly created catalog tasks via scripting in the same fashion that it is done via the visual workflow.

I assume that there must be a related table that connects the catalog variables to the catalog task and then not unlike [sc_item_option_mtom] that would reference the catalog task's sys_id and the [sc_item_option] record. If there is such a table it does not have the "mtom" in the table's name. My second guess was that it would be either a script include or a business rule that is dynamically associating the variables to the catalog tasks. However I've yet to find any scripts that perform this function.

If anyone knows how to accomplish this, I would appreciate some help.

23 REPLIES 23

Zach Biewend1
Giga Expert

Late post, but to anyone else stumbling across this same issue:

I created an after (could also be async) business rule on insert for sc_task.

(function executeRule(current, previous /*null when async*/) {

	// this is used for ad hoc tasks that are manually created. This will add all the RITM variables and variable sets to the task.
	
	var variables = []; // array of variable sys_ids to add to the task
	var catItem = current.request_item.cat_item.sys_id.toString();
	var grVars, ga, gr;
	
	// get individual variables
	var grVar = new GlideRecord( 'item_option_new' );
	grVar.addQuery( 'cat_item', catItem );
	grVar.query();
	
	while ( grVar.next() ) {
		variables.push( grVar.sys_id.toString() );
	}
	
	// get variables from variable sets
	// get m2m for var set to item
	var grSet = new GlideRecord( 'io_set_item' );
	grSet.addQuery( 'sc_cat_item', catItem );
	grSet.query();
	
	while ( grSet.next() ) {
		// get variables from each variable set
		grVars = new GlideRecord( 'item_option_new' );
		grVars.addQuery( 'variable_set', grSet.variable_set.sys_id.toString() );
		grVars.query();
		
		while ( grVars.next() ) {
			variables.push( grVars.sys_id.toString() );
		}
	}
	
	// add variable, if it isn't already there 
	for ( var i = 0; i < variables.length; ++i ) {
		// count to see if var exists already
		ga = new GlideAggregate( 'sc_item_variables_task' );
		ga.addAggregate( 'COUNT' );
		ga.addQuery( 'task', current.sys_id.toString() );
		ga.addQuery( 'variable', variables[ i ] );
		ga.query();
		ga.next();
		
		if ( ga.getAggregate( 'COUNT' ) == 0 ) {
			// add variable-task record
			gr = new GlideRecord( 'sc_item_variables_task' );
			gr.initialize();
			gr.task = current.sys_id.toString();
			gr.variable = variables[ i ];
			gr.insert();
		}
	}
	
})(current, previous);

This code will add any variables or variable sets to the task that are associated with the task's parent RITM. 

Awesome, you saved me a bunch of time and hassle figuring this out. Now I just need to adapt it to ignore variables on the RITM that are empty.

Edvin
Kilo Explorer

Just add as a BR (after) on insert on sc_task. You are welcome 🙂

(function executeRule(current, previous /*null when async*/) {
	var taskId = current.getUniqueValue();
	var gri = new GlideRecord('sc_item_variables_task');
	
	var gr = new GlideRecord('sc_item_option_mtom');
	gr.addQuery('request_item', current.getValue('request_item'));
	gr.query();
	
	while(gr.next()){
		var variable = gr.sc_item_option.item_option_new.getValue();
		
		gri.initialize();
		gri.setValue('variable', variable);
		gri.setValue('task', taskId);
		gri.insert();
	}
	
})(current, previous);

Calvin11
Tera Contributor

I've created records on the sc_item_variables_task table for every variable my catalog item has and associated with a manual SC Task that I created under a RITM from it. But the variable editor still doesn't show any of these variables. The only way they do is if I set the variables to global in the Catalog Item.

 

Yet other SC Tasks that were created automatically when the RITM was first created show all of them.

 

What am I missing?

It sounded like the solution to making variables appear on a manually created SC Task was simply to create the appropriate records on the sc_item_variables_task table, but apparently not.(?)