How to calculate total sum of a field (salary) in ServiceNow?

ny424436
Kilo Explorer

Hi everyone,

I am trying to calculate the total sum of a salary field from a custom table in ServiceNow.

The field name is u_salary, and I initially tried using GlideAggregate with SUM, but the result is not coming correctly because the values sometimes contain commas (for example 1,000 or 10,000).

I want to understand:

  1. What is the best way to calculate the total sum of a field when the data may contain formatted values like commas?
  2. Should I clean or convert the data first using GlideRecord and then calculate the sum manually?
  3. Is there any way to handle this directly using GlideAggregate?

Any guidance or best practices would be really helpful.

Thanks in advance.

1 REPLY 1

Huynh Loc
Mega Sage

Hi @ny424436 ,

If the u_salary field contains formatted values with commas, GlideAggregate will not work correctly because it expects a numeric data type.

Best practice is to store salary values as a Number or Decimal field without commas. If possible, clean the existing data and update the field type.

If you cannot change the data immediately, use GlideRecord, remove commas (for example using replace(',', '')), convert the value to a number, and then calculate the sum manually in a script.

There is no supported way to handle comma-formatted values directly using GlideAggregate.

Recommendation: Fix the data model first (numeric field), then use GlideAggregate for accurate and efficient summation.

If this response was helpful, please consider marking it as Correct and Helpful. You may mark more than one reply as an accepted solution.