From 10298ff90fe41816b521c2b451d7c7921087e0fb Mon Sep 17 00:00:00 2001 From: Templeton Tsai Date: Wed, 2 Oct 2024 14:03:15 +1000 Subject: [PATCH] Bulk Create Function - Creates multiple records in specified tables based on provided data. --- .../BulkCreateRecordsMultipleTables.js | 43 +++++++++++++++++++ .../Readme.md | 20 +++++++++ 2 files changed, 63 insertions(+) create mode 100644 Background Scripts/Bulk Create Records in Multiple Tables/BulkCreateRecordsMultipleTables.js create mode 100644 Background Scripts/Bulk Create Records in Multiple Tables/Readme.md diff --git a/Background Scripts/Bulk Create Records in Multiple Tables/BulkCreateRecordsMultipleTables.js b/Background Scripts/Bulk Create Records in Multiple Tables/BulkCreateRecordsMultipleTables.js new file mode 100644 index 0000000000..1c0b3565de --- /dev/null +++ b/Background Scripts/Bulk Create Records in Multiple Tables/BulkCreateRecordsMultipleTables.js @@ -0,0 +1,43 @@ +/** + * Creates multiple records in specified tables based on provided data. + * + * @param {Object} target - An object where each key is the name of a table, + * and each value is an array of objects representing + * the records to be created. Each object in the array + * should contain field-value pairs for the respective table. + * + * Example usage: + * bulkCreateRecords({ + * 'incident': [ + * { short_description: 'Network issue', caller_id: '681ccaf9c0a8016401c5a33be04be441', priority: 2 }, + * { short_description: 'Email outage', caller_id: '681ccaf9c0a8016401c5a33be04be442', priority: 1 } + * ], + * 'change_request': [ + * { short_description: 'Server upgrade', assigned_to: '681ccaf9c0a8016401c5a33be04be443', state: 'new' } + * ] + * }); + * + * This creates two new records in the 'incident' table and one new record in the + * 'change_request' table with the specified field values. + */ + +function bulkCreateRecords(target) { + for (var table in target) { + if (target.hasOwnProperty(table)) { + var recordData = target[table]; + recordData.forEach(function(data) { + var gr = new GlideRecord(table); + gr.initialize(); + for (var field in data) { + if (data.hasOwnProperty(field)) { + gr.setValue(field, data[field]); + } + } + gr.insert(); + }); + } + } +} + + + diff --git a/Background Scripts/Bulk Create Records in Multiple Tables/Readme.md b/Background Scripts/Bulk Create Records in Multiple Tables/Readme.md new file mode 100644 index 0000000000..1495867a72 --- /dev/null +++ b/Background Scripts/Bulk Create Records in Multiple Tables/Readme.md @@ -0,0 +1,20 @@ +# Function: `bulkCreateRecords(target)` + +Creates multiple records in specified tables based on provided data. + +## Parameters + +- **`target`** (`Object`): An object where each key is the name of a table, and each value is an array of objects representing the records to be created. Each object in the array should contain field-value pairs for the respective table. + +## Example Usage + +```javascript +bulkCreateRecords({ + 'incident': [ + { short_description: 'Network issue', caller_id: '681ccaf9c0a8016401c5a33be04be441', priority: 2 }, + { short_description: 'Email outage', caller_id: '681ccaf9c0a8016401c5a33be04be442', priority: 1 } + ], + 'change_request': [ + { short_description: 'Server upgrade', assigned_to: '681ccaf9c0a8016401c5a33be04be443', state: 'new' } + ] +}); \ No newline at end of file