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

I think you need to do the following. On the when to run tab add a condition with the item name. It will look like this when it is completed.

BrianLancaster_0-1701120229327.png

When you first click on the dropdown in the condition you may need to scroll down to the bottom and choose show related fields so you can dot walk to item.

 

Then the code in the business rule should be something as simple as.

 

(function executeRule(current, previous /*null when async*/ ) {
    current.addQuery(current.variables.company, "==", gs.getUser().getRecord().getValue('company')); 
})(current, previous);

 

 

 

Brad Bowman
Kilo Patron
Kilo Patron

This sounds dangerous, especially when using a Catalog Item variable that all Catalog Items, and hence Catalog Tasks don't have.  A before Query Business Rule will filter out some records, so if you are running this on the sc_task table, it will apply to EVERY table list view and related list, so you could not ever see all/the correct tasks.  Be careful that's really what you want to do.  In these types of BRs, 'current' acts like a GlideRecord on the table, so typically one adds a line like

current.addQuery('request_item.cat_item.name', 'Employee Onboarding');

Since this is a list, not a record, you don't have access to fields or variables like you do on an individual record without some crazy GlideRecords and arrays prior to the culminating addQuery.

 

It sounds like maybe what you're really trying to do is change the filter on the Catalog Task related list on the RITM record?  If so, what you want to do is define a custom relationship to make a different Related List.  In the relationship there's a script that works similarly, but since this is displayed on a record you have access to parent.field_name and parent.variables.var_name so with one related list you can show all Catalog Tasks for that RITM unless the Catalog Item is a certain one, then show only some based on the value of a Catalog Item variable.  

 

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);