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
Tera Sage

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Moedeb 

try this

1) query catalog ui policy action with query as variable contains Variable SysId

2) then get all those UI policies

3) then form the query

(function refineQuery(current, parent) {
    var policyIds = [];
    var actionGr = new GlideRecord('catalog_ui_policy_action');
    actionGr.addQuery('catalog_variable', 'LIKE', parent.getUniqueValue());
    actionGr.query();
    while (actionGr.next()) {
        policyIds.push(actionGr.ui_policy.toString());
    }
    if (policyIds.length > 0) {
        current.addQuery('sys_id', 'IN', policyIds.join(','));
    } else {
        // No matches, ensure no records are returned
        current.addQuery('sys_id', '-1');
    }
})(current, parent);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

Sorry how should these be set as:

Moedeb_0-1752124355185.png

 

@Moedeb 

same as per your screenshot.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yeah I've tried that and get no results:
As you can see here the variable I am using to test this on certainly is related to a catalog ui policy:

Moedeb_0-1752129667416.png

 

 

@Moedeb 

Since you mentioned you need to see Catalog UI policies associated with that variable and hence the logic I shared uses catalog UI policy as Queries from similar to your question asked.

The script I shared will show you Catalog UI policies under that variable where that variable is used in Catalog UI policy Action.

I believe that's what you wanted and you didn't wish to see Catalog UI policy action in related list.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader