Can we update records with glideaggregate?

Meenal Gharat
Giga Guru

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

 

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

View solution in original post

7 REPLIES 7

MrMuhammad
Giga Sage
Yes, you can perform database operations with GliAggregate i.e. update, delete etc. Can you share your glideAggregate code that you tested with no luck?
Regards,
Muhammad

MrMuhammad
Giga Sage

I would highly recommend reading through this blog that has worthy details with example scripts

Understanding The GlideAggregate

 

Regards,
Muhammad

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

Hello Muhammad, I went through this document I noticed to insert records they're using gliderecord. Any suggestions or thoughts if this is achievable? Thanks and Regards, Meenal