The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to dynamically check for a match on sys_id in encoded query?

sayalisheth
Tera Contributor

Hi

I am trying to write an encoded query for the following condition - if state is closed (3) and parent sys_id matches current parent. How do I write this?

var parent = current.regulatory_task;
var grRegActTsk = new GlideRecord('sn_grc_reg_change_regulatory_action_task');
grRegActTsk.addEncodedQuery("state=3^regulatory_task=parent");//this doesn't seem to work...
grRegActTsk.query();
//rest of the code after this...
1 ACCEPTED SOLUTION

😅Okay. I don't have testing environment so sorry for that I could not test the code and its output, still we will try to make it correct.

var parent = current.regulatory_task;
gs.addInfoMessage("parent = " + parent);
var totalHrs = 0;
var grRegActTsk = new GlideRecord('sn_grc_reg_change_regulatory_action_task');

// Combine conditions using ^ for AND
grRegActTsk.addEncodedQuery("state=3^regulatory_task=" + parent);
grRegActTsk.query();

while (grRegActTsk.next()) {
    if (!isNaN(parseInt(grRegActTsk.u_actual_hours, 10))) {
        totalHrs += parseInt(grRegActTsk.u_actual_hours, 10);
    }
}

View solution in original post

14 REPLIES 14

Aniket Chavan
Tera Sage
Tera Sage

Hello @sayalisheth ,

Please give a try to the code below and see how it works for you.

var parent = current.regulatory_task;
gs.addInfoMessage("parent = " + parent);
var totalHrs = 0;
var grRegActTsk = new GlideRecord('sn_grc_reg_change_regulatory_action_task');
// Remove the semicolon at the end of the next line
grRegActTsk.addEncodedQuery("state=3^regulatory_task=" + parent);
grRegActTsk.query();

while (grRegActTsk.next()) {
    // Remove the semicolon at the end of the next line
    if (!isNaN(parseInt(grRegActTsk.u_actual_hours, 10))) {
        totalHrs += parseInt(grRegActTsk.u_actual_hours, 10);
    }
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

that didn't work. The query is returning 0 results, so it won't go into the while loop. I know there are 2 records that match the query..

@sayalisheth Oh okay then lets try with the below one.

var parent = current.regulatory_task;
gs.addInfoMessage("parent = " + parent);
var totalHrs = 0;
var grRegActTsk = new GlideRecord('sn_grc_reg_change_regulatory_action_task');
grRegActTsk.addEncodedQuery("state=3^regulatory_task=" + parent);
grRegActTsk.query();

while (grRegActTsk.next()) {
    if (!isNaN(parseInt(grRegActTsk.u_actual_hours, 10))) {
        totalHrs += parseInt(grRegActTsk.u_actual_hours, 10);
    }
}

😞 no luck...

if I add a query to just search based on sys_id it finds the records. If I add a query to just search based on the state, it finds records then as well. If I put those together in the encoded query, then it doesn't work.

😅Okay. I don't have testing environment so sorry for that I could not test the code and its output, still we will try to make it correct.

var parent = current.regulatory_task;
gs.addInfoMessage("parent = " + parent);
var totalHrs = 0;
var grRegActTsk = new GlideRecord('sn_grc_reg_change_regulatory_action_task');

// Combine conditions using ^ for AND
grRegActTsk.addEncodedQuery("state=3^regulatory_task=" + parent);
grRegActTsk.query();

while (grRegActTsk.next()) {
    if (!isNaN(parseInt(grRegActTsk.u_actual_hours, 10))) {
        totalHrs += parseInt(grRegActTsk.u_actual_hours, 10);
    }
}