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.

SC_TASK Variable Relationship Script

DylanBlumenberg
Tera Guru

Hey everyone, I'm trying to create a relationship on the sc_task table and I'm running into some issues. The goal is to take the value of the employee_number catalog item variable from the current record and look up any other sc_task records that contain that same value on the employee_number variable. Below is what I have so far but it's returning every sc_task record in the system. Does anyone know where I went wrong?

 

Things worth noting: 

  • 'Applies to table' and 'Queries from table' are both sc_task.
  • There is a normal variable named employee_number and a variable set variable named employee_number. This script should look for values from both variables. 
  • In the log, gs.info is returning the correct employee_number variable.

 

(function refineQuery(current, parent) {

    var employeeNumber = parent.request_item.variables.employee_number;
    gs.info('Related List Debug. Value found is: ' + employeeNumber);

    current.addQuery('request_item.variables.employee_number', employeeNumber);

})(current, parent);

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@DylanBlumenberg 

variables are linked with RITM and not with sc_task

Something like this should work, assuming you want to see related sc_tasks with same variable value

(function refineQuery(current, parent) {

    var employeeNumber = parent.request_item.variables.employee_number;
    var variableSysId = ''; // give here the sysId of that variable from item_option_new table
    var ritmArr = [];
    var gr = new GlideRecord("sc_req_item");
    gr.addQuery("variables." + variableSysId, employeeNumber);
    gr.query();
    while (gr.next()) {
        ritmArr.push(gr.getUniqueValue());
    }
    current.addQuery('request_item.sys_id', 'IN', ritmArr.toString());

})(current, parent);

AnkurBawiskar_0-1749199957204.png

 

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

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

View solution in original post

5 REPLIES 5

@DylanBlumenberg 

Glad to help.

Happy learning !

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