How to avoid two gliderecord queries for same table.

saibharath
Tera Contributor

How to avoid duplicate gliderecord queries in this case

  • obj is an object on hardware table
  •  assuming country is 'spain' and model is 'laptop' and this value is stored in obj object which I received from bulk import.

 

 

var gr = new GlideRecord("asset_tag_rule");
gr.addEncodedQuery("country=" + obj.country + "^model=" + obj.mdl); // we are checking in asset_tag_rule table if spain country and laptop model is there.
gr.query(); // querying and assume I get one record

if (gr.next()) {
var grINCR = new GlideRecord("asset_tag_rule");// how to avoid this step
grINCR.addEncodedQuery("country=" + gr.country + "^u_ctgry_prfx=" + gr.u_ctgry_prfx);// here I am checking asset_tag_rule table if spain country and ctgry_prfx value is present in this table which we get from first glide object(gr)
grINCR.query();
while (grINCR.next()) {
grINCR.net = 3;
grINCR.update();
}
}

 

 

 

  • I can have multiple values of u_ctgry_prfx (which I get from gr) and country combination from asset_tag_rule table hence used  while in the end to update.

@Ankur Bawiskar  or anyone pls suggest a way

6 REPLIES 6

Hello @saibharath 

 

Before moving ahead plz explain your requirement in detail.

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

 

Vishal Birajdar
Giga Sage

Hi @saibharath 

 

Actually , I have cross checked in GlideRecord API & there is no method to remove existing query & add new one in object.

 

Below is workaround , can you try & check if its work: 

 

var gr = new GlideRecord("asset_tag_rule");
gr.addEncodedQuery("country=" + obj.country + "^model=" + obj.mdl); 
gr.query(); 

var firstQuery = gr.getEncodedQuery();
var joinQuery = "^country!=" + obj.country + "^model!=" + obj.mdl ; //this is to cancel out the first query
//gs.info(joinQuery) ;
if (gr.next()) {
var newQuery = joinQuery + "^NQcountry=" + gr.country + "^u_ctgry_prfx=" + gr.u_ctgry_prfx;  // add New OR query 
gr.addEncodedQuery(newQuery);
//gs.info(gr.getEncodedQuery());  
gr.query();
while (gr.next()) {
gr.net = 3;
gr.update();
}
}

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates