Why is setLimit() ignored in this GlideRecord query?

Igor __tek
Tera Contributor

Hi people,

I had this simple script running in UI Action on the Part Requirement form. For some reason even if use setLimit() with hardcoded value or variable it is ignored and Query always updates all records returned from addEncodedQuery.

var prModel = current.model.sys_id;
var currentPrId = current.sys_id;
var prQuantity = parseInt(current.required_quantity);
var parentSOT = current.service_order_task.sys_id;
var parentWOT = "";
var grWOT = new GlideRecord('wm_task');
if (grWOT.get(parentSOT)) {
	parentWOT = grWOT;
}
var parentStockroom = parentWOT.u_destination_stockroom.sys_id;
var query = "substatus=available";
	query += "^stockroom=" + parentStockroom; 
	query += "^model=" + prModel; 
var grAsset = new GlideRecord('alm_asset');
	grAsset.addEncodedQuery(query);
	grAsset.setLimit(1); // .setLimit(prQuantity);
	grAsset.query();
while (grAsset.next()) {
	grAsset.u_part_requirement.setValue(currentPrId);
	grAsset.install_status.setValue("6");
	grAsset.substatus.setValue("reserved");
	grAsset.update();
}
7 REPLIES 7

Allen Andreas
Administrator
Administrator

Hi,

It should be working. Can you fix formatting here though:

grAsset.setValue('u_part_requirement', currentPrId);
grAsset.setValue('install_status', "6");
grAsset.setValue('substatus', "reserved");

If you know you'll be using setLimit(1) anyway, you should change from "while" loop to "if".

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


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

Hello,

Not sure if you've seen my reply above as you're responding to others, but please review and consider adjusting. Thanks!

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server/no-namespace/c_GlideRecordSc...

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


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

Jake Sadler
Kilo Sage

Hi,

 

Your code looks like it should be working.

 

Perhaps use a for loop instead of a while loop.

 

Thanks

 

Jake

Chuck Tomasi
Tera Patron

As a debug measure, put this after your query() call...

gs.info('query=' + grAsset.getEncodedQuery());

Check the log and make sure the query is what you think it is. Remember, unless you specify otherwise, invalid bits of the query are ignored.

Also, have you tested that encoded query against a list in the browser?