- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 06:30 PM
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
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 08:24 PM
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
Thanks
Anil Lande

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 06:50 PM
Hi,
Model Category field is reference field, you need to provide sys_id of category in gliderecord query.
var gr1 = new GlideRecord('alm_hardware');
gr1.addEncodedQuery('model_category', <sys_id of category>);
gr1.query();
if (!gr1.next()) {
gs.log("modelcategory is " + category);
return "Model category is not existing in the system";
}
Thanks,
Anil Lande
Thanks
Anil Lande

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 06:52 PM
If you want to use name based query then you can try below:
var gr1 = new GlideRecord('alm_hardware');
gr1.addEncodedQuery('model_category.name', category); // use dot walking in query
gr1.query();
if (!gr1.next()) {
gs.log("modelcategory is " + category);
return "Model category is not existing in the system";
}
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 07:14 PM
Thanks Anil,
I have tried below code, It is not working .. can you please help??
var gr1 = new GlideRecord('alm_hardware');
gr1.addEncodedQuery('model_category.name', category);
gr1.query();
if (!gr1.next()) {
gs.log("modelcategory is " + category);
return "Model category is not existing in the system";

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 07:22 PM
Hi,
Please check below, this worked fine for me:
var gr1 = new GlideRecord('alm_hardware');
gr1.addQuery('model_category.name', 'Computer');
gr1.query();
if (gr1.next()) {
gs.info("modelcategory is " + gr1.model_category.getDisplayValue());
}else{
gs.info( "Model category is not existing in the system");
}
Use addQuery instead of addEncodedQuery.
Thanks,
Anil Lande
Thanks
Anil Lande