How to Glide Record the model category field in the alm_hardware table

Manasa23
Tera Contributor

HI,

I'm trying to GlideRecord the model category field in the  alm_hardware table, below is the code. Code is not working..
can someone help me with the correct code.

I tried to glide record the  Asset tag, serial number  and other  fields in alm_hardware using the same code, its working fine !!!

var gr1 = new GlideRecord('alm_hardware');
gr1.addEncodedQuery('model_category', category);
gr1.query();
if (!gr1.next()) {
gs.log("modelcategory is " + category);
 return "Model category is not existing in the system";

}

Thanks,
M

1 ACCEPTED SOLUTION

Have you defined category variable in your script:

like below:

var category = 'Computer';
var gr1 = new GlideRecord('alm_hardware');
gr1.addQuery('model_category.name', category); 
gr1.query();
if (!gr1.next()) {
gs.print("model category is not exisit");
}

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

12 REPLIES 12

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

what debugging have you performed by adding logs etc?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Manasa23
Tera Contributor

HI Ankur,

 

Ran the below in background  script 

var gr1 = new GlideRecord('alm_hardware');
gr1.addEncodedQuery('model_category.name', "Computer");
gr1.query();
if (!gr1.next()) {
gs.print("model category is not exisit");
}

find_real_file.png

Below is correct way when you want to use gr1.addEncodedQuery();

var gr1 = new GlideRecord('alm_hardware');
gr1.addEncodedQuery('model_category.name='+'Computer');
gr1.query();
if (gr1.next()) {

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi,

update query as this

var gr1 = new GlideRecord('alm_hardware');
gr1.addQuery('model_category.name', "Computer");
gr1.query();
if (!gr1.next()) {
gs.print("model category is not exisit");
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Manasa23
Tera Contributor

HI Ankur,

When i pass the direct string in add query, it is working fine.

when I try to addQuery  with a variable, it is not working ...


var gr1 = new GlideRecord('alm_hardware');
gr1.addQuery('model_category.name', category); 
gr1.query();
if (!gr1.next()) {
gs.print("model category is not exisit");
}