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

Harish KM
Kilo Patron
Kilo Patron

Hi @sayalisheth please find below code

 

var parent = current.regulatory_task;
var grRegActTsk = new GlideRecord('sn_grc_reg_change_regulatory_action_task');
grRegActTsk.addEncodedQuery("state=3^regulatory_task="+parent);
grRegActTsk.query();
Regards
Harish

as soon as I add the encoded query, I don't get any records. I should get 2. If I run the query separately, I do get some records. What else could be the issue?

 

 var parent = current.regulatory_task;
    gs.addInfoMessage("parent = " + parent);
    var totalHrs = 0;
    var grRegActTsk = new GlideRecord('sn_grc_reg_change_regulatory_action_task');
    //grRegActTsk.addQuery('state','3');
//grRegActTsk.addQuery('regulatory_task',parent);
    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);
        }
    }

Nick Parsons
Mega Sage

You need to use string concatenation:

 

grRegActTsk.addEncodedQuery("state=3^regulatory_task=" + parent);

 

This will use the value in the variable parent and "glue"/"join" it together with the string. See the aforementioned link for details.

AshishKM
Kilo Patron
Kilo Patron

Hi @sayalisheth

Use the parent variable in the query as below 

grRegActTsk.addEncodedQuery("state=3^regulatory_task="+parent);

 

-Thanks

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution