Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Relationship Query script from string

DoDo labs___
Mega Sage

Hi!

 

I have created the following script in Relationship:

 

(function refineQuery(current, parent) {

var lista = '';
var tomb = [];

var gr = new GlideRecord('problem');
gr.addQuery('number', current.sys_id);
gr.query();
if (gr.next())
    {
        lista = gr.u_change_list;
    }  

tomb = lista.split(",");

for (var i = 0; i < tomb.length; i++)
    {
        current.addQuery(tomb[i], parent.number);
    }

})(current, parent);
 
DoDolabs____0-1720003329499.png

 

 
In the Problem table, there is a string-type dictionary entries. This entry contains change requests separated by commas. For example: CHG0034611,CHG0034595,CHG0034539.
I would like to display these in the related list, but no changes are displayed.
 

Can anyone please help me regarding it.

Thanks!

 
1 ACCEPTED SOLUTION

DoDo labs___
Mega Sage
this is what worked for me:
 
(function refineQuery(current, parent) {

var lista = '';

var gr = new GlideRecord('problem');
gr.addQuery('sys_id', parent.sys_id);
gr.query();
if (gr.next())
    {
        lista = gr.u_change_list;
    }  

current.addQuery('number', 'IN', lista);

})(current, parent);

View solution in original post

6 REPLIES 6

Satishkumar B
Giga Sage

Hi @DoDo labs___ 

To display related Change Requests from a comma-separated list stored in the "u_change_list" field of a Problem record:

 

 

(function refineQuery(current, parent) {
    var lista = current.u_change_list;
    if (lista) {
        var changeNumbers = lista.split(",");
        var relatedChangeRequests = new GlideRecord('change_request');
        relatedChangeRequests.addQuery('number', 'IN', changeNumbers);
        relatedChangeRequests.query();
        
        // Assuming you want to display related change requests in a related list
        var relatedList = current.getOrCreateRelatedList('change_request');
        relatedList.addQuery('number', 'IN', changeNumbers);
    }
})(current, parent);

 

 

 

 

Ensure to replace 'change_request' with the actual related list table name in your ServiceNow instance. This script should help display the related change requests correctly in the related list on the Problem form.

--------------------------------------------------------------------------------------------------------------------
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

 

DoDo labs___
Mega Sage
this is what worked for me:
 
(function refineQuery(current, parent) {

var lista = '';

var gr = new GlideRecord('problem');
gr.addQuery('sys_id', parent.sys_id);
gr.query();
if (gr.next())
    {
        lista = gr.u_change_list;
    }  

current.addQuery('number', 'IN', lista);

})(current, parent);