Skip to content

Bulk Create Function - Creates multiple records in specified tables b… #1092

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
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
Original file line number Diff line number Diff line change
@@ -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();
});
}
}
}



Original file line number Diff line number Diff line change
@@ -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' }
]
});
Loading