- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2020 01:46 PM
Hello experts,
Can someone please help me to understand if we can update the records using Glideaggreate?
As part of health scan servicenow suggested to use glideaggreate count instead of glideRecord.getRowCount();
While I was testing few script I came across that using glideaggreate count I was not able to update or delete the records
For eg : consider below business rule for example
var gr = new glideRecord('sc_task');
gr.addQuery('active',true);
gr.query();
var count=gr.getRowCount();
If(count >5)
gr.comment = "test";
gr.update();
Will I be able to achieve update /delete and count with glideaggregate?
Kindly suggest
Thank you.
BR,
MEENAL
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2020 03:23 PM
As is written in ServiceNow documentation, there's no update() method in GlideAggregate.
https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideAggregateScopedAPI#r_ScopedGlideAggregateNext
To update a record, it is necessary to use GlideRecord.
https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideRecordScopedAPI
It is, however, possible to use GlideAggregate to sum columns and then use GlideRecord to update a record.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2020 01:56 PM
Muhammad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2020 02:01 PM
I would highly recommend reading through this blog that has worthy details with example scripts
Understanding The GlideAggregate
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2020 02:15 PM
Hi muhammad,
before insert and update business rule
Updated the record on RITM
var num = current.number;
var reqNum = current.request_item;
if(current.comments.changes() && current.comments.indexOf("Comments from RITM") == -1){
var req_item = new GlideRecord("sc_req_item");
req_item.addQuery("sys_id", "=", reqNum);
req_item.query();
while(req_item.next())
{
gs.info("count"+req_item.getRowCount());
req_item.comments = "Comments from Task (" + current.number + "): " + current.comments;
req_item.update();
}
}
My replaced Code with no luck--- this gave me the count in logs but did not update the record on RITM.
var num = current.number;
var reqNum = current.request_item;
if(current.comments.changes() && current.comments.indexOf("Comments from RITM") == -1){
var req_item = new GlideAggregate("sc_req_item");
req_item.addQuery("sys_id", "=", reqNum);
req_item.addAggregate('COUNT');
req_item.query();
while(req_item.next())
{
gs.info("count"+req_item.getAggregate('COUNT'));
req_item.comments = "Comments from Task (" + current.number + "): " + current.comments;
req_item.update();
}
}
Thanks and Regards,
Meenal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2020 02:34 PM