Skip to content
Closed
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
36 changes: 36 additions & 0 deletions Core ServiceNow APIs/GlideRecord/Conditional Clone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# GlideRecord Conditional Clone

## Description
This snippet clones an existing record in a ServiceNow table while allowing optional field overrides.
It is useful for duplicating incidents, tasks, or custom records and modifying specific fields such as `assigned_to`.

## Prerequisites
- Server-side context (Background Script, Business Rule, or Script Include)
- Access to the table
- Familiarity with GlideRecord and sys_id

## Note

- Works in Global Scope
- Server-side execution only
- Ensures sys_id and system fields are not copied to avoid conflicts
- Returns the sys_id of the new record, or null if cloning fails

## Usage
```javascript
// Clone an incident and assign it to a new user
cloneRecord('incident', 'abc123sysid', {assigned_to: 'new_user_sysid'});

// Clone a task without changing any fields
cloneRecord('task', 'xyz789sysid', {});
```

## Output
```
Record cloned successfully. New sys_id: <sys_id>
```

## Tips

- Use fieldOverrides to update only specific fields without manually modifying the cloned record

45 changes: 45 additions & 0 deletions Core ServiceNow APIs/GlideRecord/Conditional Clone/cloneRecord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Clone a record in a ServiceNow table and optionally override specific fields.
*
* @param {string} table - Name of the table
* @param {string} sys_id - sys_id of the record to clone
* @param {object} fieldOverrides - Key-value pairs of fields to modify in the cloned record
* @returns {string|null} - sys_id of the new record or null if cloning fails
*/
function cloneRecord(table, sys_id, fieldOverrides) {
if (!table || !sys_id) {
gs.error('Table name and sys_id are required.');
return null;
}

var gr = new GlideRecord(table);
if (!gr.get(sys_id)) {
gs.error('Record not found with sys_id: ' + sys_id);
return null;
}

var newGr = new GlideRecord(table);
gr.getFields().forEach(function(field) {
var name = field.getName();
// Do not copy sys_id or system fields
if (name !== 'sys_id' && !field.getED().isVirtual()) {
newGr.setValue(name, gr.getValue(name));
}
});

// Apply field overrides
if (fieldOverrides && typeof fieldOverrides === 'object') {
for (var key in fieldOverrides) {
newGr.setValue(key, fieldOverrides[key]);
}
}

var newSysId = newGr.insert();
if (newSysId) {
gs.info('Record cloned successfully. New sys_id: ' + newSysId);
return newSysId;
} else {
gs.error('Failed to clone record.');
return null;
}
}
32 changes: 32 additions & 0 deletions Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# GlideRecord Bulk Delete with Safety Checks

## Description
This snippet allows you to safely delete multiple records from a ServiceNow table based on an encoded query.
It logs all records that match the query so you can review them before actually deleting anything.
Helps prevent accidental mass deletion of important data.

## Note
- Works in Global Scope by default
- Can be executed in Background Scripts or Script Includes
- **ALWAYS REVIEW LOGS BEFORE ENABLING DELETION**
## Prerequisites
- Server-side context (Background Script, Business Rule, Script Include)
- Access to the target table
- Basic understanding of GlideRecord and Encoded Queries

## Usage
```javascript
// Logs all active low-priority incidents that would be deleted
safeDelete('incident', 'active=true^priority=5');

// To perform actual deletion, uncomment gr.deleteRecord() inside the function
```

## Output
```
Records matching query: 3
Record sys_id: 12345abcdef would be deleted.
Record sys_id: 23456bcdef would be deleted.
Record sys_id: 34567cdefg would be deleted.
Bulk delete preview complete. Verify logs before enabling deletion.
```
33 changes: 33 additions & 0 deletions Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/safeDelete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Safely delete multiple records from a ServiceNow table.
* Logs all affected records before deletion to prevent accidental data loss.
* Uncomment gr.deleteRecord() to perform the actual deletion.
*
* @param {string} table - The table name
* @param {string} encodedQuery - GlideRecord encoded query for filtering records
*/
function safeDelete(table, encodedQuery) {
if (!table || !encodedQuery) {
gs.error('Both table name and encoded query are required.');
return;
}

var gr = new GlideRecord(table);
gr.addEncodedQuery(encodedQuery);
gr.query();

var count = gr.getRowCount();
gs.info('Records matching query: ' + count);

if (count === 0) {
gs.info('No records found. Nothing to delete.');
return;
}

while (gr.next()) {
gs.info('Record sys_id: ' + gr.getValue('sys_id') + ' would be deleted.');
// gr.deleteRecord(); // Uncomment this line to actually delete
}

gs.info('Bulk delete preview complete. Verify logs before enabling deletion.');
}
Loading