Skip to content

Commit 10298ff

Browse files
Templeton TsaiTempleton Tsai
authored andcommitted
Bulk Create Function - Creates multiple records in specified tables based on provided data.
1 parent 543b15b commit 10298ff

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Creates multiple records in specified tables based on provided data.
3+
*
4+
* @param {Object} target - An object where each key is the name of a table,
5+
* and each value is an array of objects representing
6+
* the records to be created. Each object in the array
7+
* should contain field-value pairs for the respective table.
8+
*
9+
* Example usage:
10+
* bulkCreateRecords({
11+
* 'incident': [
12+
* { short_description: 'Network issue', caller_id: '681ccaf9c0a8016401c5a33be04be441', priority: 2 },
13+
* { short_description: 'Email outage', caller_id: '681ccaf9c0a8016401c5a33be04be442', priority: 1 }
14+
* ],
15+
* 'change_request': [
16+
* { short_description: 'Server upgrade', assigned_to: '681ccaf9c0a8016401c5a33be04be443', state: 'new' }
17+
* ]
18+
* });
19+
*
20+
* This creates two new records in the 'incident' table and one new record in the
21+
* 'change_request' table with the specified field values.
22+
*/
23+
24+
function bulkCreateRecords(target) {
25+
for (var table in target) {
26+
if (target.hasOwnProperty(table)) {
27+
var recordData = target[table];
28+
recordData.forEach(function(data) {
29+
var gr = new GlideRecord(table);
30+
gr.initialize();
31+
for (var field in data) {
32+
if (data.hasOwnProperty(field)) {
33+
gr.setValue(field, data[field]);
34+
}
35+
}
36+
gr.insert();
37+
});
38+
}
39+
}
40+
}
41+
42+
43+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Function: `bulkCreateRecords(target)`
2+
3+
Creates multiple records in specified tables based on provided data.
4+
5+
## Parameters
6+
7+
- **`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.
8+
9+
## Example Usage
10+
11+
```javascript
12+
bulkCreateRecords({
13+
'incident': [
14+
{ short_description: 'Network issue', caller_id: '681ccaf9c0a8016401c5a33be04be441', priority: 2 },
15+
{ short_description: 'Email outage', caller_id: '681ccaf9c0a8016401c5a33be04be442', priority: 1 }
16+
],
17+
'change_request': [
18+
{ short_description: 'Server upgrade', assigned_to: '681ccaf9c0a8016401c5a33be04be443', state: 'new' }
19+
]
20+
});

0 commit comments

Comments
 (0)