Dotwalking in an addQuery in GlideRecord on [alm_asset] table not working?

patricklatella
Mega Sage

Hi all,

I'm trying to use a single GlideRecord to fetch [alm_asset] records for a user that have a "ci" record with the "form_factor" field that = "Laptop".  I'd like to do this in one GlideRecord as opposed to finding all the asset records for the user, and then running a 2nd GlideRecord on the [cmdb_ci_computer] table to check for the ones where Form Factor = "Laptop".  But it's not working as I would expect...is there something special about [alm_asset] that prevents this?  Here's the script that doesn't work, and below is the nested GlideRecord that does work, but isn't the way I'd like to do it.  Anyone see my issue?  thanks!

This doesn't work.  Joe has 2 assets, neither is laptop, but the gs.print as shown below:

var comp = new GlideRecord('alm_asset');
comp.addQuery('assigned_to', '06186449dbb3ab40d22664904b9619ad');//my user Joe
comp.addQuery('ci.form_factor', 'Laptop');//appears to skip this line entirely...trying to dotwalk here
comp.query();
while (comp.next()) 
{
gs.print('Form factor is '+comp.ci.form_factor);
}

Prints:

*** Script: Form factor is Tablet
*** Script: Form factor is Desktop

_____________________________________________________

This does work...Joe has 2 assets, but neither is a laptop...so the gs.print shows "Joe doesn't have a laptop" twice, which is correct (shown below).  But I don't want to use this nested GlideRecord.  Is it actually ok to do it this way?

var comp = new GlideRecord('alm_asset');
comp.addQuery('assigned_to', '06186449dbb3ab40d22664904b9619ad');//my test user Joe
comp.query();
while (comp.next())
{
var formFactor = new GlideRecord('cmdb_ci_computer');
formFactor.addQuery('asset', comp.sys_id);
formFactor.addQuery('form_factor', 'Laptop');
formFactor.query();
if(formFactor.next()){
gs.print('Joe has a laptop');
}else{
gs.print("Joe doesn't have a laptop");
}
}

Prints:

*** Script: Joe doesn't have a laptop
*** Script: Joe doesn't have a laptop
1 ACCEPTED SOLUTION

alm_asset.ci is a ref to cmdb_ci rather than cmdb_ci_computer.

if you look directly on cmdb_ci there is no form_factor field, it is on cmdb_ci_computer.

form factor is on the extended table so cannot be referenced as part of the parent.

that is why the dot walk does not work.

if you run in fix script you will see:

var comp = new GlideRecord('alm_asset');
comp.query();
while (comp.next())
{
gs.print('Form factor is '+comp.ci.form_factor);
}

View solution in original post

8 REPLIES 8

Nested Glide Records are not recommend since it's likely to impact your instance performance.

 

Check below for better ways to nesting glide record query

 

https://codecreative.io/blog/3-strategies-to-fix-nested-gliderecords/

 

Regards,

Sachin

 

 

Thanks Sachin,

that's my understanding...I think in this case the performance impact will be minimal, but I'll check out the link.

Community Alums
Not applicable

Hi Patrick,

 

Can you check  the values again in table. Also, try updating the code

var comp = new GlideRecord('alm_asset');
comp.addQuery('assigned_to', '06186449dbb3ab40d22664904b9619ad');//my test user Joe
comp.query();
while (comp.next())
{
var formFactor = new GlideRecord('cmdb_ci_computer');
formFactor.addQuery('asset', comp.sys_id);
formFactor.addQuery('form_factor', 'Laptop');
formFactor.query();
//formFactor.next();
var count = formFactor.getRowCount();
for(i=0; i< count ; i++)){
gs.print('Joe has a laptop');
}
}

 

Let me know in case of any issues.

 

Please mark this comment as Helpful/Correct Answer if it helped you.

Cheers,

Hardit Singh