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.

getting the item name from query BR

samadam
Kilo Sage

I want to restrict catalog tasks for certain catalog item to only if it matches the company variable on the record. I am trying to write a before query BR where I set the item in Filter condition. I see some java script errors after I add this. 

I am not able to access the name or variables using the following.

 

var item_name = current.request_item.cat_item.name;
var comp = current.variables.company.toString();

 

Ant ideas on how to get this?

1 ACCEPTED SOLUTION

OK, so not terribly crazy on the before Query script, but still dangerous as no one will see every Catalog Task for these items now.  You can run this BR with no Filter Conditions or Condition:

(function executeRule(current, previous /*null when async*/) {
	var tskArr = [];
	var tskGR = new GlideRecord('sc_task');
	tskGR.query();
	while (tskGR.next()) {
		//push every Catalog Task to an array, unless it belongs to Catalog Item 'A'
		if (tskGR.request_item.cat_item.name != 'A'){
			tskArr.push(tskGR.sys_id.toString());
		//only push this Catalog Item's tasks if the 'company' variable value is the same as the current user's 
		} else if (tskGR.variables.company == gs.getUser().getRecord().getValue('company')) {
			tskArr.push(tskGR.sys_id.toString());
		}
	}
	current.addQuery('sys_id', 'IN', tskArr.join(','));
})(current, previous);

 

View solution in original post

7 REPLIES 7

Brian Lancaster
Kilo Patron

if you want to see the name you need the following.

current.request_item.cat_item.getDisplayValue()

current.variables.company.getDisplayValue()

samadam
Kilo Sage

I tried that but no luck.

Can you give us all the code and explain more what you are trying to do.

samadam
Kilo Sage

I have a Query business rule on sc_task. I am trying to restrict the sc_tasks related to catalog item A to only users in the same company.

(function executeRule(current, previous /*null when async*/) {
var item_name = current.request_item.cat_item.getDisplayValue();
var comp = current.variables.company;
var ucomp;
//I want to run this only if catalog item == A so trying to get the catalog item name
  var usr = new GlideRecord('sys_user');
        usr.addQuery('sys_id', user_id.toString());
        usr.query();
        if (usr.next()) {
ucomp = usr.company;
if (office != '')
current.addQuery("variables.company", ucomp);
}
})(current, previous);