Skip to content

Commit 3aa97b0

Browse files
authored
Correct Readme files added according to the scripts (#2381)
1 parent 27f1db0 commit 3aa97b0

File tree

5 files changed

+117
-43
lines changed

5 files changed

+117
-43
lines changed
Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,36 @@
1-
Auto-Populate Short Description (Client Script)
2-
Overview
1+
# Auto-Populate Short Description (Client Script)
2+
## Overview
33

44
This client script automatically updates the Short Description field in ServiceNow whenever a user selects a category on the form. It improves data consistency, saves time, and ensures that short descriptions remain meaningful and standardized.
55

6-
How It Works
6+
## How It Works
77

88
When a user selects a category such as Hardware, Software, or Network, the script automatically prefixes the existing short description with a corresponding label (for example, “Hardware Issue –”).
99
This makes incident records easier to identify and improves the quality of data entry.
1010

11-
Configuration Steps
11+
## Configuration Steps
1212

13-
Log in to your ServiceNow instance with admin or developer access.
14-
15-
Navigate to System Definition → Client Scripts.
16-
17-
Create a new Client Script with the following details:
13+
1. Log in to your ServiceNow instance with admin or developer access.
14+
2. Navigate to System Definition → Client Scripts.
15+
3. Create a new Client Script with the following details:
1816

17+
```
1918
Table: Incident
20-
2119
Type: onChange
22-
2320
Field name: category
24-
2521
Copy and paste the provided script into the Script field.
26-
2722
Save the record and make sure the script is active.
23+
```
2824

29-
Testing
25+
## Testing
3026

31-
Open any existing or new Incident record.
27+
1. Open any existing or new Incident record.
28+
2. Select a category such as Hardware or Software.
29+
3. The Short Description field will automatically update with the corresponding prefix.
3230

33-
Select a category such as Hardware or Software.
34-
35-
The Short Description field will automatically update with the corresponding prefix.
36-
37-
Benefits
31+
## Benefits
3832

3933
Speeds up data entry for users.
40-
4134
Maintains consistency in short descriptions.
42-
4335
Reduces manual effort and human errors.
44-
4536
Enhances clarity in incident listings and reports.
Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,100 @@
1-
Convert Active Incidents to JSON – ServiceNow
2-
Overview
1+
# Active Incidents JSON Export – ServiceNow
32

4-
This script fetches all active Incident records from your ServiceNow instance and converts them into a JSON format. The JSON output is easy to read and can be used for reporting, integration, or debugging purposes.
3+
This repository contains **two approaches** to fetch active incidents from ServiceNow and convert them to JSON. Both use `GlideRecord` but differ in flexibility and readability.
54

6-
By converting records to JSON, you can quickly share structured data with external systems, automate processes, or use it in scripts and dashboards.
5+
---
76

8-
Features
7+
## 1. Simple Approach
98

10-
Retrieves all active incidents from the incident table.
9+
This method fetches a **fixed set of fields** and converts them directly to JSON.
1110

12-
Dynamically extracts selected fields (configurable).
11+
```javascript
12+
var incidents = [];
13+
var gr = new GlideRecord('incident');
14+
gr.addQuery('active', true);
15+
gr.query();
16+
while (gr.next()) {
17+
incidents.push({
18+
number: gr.number.toString(),
19+
short_description: gr.short_description.toString(),
20+
state: gr.state.toString(),
21+
assigned_to: gr.assigned_to.getDisplayValue('name'),
22+
created_on: gr.sys_created_on.getDisplayValue()
23+
});
24+
}
1325

14-
Automatically resolves reference fields to display values.
26+
var jsonOutput = JSON.stringify(incidents);
27+
gs.info(jsonOutput);
28+
```
1529

16-
Adds human-readable state labels (e.g., "New", "In Progress", "Resolved").
30+
### ✅ Pros
31+
- Simple and easy to implement
32+
- Works fine for a fixed set of fields
33+
- Direct JSON output
1734

18-
Outputs pretty-printed JSON for easy readability.
35+
### ❌ Cons
36+
- Fields are hard-coded → not reusable for other tables
37+
- Reference fields handling is not dynamic
38+
- Numeric state values are not human-readable
1939

20-
Configuration
40+
---
2141

22-
Table Name: Set the tableName variable to the table you want to extract records from.
42+
## 2. Flexible & Dynamic Approach
2343

24-
Fields to Include: Update the fieldsToInclude array to include the fields you need in the JSON. For example:
44+
This method allows dynamic fields, handles reference fields, and adds human-readable state labels.
45+
46+
```javascript
47+
var tableName = 'incident';
48+
var fieldsToInclude = ['number', 'short_description', 'state', 'assigned_to', 'sys_created_on'];
49+
var incidentList = [];
50+
51+
var gr = new GlideRecord(tableName);
52+
gr.addQuery('active', true);
53+
gr.query();
54+
55+
while (gr.next()) {
56+
var incidentObj = {};
57+
58+
fieldsToInclude.forEach(function(field) {
59+
if (gr.isValidField(field) && gr[field].getRefRecord) {
60+
incidentObj[field] = gr[field].getDisplayValue();
61+
} else if (gr.isValidField(field)) {
62+
incidentObj[field] = gr[field].toString();
63+
} else {
64+
incidentObj[field] = null;
65+
}
66+
});
67+
68+
// Map numeric state to human-readable label
69+
var stateMap = {
70+
'1': 'New',
71+
'2': 'In Progress',
72+
'3': 'On Hold',
73+
'6': 'Resolved',
74+
'7': 'Closed'
75+
};
76+
incidentObj.state_label = stateMap[incidentObj.state] || 'Unknown';
77+
78+
incidentList.push(incidentObj);
79+
}
80+
81+
var jsonOutput = JSON.stringify(incidentList, null, 2);
82+
gs.info("Here's your JSON for active incidents:\n" + jsonOutput);
83+
```
84+
85+
### ✅ Pros
86+
- Dynamic → easily reusable for any table and fields
87+
- Handles reference fields gracefully
88+
- Adds human-readable state labels
89+
- JSON is formatted for readability
90+
- Checks `isValidField` to prevent errors
91+
92+
### ❌ Cons
93+
- Slightly more complex than the simple version
94+
- Requires manual mapping for fields like state labels
95+
96+
97+
## Summary
98+
99+
- **Simple Approach**: Best for quick tasks with fixed fields
100+
- **Flexible Approach**: Best for reusable scripts, handling dynamic tables, reference fields, and human-readable output
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
*************
2-
This script will query the group table and look for groups with inactive members. The script will replace the inactive manager with the oldest active member of the group.
1+
## update Multiple Records
2+
These 2 scripts are used to update Multiple records but in different ways
3+
### update_multiple_records.script file
4+
In this script it uses `updateMultiple()` Function
5+
1. In this script it uses `updateMultiple()` Function
6+
2. Automatically updates system fields like sys_updated_by and sys_updated_on.
7+
3. Cannot skip system fields using autoSysFields(false).
8+
4. Always triggers workflows, business rules, and script actions.
9+
5. Cannot disable per record.
310

4-
Logs: If no active members are there in group //gs.info("Group " + grp.name + " does not have any active user");
5-
6-
After manager is replaced : gs.info("Group " + inactiveMgrGrp.name + " manager changed to " + getOlderGroupMember(inactiveMgrGrp).name);
7-
8-
*************
11+
### update_multiple_records_v2.script file
12+
In this script it uses `update()` Function
13+
1. update() is a GlideRecord method used to update a single record in the database. It is commonly used inside a loop to update multiple records individually.
14+
2. Can skip updating system fields using autoSysFields(false).
15+
3. Can skip workflows/business rules using setWorkflow(false).

0 commit comments

Comments
 (0)