diff --git a/Background Scripts/Bulk Update Tables/BulkUpdateWithConditions.js b/Background Scripts/Bulk Update Tables/BulkUpdateWithConditions.js new file mode 100644 index 0000000000..a2d54f35bc --- /dev/null +++ b/Background Scripts/Bulk Update Tables/BulkUpdateWithConditions.js @@ -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(); + } +} + + + diff --git a/Background Scripts/Bulk Update Tables/Readme.md b/Background Scripts/Bulk Update Tables/Readme.md new file mode 100644 index 0000000000..6c8649c8b3 --- /dev/null +++ b/Background Scripts/Bulk Update Tables/Readme.md @@ -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 }); +``` +