Skip to content

Commit fb77822

Browse files
committed
feature of preserving sys_fields is added
1 parent 036d425 commit fb77822

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Update fields while preserving system fields (Background Script)
2+
3+
This background script pattern demonstrates how to update user editable fields on a record while preserving system managed fields (such as sys_created_by, sys_created_on, sys_updated_by, sys_updated_on, and sys_mod_count) and avoiding unintended side effects.
4+
5+
## Purpose
6+
7+
When programmatically updating records you may want to:
8+
- Update a subset of fields (business fields) while leaving system fields untouched.
9+
- Preserve created/updated metadata when performing a migration or bulk update in a safe way.
10+
11+
This snippet shows a conservative approach: copy only allowed fields from a source object, avoid writing system fields, and use GlideRecord's update operations safely.
12+
13+
## Behavior
14+
15+
1. Initializes a GlideRecord object for a specified table.
16+
2. Applies a query condition to filter which records should be updated.
17+
3. Iterates through matching records using a while (gr.next()) loop.
18+
4. Temporarily disables:
19+
- System field updates (sys_updated_by, sys_updated_on)
20+
- Business rules, flows, and workflows
21+
5. Sets new field values using setValue() for both normal and reference fields.
22+
6. Saves the updated records with update().
23+
24+
25+
## Important notes and options
26+
27+
- Do NOT set or copy fields that start with `sys_` (for example: `sys_created_by`, `sys_created_on`, `sys_updated_on`, `sys_mod_count`). These are system-controlled and writing them directly can lead to audit and integrity issues.
28+
- To prevent side effects from business rules or workflows, you can call `gr.setWorkflow(false)` and `gr.autoSysFields(false)`
29+
- When running bulk updates, batch the records and monitor system performance. Use indexes on query fields and limit the scope of updates.
30+
31+
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var gr=new GlideRecord('<table_name>');
2+
gr.addQuery('<query condition>');
3+
gr.query();
4+
while(gr.next()){
5+
// Prevent system fields from being updated
6+
gr.autoSysFields(false);
7+
// Disable business rules and workflows
8+
gr.setWorkflow(false);
9+
// Update fields as needed
10+
gr.setValue('<field backend name>', '<value to be set>');
11+
gr.setValue('<reference field backend name>', '<value to be set>');
12+
gr.update();
13+
}

0 commit comments

Comments
 (0)