Why is setLimit() ignored in this GlideRecord query?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2022 08:42 AM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2022 08:55 AM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2022 07:07 AM
Hello,
Not sure if you've seen my reply above as you're responding to others, but please review and consider adjusting. Thanks!
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2022 08:57 AM
Hi,
Your code looks like it should be working.
Perhaps use a for loop instead of a while loop.
Thanks
Jake

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2022 09:18 AM
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?