Help with building a Relationship to query RITM variables

Steve8
Tera Contributor

I am trying to reuse some code I found on another post for building relationships. I am still learning scripting. But I fear trying to query off RITM variables is tossing me for a loop and I could use some help. My goal is to build a related list for my facility table to show all request items that have the facility selected in the RITM's variable field site_location_1 or site_location_2. Ideally I dont want to have to define the catalog item specifically, because I have many catalog items that use this field name. But if I do, would need to build an array of cata item names.

 

(function refineQuery(current, parent) {

	// Add your code here, such as current.addQuery(field, value);
	var ritms = [];
	var gr = new GlideRecord('sc_req_item');
	gr.addQuery('cat_item.variables.site_location_1',parent.sys_id);
	gr.addQuery('cat_item.variables.site_location_2',parent.sys_id);
	gr.query();
	while(gr.next())
		{
		ritms.push(gr.getValue('sys_id'));
		}
	
	current.addEncodedQuery(ritms);

})(current, parent);

 

find_real_file.png

1 ACCEPTED SOLUTION

Steve8
Tera Contributor

I played around with the code some and got this working. Thank you for the help!

 

(function refineQuery(current, parent) {

	// Add your code here, such as current.addQuery(field, value);
	var ritms = [];
	var gr = new GlideRecord('sc_req_item');
	gr.query();
	while (gr.next()) {
		var siteLoc1 = gr.variables.site_location_1;
		var siteLoc2 = gr.variables.site_location_2;
		if (siteLoc1 == parent.getValue('sys_id') || siteLoc2 == parent.getValue('sys_id')) {
			ritms.push(gr.getValue('sys_id'));
		}
	}
	current.addQuery('sys_id', ritms);
})(current, parent);

View solution in original post

5 REPLIES 5

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Steve,

Updated code below.

(function refineQuery(current, parent) {

    // Add your code here, such as current.addQuery(field, value);
    var ritms = [];
    var ritmGr = new GlideRecord('sc_req_item');
    ritmGr.query();
    while (ritmGr.next()) {
        var siteLoc1 = ritmGr.variables.site_location_1.getDisplayValue();
        siteLoc1 = siteLoc1.toLowerCase();
        var siteLoc2 = ritmGr.variables.site_location_1.getDisplayValue();
        siteLoc2 = siteLoc2.toLowerCase();
        if (siteLoc1.includes("facility") || siteLoc2.includes("facility")) // Replace facility with the exact word you want to search
        {
            ritms.push('' + ritmGr.sys_id);

        }
    }
    current.addQuery('sys_id', ritms);

})(current, parent);

- Pradeep Sharma

Hey Pradeep,

 

I tried your recommended code and my related list is showing all ritms and instead of a filtered list. Also in this scenario, the ritm variables site_location_(1/2) are reference fields to current's database table. Instead of using name strings, how would I search off current's system id?

Thank you for the help,

Steve

Community Alums
Not applicable

Hi Pradeep,

 

I just want to seek help also regarding this, we have almost the same requirement. But the difference is we have catalog items variables that are referenced to a custom asset table. The main objective is that we need to query all the RITMS that uses this asset and display it on the related list of our vehicle table. We have this script which is working but the downside of this is the custom table is loading too slow. How can we optimize this? Thanks

 

(function refineQuery(current, parent) {


var parID = parent.getUniqueValue();
var itemList1 = 'variables.389f2246db269010d273dc2dd39619ae';

var itemList2 = 'variables.4749b646db225410a0827c63f396192c'; 
var itemList3 = 'variables.62d4b682dbee1410a0827c63f39619f3'; 


 current.addQuery(itemList1, parID).addOrCondition(itemList2, parID).addOrCondition(itemList3,parID);
})(current, parent);

 

Thanks in advance.

Steve8
Tera Contributor

I played around with the code some and got this working. Thank you for the help!

 

(function refineQuery(current, parent) {

	// Add your code here, such as current.addQuery(field, value);
	var ritms = [];
	var gr = new GlideRecord('sc_req_item');
	gr.query();
	while (gr.next()) {
		var siteLoc1 = gr.variables.site_location_1;
		var siteLoc2 = gr.variables.site_location_2;
		if (siteLoc1 == parent.getValue('sys_id') || siteLoc2 == parent.getValue('sys_id')) {
			ritms.push(gr.getValue('sys_id'));
		}
	}
	current.addQuery('sys_id', ritms);
})(current, parent);