File tree Expand file tree Collapse file tree 2 files changed +81
-0
lines changed
Core ServiceNow APIs/GlideRecord/Conditional Clone Expand file tree Collapse file tree 2 files changed +81
-0
lines changed Original file line number Diff line number Diff line change 1+ # GlideRecord Conditional Clone
2+
3+ ## Description
4+ This snippet clones an existing record in a ServiceNow table while allowing optional field overrides.
5+ It is useful for duplicating incidents, tasks, or custom records and modifying specific fields such as ` assigned_to ` .
6+
7+ ## Prerequisites
8+ - Server-side context (Background Script, Business Rule, or Script Include)
9+ - Access to the table
10+ - Familiarity with GlideRecord and sys_id
11+
12+ ## Note
13+
14+ - Works in Global Scope
15+ - Server-side execution only
16+ - Ensures sys_id and system fields are not copied to avoid conflicts
17+ - Returns the sys_id of the new record, or null if cloning fails
18+
19+ ## Usage
20+ ``` javascript
21+ // Clone an incident and assign it to a new user
22+ cloneRecord (' incident' , ' abc123sysid' , {assigned_to: ' new_user_sysid' });
23+
24+ // Clone a task without changing any fields
25+ cloneRecord (' task' , ' xyz789sysid' , {});
26+ ```
27+
28+ ## Output
29+ ```
30+ Record cloned successfully. New sys_id: <sys_id>
31+ ```
32+
33+ ## Tips
34+
35+ - Use fieldOverrides to update only specific fields without manually modifying the cloned record
36+
Original file line number Diff line number Diff line change 1+ /**
2+ * Clone a record in a ServiceNow table and optionally override specific fields.
3+ *
4+ * @param {string } table - Name of the table
5+ * @param {string } sys_id - sys_id of the record to clone
6+ * @param {object } fieldOverrides - Key-value pairs of fields to modify in the cloned record
7+ * @returns {string|null } - sys_id of the new record or null if cloning fails
8+ */
9+ function cloneRecord ( table , sys_id , fieldOverrides ) {
10+ if ( ! table || ! sys_id ) {
11+ gs . error ( 'Table name and sys_id are required.' ) ;
12+ return null ;
13+ }
14+
15+ var gr = new GlideRecord ( table ) ;
16+ if ( ! gr . get ( sys_id ) ) {
17+ gs . error ( 'Record not found with sys_id: ' + sys_id ) ;
18+ return null ;
19+ }
20+
21+ var newGr = new GlideRecord ( table ) ;
22+ gr . getFields ( ) . forEach ( function ( field ) {
23+ var name = field . getName ( ) ;
24+ // Do not copy sys_id or system fields
25+ if ( name !== 'sys_id' && ! field . getED ( ) . isVirtual ( ) ) {
26+ newGr . setValue ( name , gr . getValue ( name ) ) ;
27+ }
28+ } ) ;
29+
30+ // Apply field overrides
31+ if ( fieldOverrides && typeof fieldOverrides === 'object' ) {
32+ for ( var key in fieldOverrides ) {
33+ newGr . setValue ( key , fieldOverrides [ key ] ) ;
34+ }
35+ }
36+
37+ var newSysId = newGr . insert ( ) ;
38+ if ( newSysId ) {
39+ gs . info ( 'Record cloned successfully. New sys_id: ' + newSysId ) ;
40+ return newSysId ;
41+ } else {
42+ gs . error ( 'Failed to clone record.' ) ;
43+ return null ;
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments