Skip to content

Commit aacbf5f

Browse files
templetontsaiTempleton Tsai
andauthored
Bulk Update Function - Use the code/function to bulk change some fields in any tables in your background script and in any other serverside scripts. (#1090)
Co-authored-by: Templeton Tsai <[email protected]>
1 parent 9825f35 commit aacbf5f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
/**
4+
* Performs a bulk update on a specified table, applying the given data to all records that match the query.
5+
*
6+
* @param {string} table - The name of the table where the bulk update is to be performed.
7+
* @param {string} query - The encoded query string that filters which records to update.
8+
* @param {Object} data - An object representing the field-value pairs to update.
9+
* Each key is a field name, and the value is the new value for that field.
10+
*
11+
* Example usage:
12+
* bulkUpdate('incident', 'priority=1^state=2', { priority: 2, state: 3 });
13+
*
14+
* This updates all incidents where priority is 1 and state is 2, setting priority to 2 and state to 3.
15+
*/
16+
function bulkUpdate(table, query, data) {
17+
18+
var getRecord = new GlideRecord(table);
19+
getRecord.addEncodedQuery(query);
20+
getRecord.query();
21+
while (getRecord.next()) {
22+
for (var field in data) {
23+
if (data.hasOwnProperty(field)) {
24+
getRecord.setValue(field, data[field]);
25+
}
26+
}
27+
getRecord.update();
28+
}
29+
}
30+
31+
32+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Bulk Update Function Documentation - Use the code/function to bulk change some fields in any tables.
2+
3+
## `bulkUpdate(table, query, data)`
4+
5+
Performs a bulk update on a specified table, applying the given data to all records that match the query.
6+
7+
### Parameters
8+
9+
- **`table`** (`string`): The name of the table where the bulk update is to be performed.
10+
- **`query`** (`string`): The encoded query string that filters which records to update.
11+
- **`data`** (`Object`): An object representing the field-value pairs to update.
12+
- Each key is a field name, and the value is the new value for that field.
13+
14+
### Example Usage
15+
16+
```javascript
17+
bulkUpdate('incident', 'priority=1^state=2', { priority: 2, state: 3 });
18+
```
19+

0 commit comments

Comments
 (0)