Using GlideQuery() to query a table and return values

jhuggins
Tera Contributor

Hello. We currently have a requirement that requesters select a hardware asset in a reference list on a catalog item. At a certain point in the flow, that catalog item and other information is passed to a script include. The script include should receive the passed information, query the hardware asset table to make sure the selected asset is assigned to the requested for of the request, and return the result.

I am using the GlideQuery() API and have followed the documentation as far as building the query and using correct syntax in the script include [1]. However, my query is not returning any values as we can see in our log files [2][3], despite the correct asset sys_id and requested for information being passed in. 

 

Any help is appreciated to figure out why the query is not returning any results. I have tried just using the first .where condition to match the sys_id of the asset record to the sys_id of the passed in asset, but am receiving the same results.

 

Thanks in advance

 

[1]

var UnassignAssetFromReqFor = Class.create();

var removeAsset;
var assigned_to;

UnassignAssetFromReqFor.prototype = {
    initialize: function() {
    },
	
	unassignAsset: function(removeAsset, assigned_to) {
		gs.log('Asset: ' + removeAsset); // verify asset sys_id passed correctly
		gs.log('Assigned to: ' + assigned_to.getDisplayValue()); // verify requested for of request is passed correctly
		
		var asset = new global.GlideQuery('alm_hardware') // initialize query
			.where('sys_id', removeAsset) // only query items where sys_id is the same as the passed asset
			.where(new GlideQuery('alm_hardware') // AND 
				.where('assigned_to', assigned_to) // the assigned to of the asset is the same as the passed requested for
 				.orWhere('parent', removeAsset)) // OR the parent of the asset is the same as the passed asset
 			.select('u_asset_number', 'asset_tag', 'parent')
			.toArray(100);

		gs.info(JSON.stringify(asset, null, 2)); // verify query results
		
		gs.log('these are the assets: ' + asset); // verify query results

        },
	
	type: 'UnassignAssetFromReqFor'
};

 

[2]

output of 'gs.info(JSON.stringify(asset, null, 2));' is []

[3]

these are the assets: (this part is empty as there are no results found)

 

1 ACCEPTED SOLUTION

Hi,

Ok. I said that because you have a global.GlideQuery in there.

Past all of that, you'd want to check your values for what you're passing as function params. With logging, what are you getting for removedAsset and do you need to add .toString() to ensure it's in appropriate format?

The whole thing with GlideQuery is that it's meant to totally fail if a certain piece is not correct. GlideRecord, would just ignore it.

So something must be wrong with perhaps the params (if you've temp removed the nested GlideQuery).

Like I've used just this...

var asset = new global.GlideQuery('alm_hardware') // initialize query
.where('sys_id', '00a96c0d3790200044e0bfc8bcbe5dc3')
.select('asset_tag', 'parent')
.toArray(5);
gs.info(JSON.stringify(asset));

and I get a result like this:

find_real_file.png

Obviously I don't have your custom field so I removed it, but the point here is that it works.

So as we narrow things down, it comes down to either:

  1. removedAsset isn't anything helping the query actually happen or isn't in the right format that GlideQuery expects (a string sys_id in your case).
  2. The custom field is perhaps named wrong (although the query in background "worked"...did something, it threw errors and wasn't a clean array -- so I'm listing that here as a probably cause).

I'd recommend checking those two things 🙂

Please mark reply as Helpful/Correct, if applicable. Thanks!

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

9 REPLIES 9

Allen Andreas
Administrator
Administrator

Hi,

Would you need to use the .forEach (for multiple) or .selectOne / .ifPresent (for single).

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Allen Andreas
Administrator
Administrator

Hi,

Is this missing a second 😞

.where(new GlideQuery('alm_hardware') // AND 

Not a big user of GlideQuery (and using a nested GlideQuery inside as well), but that's something that stood out.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thanks for the response. The closing ) is 2 lines down to close off the nested GlideQuery. I got this technique from the doc site

.orWhere('parent', removeAsset))

Ah, understood. I wasn't sure if then that was in the wrong place too, haha.

Anyways, if you removed the nested GlideQuery, does it work then?

Just curious if adding that part on is causing any issue.

At a minimum if anything was in the array, even without stringifying it, it would show as [object object],[object object], etc. So the fact that both of your logging isn't showing anything makes me think you could try adjusting some of your query.

Also, if this is for a scoped app, gs.log is not appropriate to use as it won't work and has been known to actually cause code to have issues. gs.info (as you used in the line above) is appropriate and recommended to get into muscle memory to always use.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!