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.

how to add dynamic and static condition in addquery in a business rule

Fdeveloper
Kilo Guru

Hi,

i have this query ('sys_created_byLIKEcapit055^name!=DEFAULT^name!=saved_items^ORname=NULL');

i want to add this query in a business rule in addquery but the sys_id should be dynamic  and keep the rest of the query
i want to somtheing lik this :
var id = gs.getUserName();
gr.addQuery('sys_created=id^name!=DEFAULT^ORname=NULL^name!=saved_items^ORname=NULL');
any suggestion please to acheive that, thank you
 
 
1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Just concatenate the query string. BTW, shouldn't this be an .addEncodedQuery() instead of .addQuery()?

var id = gs.getUserName();
gr.addEncodedQuery('sys_created=' + id + '^name!=DEFAULT^ORname=NULL^name!=saved_items^ORname=NULL');

View solution in original post

2 REPLIES 2

Hitoshi Ozawa
Giga Sage
Giga Sage

Just concatenate the query string. BTW, shouldn't this be an .addEncodedQuery() instead of .addQuery()?

var id = gs.getUserName();
gr.addEncodedQuery('sys_created=' + id + '^name!=DEFAULT^ORname=NULL^name!=saved_items^ORname=NULL');

Community Alums
Not applicable

Hi @Fdeveloper ,

You may also find below link helpful for more info on how to write the GlideRecord query.

https://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/

 

Also, here is an example:

(function executeRule(current, previous /*null when async*/) {
  var gr = new GlideRecord("table2");
  gr.addQuery("u_field_b1",current.u_field_a1);
  gr.addQuery("u_field_b2",current.u_field_a2);
  gr.addQuery("u_field_b3","VALIE_b3");
  gr.query();
  if(gr.next()) {
     gr.u_field_b4=current.u_field_a3;
     gr.update();
  }
})(current, previous);

 

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep