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.

Can we use variable in EncodedQuery

vamsee1
Mega Contributor

Hi,

Can we use variable in Encoded query, I am trying to assign a value to variable and use it in the encoded query it doesn't seems to be working. Can any one let me know how to do this. Below is the code, I wanted to replace the sysid highlighted with the variable X declared.

var a = new GlideRecord('u_backup_job');
a.addEncodedQuery("u_backup_system=Idera^u_status=failed^sys_created_onBETWEENjavasc#ipt:gs.hoursAgo(3)@javasc#ipt:gs.minutesAgoStart(1)");

a.query();

while(a.next())

  {

    var x = a.u_client_host;

    var b = new GlideRecord('u_mon_hosts');

    b.addEncodedQuery("u_active=true^u_host=9a5fdfe5301ad54c915a54999ba031b3^u_service_check.u_display_name=MSSQLSERVER");
  b.query();

    if(b.next())

  {

      gs.log("Create ticket");

  }}

Thanks,

Krishna.

1 ACCEPTED SOLUTION

edwin_munoz
Mega Guru

Hi Krishna,



Yes you can use a variable, you need to use the concatenation operator "+":



b.addEncodedQuery("u_active=true^u_host=" + x + "^u_service_check.u_display_name=MSSQLSERVER");



Please read the following link for more information regarding the concatenation operator:


Arithmetic operators - JavaScript | MDN



Thank you.


View solution in original post

2 REPLIES 2

edwin_munoz
Mega Guru

Hi Krishna,



Yes you can use a variable, you need to use the concatenation operator "+":



b.addEncodedQuery("u_active=true^u_host=" + x + "^u_service_check.u_display_name=MSSQLSERVER");



Please read the following link for more information regarding the concatenation operator:


Arithmetic operators - JavaScript | MDN



Thank you.


Thanks Edwin..