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.

Custom related list to show catalog ui policies on a variable

Moedeb
Tera Guru

I am wanting to create a related list that I can see any catalog ui policies that a variable has related to it.

 

I have been creating a relationship record as such: 

Name: Catalog UI Policies
Application:Global
Applies to table:Variable [item_option_new]
Queries from table:Catalog UI Policy [catalog_ui_policy]

 

Script:

(function refineQuery(current, parent) {
var records = new GlideRecord('catalog_ui_policy');
records.addQuery('item_option_new', parent.getUniqueValue());
var encodedQuery = records.getEncodedQuery();
if (encodedQuery) {
	current.addQuery('sys_id', 'IN', encodedQuery);
}
})(current, parent);

 

No results are showing. I have tried some other scripts such as:

current.addQuery('sys_id', 'IN', (function() {
    var ids = [];
    var gr = new GlideRecord('sys_ui_policy_action');
    gr.addQuery('variable', current.sys_id);
    gr.query();
    while (gr.next()) {
        ids.push(gr.ui_policy.toString());
    }
    return ids.join(',');
})());

This however shows all ui policies

 

Anyone able to assist?

 

1 ACCEPTED SOLUTION

J Siva
Kilo Patron
Kilo Patron

Hi @Moedeb 
You can acheive this using "Relationships".
Try the below.

Table: sys_relationship

(function refineQuery(current, parent) {
	var id = "IO:"+parent.sys_id.toString();
	current.addQuery("catalog_variable",id);

	// Add your code here, such as current.addQuery(field, value);

})(current, parent);

JSiva_0-1752123482575.png

Output:

JSiva_1-1752123551004.png

Hope this helps.
Regards,
Siva

 

 

View solution in original post

11 REPLIES 11

Community Alums
Not applicable

In your relationship, keep:

  • Applies to table: item_option_new

  • Queries from table: catalog_ui_policy

(function refineQuery(current, parent) {
    var ids = [];
    var gr = new GlideRecord('catalog_ui_policy_action');
    gr.addQuery('variable', parent.sys_id);
    gr.query();
    while (gr.next()) {
        ids.push(gr.ui_policy.toString());
    }
    if (ids.length > 0) {
        current.addQuery('sys_id', 'IN', ids.join(','));
    } else {
        // add an impossible query to prevent showing all policies
        current.addQuery('sys_id', 'NULL');
    }
})(current, parent);

 

@Community Alums Thanks for trying to help, unfortunately it still isn't showing any results