From 87db9e89f7c4b69957b6fbb357f82bad4e4fe2ec Mon Sep 17 00:00:00 2001 From: Templeton Tsai Date: Wed, 2 Oct 2024 10:04:39 +1000 Subject: [PATCH 1/4] 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. --- .../BulkUpdateWithConditions.js | 32 +++++++++++++++++++ .../Bulk Update Tables/Readme.md | 19 +++++++++++ 2 files changed, 51 insertions(+) create mode 100644 Background Scripts/Bulk Update Tables/BulkUpdateWithConditions.js create mode 100644 Background Scripts/Bulk Update Tables/Readme.md 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 }); +``` + From b3f1588b0f423f21511ed112391a900c7815e059 Mon Sep 17 00:00:00 2001 From: Templeton Tsai Date: Wed, 2 Oct 2024 11:41:15 +1000 Subject: [PATCH 2/4] Bulk Delete Function - Use the code/function to bulk-deletes records from multiple tables based on provided encoded queries. --- ...leteRecordsMultipleTablesWithConditions.js | 34 +++++++++++++++++++ .../Readme.md | 20 +++++++++++ 2 files changed, 54 insertions(+) create mode 100644 Background Scripts/Bulk Delete Records in Multiple Tables with Conditions/BulkDeleteRecordsMultipleTablesWithConditions.js create mode 100644 Background Scripts/Bulk Delete Records in Multiple Tables with Conditions/Readme.md diff --git a/Background Scripts/Bulk Delete Records in Multiple Tables with Conditions/BulkDeleteRecordsMultipleTablesWithConditions.js b/Background Scripts/Bulk Delete Records in Multiple Tables with Conditions/BulkDeleteRecordsMultipleTablesWithConditions.js new file mode 100644 index 0000000000..87a3ff3008 --- /dev/null +++ b/Background Scripts/Bulk Delete Records in Multiple Tables with Conditions/BulkDeleteRecordsMultipleTablesWithConditions.js @@ -0,0 +1,34 @@ + + +/** + * Deletes records from multiple tables based on provided encoded queries. + * + * @param {Object} target - An object where each key is the name of a table and each value is an encoded query string. + * The function will delete all records matching the encoded query for each specified table. + * + * Example usage: + * bulkDelete({ + * 'incident': 'priority=1^state=2', + * 'change_request': 'state=3^risk=high' + * }); + * + * This deletes all records in the 'incident' table where the priority is 1 and the state is 2, + * and all records in the 'change_request' table where the state is 3 and risk is 'high'. + */ +function bulkDelete(target) { + + for (var table in target) { + if (target.hasOwnProperty(table)) { + var getRecord = new GlideRecord(table); + getRecord.addEncodedQuery(target[table]); + getRecord.query(); + while (getRecord.next()) { + + getRecord.deleteRecord(); + } + } + } +} + + + diff --git a/Background Scripts/Bulk Delete Records in Multiple Tables with Conditions/Readme.md b/Background Scripts/Bulk Delete Records in Multiple Tables with Conditions/Readme.md new file mode 100644 index 0000000000..71db1c95b8 --- /dev/null +++ b/Background Scripts/Bulk Delete Records in Multiple Tables with Conditions/Readme.md @@ -0,0 +1,20 @@ +# Bulk Delete Function Documentation - Use the code/function to bulk-deletes records from multiple tables based on provided encoded queries. + +# Function: `bulkDelete(target)` + +Deletes records from multiple tables based on provided encoded queries. + +## Parameters + +- **`target`** (`Object`): An object where each key is the name of a table, and each value is an encoded query string. + - The function will delete all records matching the encoded query for each specified table. + +## Example Usage + +```javascript +bulkDelete({ + 'incident': 'priority=1^state=2', + 'change_request': 'state=3^risk=high' +}); +``` + From 226dcaa38339b534a48c290c85493b5f913a6a4a Mon Sep 17 00:00:00 2001 From: Carlos Camacho Junior <56931121+kmxo@users.noreply.github.com> Date: Tue, 1 Oct 2024 23:04:47 -0300 Subject: [PATCH 3/4] Delete Background Scripts/Bulk Update Tables/Readme.md File already sent --- .../Bulk Update Tables/Readme.md | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 Background Scripts/Bulk Update Tables/Readme.md diff --git a/Background Scripts/Bulk Update Tables/Readme.md b/Background Scripts/Bulk Update Tables/Readme.md deleted file mode 100644 index 6c8649c8b3..0000000000 --- a/Background Scripts/Bulk Update Tables/Readme.md +++ /dev/null @@ -1,19 +0,0 @@ -# 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 }); -``` - From d9360e4fa823f2879b1bf92588651a589d2496b0 Mon Sep 17 00:00:00 2001 From: Carlos Camacho Junior <56931121+kmxo@users.noreply.github.com> Date: Tue, 1 Oct 2024 23:06:00 -0300 Subject: [PATCH 4/4] Delete Background Scripts/Bulk Update Tables/BulkUpdateWithConditions.js File already sent --- .../BulkUpdateWithConditions.js | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 Background Scripts/Bulk Update Tables/BulkUpdateWithConditions.js diff --git a/Background Scripts/Bulk Update Tables/BulkUpdateWithConditions.js b/Background Scripts/Bulk Update Tables/BulkUpdateWithConditions.js deleted file mode 100644 index a2d54f35bc..0000000000 --- a/Background Scripts/Bulk Update Tables/BulkUpdateWithConditions.js +++ /dev/null @@ -1,32 +0,0 @@ - - -/** - * 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(); - } -} - - -