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

Hi @Meenal Gharat,

a small mistake in addAggregate() and getAggregate() functions. You need to pass second paramter as the field name on which you need to do aggregate o find count. 

addAggregate('COUNT', 'FIELD NAME');

getAggregate('COUNT', 'FIELD NAME'); 

Hope that Helps!

Thanks & Regards,

Sharjeel

Regards,
Muhammad

Yes, you are right for database operations GlideRecord has been used. Also, as @hozawa mentioned it is necessary to use GlideRecord for update(). 

By looking at your script I don't see the need for GlideAggregate. As you don't have any condition that is checking for COUNT etc except in the logs you are printing the count. In your case, you can achieve the update using GlideRecord without GlideAggregate.   

Regards,
Muhammad

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.