Skip to content

Bulk Update Function - Use the code/function to bulk change some fiel… #1090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Background Scripts/Bulk Update Tables/BulkUpdateWithConditions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


/**
* Performs a bulk update on a specified table, applying the given data to all records that match the query.
*
* @param {string} table - The name of the table where the bulk update is to be performed.
* @param {string} query - The encoded query string that filters which records to update.
* @param {Object} data - An object representing the field-value pairs to update.
* Each key is a field name, and the value is the new value for that field.
*
* Example usage:
* bulkUpdate('incident', 'priority=1^state=2', { priority: 2, state: 3 });
*
* This updates all incidents where priority is 1 and state is 2, setting priority to 2 and state to 3.
*/
function bulkUpdate(table, query, data) {

var getRecord = new GlideRecord(table);
getRecord.addEncodedQuery(query);
getRecord.query();
while (getRecord.next()) {
for (var field in data) {
if (data.hasOwnProperty(field)) {
getRecord.setValue(field, data[field]);
}
}
getRecord.update();
}
}



19 changes: 19 additions & 0 deletions Background Scripts/Bulk Update Tables/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Bulk Update Function Documentation - Use the code/function to bulk change some fields in any tables.

## `bulkUpdate(table, query, data)`

Performs a bulk update on a specified table, applying the given data to all records that match the query.

### Parameters

- **`table`** (`string`): The name of the table where the bulk update is to be performed.
- **`query`** (`string`): The encoded query string that filters which records to update.
- **`data`** (`Object`): An object representing the field-value pairs to update.
- Each key is a field name, and the value is the new value for that field.

### Example Usage

```javascript
bulkUpdate('incident', 'priority=1^state=2', { priority: 2, state: 3 });
```

Loading