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.

Relationship Query script

SO10
Tera Contributor

Hello.
I'm working on creating a Query script for Relationship.
I would like to use the sys_id of the current record to narrow down the records displayed in the related list, but I am having trouble getting the sys_id using current.sys_id.
How can I get the sys_id of the currently displayed record?
thank you.

1 ACCEPTED SOLUTION

Hey @SO10 :

if you are looking to narrow down the search of the file which has test in its name then your relationship query should be like the below and

Queries from: Attachment

Applies to : <Should be your target table>

var qc = current.addQuery('table_sys_id', parent.sys_id);
qc.addQuery('file_name', 'test.pdf');

 

 

I hope this solves your issue
Mark this as Helpful / Accept the Solution if this clears your issue

View solution in original post

3 REPLIES 3

Eswar Chappa
Mega Sage

Hi @SO10 Can you please post the script here?

@Eswar Chappa 
Thank you for your reply.
I have created the following script.

(function refineQuery(current, parent) {
    var attachment = new GlideSysAttachment();
    var agr = attachment.getAttachments('u_test', current.sys_id); 
    var attachmentSysIds = [];

    while (agr.next()) {
        var fileName = agr.getValue('file_name');
        if (fileName.includes('test.pdf')) {
            attachmentSysIds.push(agr.getValue('sys_id'));
        }
    }
    current.addQuery('u_attachment_id',  attachmentSysIds);
})(current, parent);

What I want to do is query the u_attachment_id field of the related list with the sys_id of the test file attached to the record in the u_test table. For that I want to get the sys_id of the currently open record in u_test.


I'm not very good at scripting, so sorry if I'm wrong to begin with.

Hey @SO10 :

if you are looking to narrow down the search of the file which has test in its name then your relationship query should be like the below and

Queries from: Attachment

Applies to : <Should be your target table>

var qc = current.addQuery('table_sys_id', parent.sys_id);
qc.addQuery('file_name', 'test.pdf');

 

 

I hope this solves your issue
Mark this as Helpful / Accept the Solution if this clears your issue