Skip to content

Commit 8bb80f5

Browse files
committed
Succesfully added Clone parent records BG script and readMe
1 parent a81a30b commit 8bb80f5

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* This script copies records from a parent table into a child table, optionally keeping a reference (link) to the parent record.
3+
* This Script Uses Immediately Invoked Function Expression (IIFE) — meaning the script runs immediately once loaded.
4+
*/
5+
(function() {
6+
var parentGR = new GlideRecord('x_parent_table');
7+
parentGR.addQuery('your Query');
8+
parentGR.query();
9+
10+
while (parentGR.next()) {
11+
// Create a record in the child table
12+
var childGR = new GlideRecord('x_child_table');
13+
childGR.initialize();
14+
15+
// Copy parent fields (example fields)
16+
childGR.short_description = parentGR.short_description;
17+
childGR.description = parentGR.description;
18+
19+
// If you want to maintain parent reference
20+
childGR.parent = parentGR.sys_id;
21+
22+
childGR.insert();
23+
}
24+
25+
gs.info('Records from parent table copied into child table successfully!');
26+
})();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# CloneParentRecords.js
2+
3+
This background script copies records from a parent table into a child table in ServiceNow. It's intended to be run from the Scripts - Background module (or adapted into a Script Include / scheduled job).
4+
5+
## Purpose
6+
7+
The script iterates parent records that match a query and creates corresponding records in a child table. Optionally it preserves a reference to the parent record by storing the parent's `sys_id` on the child record.
8+
9+
## Configuration (what to edit)
10+
Open `CloneParentRecords.js` and update these values to match your instance:
11+
12+
- `x_parent_table` – change to your parent table name (e.g. `incident`).
13+
- `your Query` – replace with a Glide encoded query or condition (e.g. `active=true^priority=1`).
14+
- `x_child_table` – change to your child table name.
15+
- Field mappings – modify the lines that copy fields (`short_description`, `description`, etc.) to match the fields you want to clone.
16+
- `childGR.parent` – change this to the child-side reference field name if different.
17+
18+
## How to run
19+
20+
1. In your ServiceNow instance go to **System Definition > Scripts - Background**.
21+
2. Paste the script content from `CloneParentRecords.js` into the editor.
22+
3. Test first on a limited dataset by changing the query to return a small set (or by using `parentGR.setLimit(n)` before `query()` if appropriate).
23+
4. Click **Run script**.
24+
25+
Alternatively, convert the logic into a Script Include and call it from a Scheduled Job for repeatable, controlled execution.
26+
27+
## Safety & performance notes
28+
29+
- Always run in a sub-production (development or test) instance first.
30+
- If the parent table contains many records, run in batches. Large transactions can time out or lock tables.
31+
- Consider adding `parentGR.setLimit(1000)` or using ranges (for example, by sys_created_on or sys_id ranges) to process data in chunks.
32+
- Use `gs.info()` for progress logging. For very large operations, consider using an asynchronous approach (Scheduled Jobs, Event-driven, or Import Set transform).
33+
- Ensure the user running the script has write access to the child table fields being created.
34+
35+
## Error handling suggestions
36+
37+
- Wrap critical operations with try/catch in a Script Include version and log errors to a custom table or `syslog`.
38+
- Validate required fields before `insert()` to avoid runtime field validation errors.
39+
40+
## Example modifications
41+
42+
- To avoid copying every record, change the query to: `parentGR.addQuery('active', true);` or `parentGR.addEncodedQuery('active=true^priority=1');`
43+
- To map additional fields: `childGR.u_custom = parentGR.u_custom;` (replace `u_custom` with your field names).
44+
45+
46+

0 commit comments

Comments
 (0)