summaryrefslogtreecommitdiff
path: root/content/posts/get_changed_fields_in_server_scripts_in_servicenow.md
blob: 724263954cbaaaeaac9d30dcc989844fe8c15772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
---
title: "Get changed fields in server scripts in ServiceNow"
description: "In ServiceNow you often write Business Rules or some other logic, based on fields that has been changed/updated. For the most part, this can be done via GUI, but sometimes you have to resort to some scripting."
date: 2021-08-17T10:04:06+02:00
tags: ['servicenow', 'javascript']
draft: false
---

In ServiceNow you often write Business Rules or some other logic, based on fields that has been changed/updated. For the most part, this can be done via GUI, but sometimes you have to resort to some scripting. If you ever need to get which fields has been changed/updated, e.g in an advanced filter, this is how you check for it.

<!--more-->

```javascript
(function(current){
    var gru = GlideScriptRecordUtil.get(current);

    // Returns an arrayList of changed field elements with friendly names
    var changedFields = gru.getChangedFields();

    //Returns an arrayList of changed field elements with database names
    var changedFieldNames = gru.getChangedFieldNames();

    //Returns an arrayList of all change values from changed fields
    var changes = gru.getChanges();

    // Convert to JavaScript Arrays
    gs.include('j2js');
    changedFields = j2js(changedFields);
    changedFieldNames = j2js(changedFieldNames);
    changeds = j2js(changes);

    gs.info("Changed Fields: " + JSON.stringify(changedFields));
    gs.info("Changed Field Names: " + JSON.stringify(changedFieldNames));
    gs.info("Changes: " + JSON.stringify(changes));
})(current);
```