Skip to content

Commit 611dc63

Browse files
authored
Merge branch 'ServiceNowDevProgram:main' into Check-Attchment
2 parents 795a4ce + 6ec1f27 commit 611dc63

File tree

8 files changed

+139
-0
lines changed

8 files changed

+139
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Scheduled Job Update Set Capture Script
2+
3+
This ServiceNow background script addresses a critical deployment challenge by programmatically capturing scheduled jobs in update sets.
4+
By default, ServiceNow scheduled jobs are not automatically captured in update sets, making them difficult to migrate between environments.
5+
This script uses the GlideUpdateManager2 API to force a scheduled job record into the current update set, enabling seamless deployment through standard update set processes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var gr = new GlideRecord('sysauto_script');
2+
gr.get('<sys_id of your scheduled job>');
3+
var gum = new GlideUpdateManager2();
4+
gum.saveRecord(gr);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Simple background script to test user access to records and fields by impersonating different users.
2+
3+
## What it does
4+
5+
- Impersonates a specified user
6+
- Tests access to a specific record
7+
- Checks visibility of configured fields
8+
- Returns to original user context
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var CONFIG = {
2+
tableName: 'incident',
3+
recordSysId: 'your_record_sys_id_here',
4+
userToImpersonate: 'user_sys_id_here',
5+
fieldsToTest: ['number', 'short_description', 'priority', 'assignment_group']
6+
};
7+
8+
var originalUserID = gs.getUserID();
9+
var impersonator = new GlideImpersonate();
10+
11+
gs.print("Original user: " + gs.getUser().getDisplayName());
12+
13+
impersonator.impersonate(CONFIG.userToImpersonate);
14+
gs.print("Now impersonating: " + gs.getUser().getDisplayName());
15+
16+
var gr = new GlideRecordSecure(CONFIG.tableName);
17+
if (gr.get(CONFIG.recordSysId)) {
18+
gs.print("Record accessible: " + gr.getDisplayValue());
19+
20+
for (var i = 0; i < CONFIG.fieldsToTest.length; i++) {
21+
var field = CONFIG.fieldsToTest[i];
22+
var value = gr.getDisplayValue(field);
23+
gs.print("Field '" + field + "': " + (value || 'Empty/No access'));
24+
}
25+
} else {
26+
gs.print("Record not found or not accessible");
27+
}
28+
29+
impersonator.impersonate(originalUserID);
30+
gs.print("Back to original user: " + gs.getUser().getDisplayName());
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
>**When a new Approval record (sysapproval_approver table) is created**
2+
3+
1. Create a Before Business Rule on the Approval (sysapproval_approver) table.
4+
5+
2. Check if the table of the record being approved is the Requested For table.
6+
7+
3. If it does:
8+
9+
Verify whether the Approver (approver) is the same as the Opened by (opened_by) field on the related Requested For record.
10+
11+
If both match:
12+
13+
Automatically approve the approval record.
14+
15+
Add appropriate approval comments (e.g., “Auto-approved since approver is the requestor (Opened By).”)
16+
17+
Use setWorkflow(false) to prevent triggering additional workflows or business rules.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(function executeRule(current, previous /*null when async*/ ) {
2+
3+
try {
4+
gs.setWorkflow(false);
5+
if (!current.sysapproval)
6+
return;
7+
8+
// Get the parent record (RITM / REQ / etc.)
9+
var parent = current.sysapproval.getRefRecord();
10+
if (!parent || !parent.isValidRecord())
11+
return;
12+
13+
if (parent.getTableName() == "sc_req_item") {
14+
15+
// Load Opened By user record
16+
var userGR = new GlideRecord("sys_user");
17+
if (!userGR.get(parent.requested_for))
18+
return;
19+
20+
21+
// If approver == Opened By → auto-approve
22+
if (current.approver == parent.opened_by) {
23+
current.state = "approved";
24+
current.comments = "Auto-approved as " + current.getDisplayValue("approver") + " is the manager of Requested For";
25+
current.update();
26+
27+
// Also update parent RITM stage to 'Approved'
28+
parent.stage = "approved";
29+
parent.update();
30+
}
31+
}
32+
33+
} catch (ex) {
34+
gs.error("Auto-approval BR error: " + ex.message);
35+
}
36+
37+
})(current, previous);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ServiceNow Incident CSV Export Email Script
2+
3+
This ServiceNow email script automatically generates and attaches a CSV file containing incident data to email notifications. The script extracts active incidents from your ServiceNow instance, formats them into a structured CSV file, and attaches the file to outbound email notifications, providing recipients with a comprehensive incident report in a portable format.
4+
5+
What This Script Does:
6+
The email script performs the following operations:
7+
Data Extraction: Queries all active incidents from the ServiceNow incident table
8+
CSV Generation: Formats incident data into a structured CSV file with predefined headers
9+
File Attachment: Automatically attaches the generated CSV file to email notifications
10+
Dynamic Content: Creates fresh data exports each time the notification is triggered
11+
Portable Format: Provides incident data in a universally readable CSV format
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
2+
/* Optional EmailOutbound */
3+
email, /* Optional GlideRecord */ email_action,
4+
/* Optional GlideRecord */
5+
event) {
6+
7+
var Headers = ["Number", "Caller", "Short Desc", "Assignment Group", "Assigned To"];
8+
var currentDate = new GlideDateTime();
9+
var fileName = 'Incidents' + '.csv';
10+
var csvData = ''; //The variable csvData will contain a string which is used to build the CSV file contents
11+
for (var i = 0; i < Headers.length; i++) { //Build the Headers
12+
csvData = csvData + '"' + Headers[i] + '"' + ',';
13+
}
14+
csvData = csvData + "\r\n";
15+
16+
var gr = new GlideRecord("incident");
17+
gr.addActiveQuery();
18+
gr.query();
19+
while (gr.next()) {
20+
csvData = csvData + '"' + gr.number + '",' + '"' + gr.caller_id.getDisplayValue() + '",' + '"' + gr.short_description + '",' + '"' + gr.assignment_group.getDisplayValue() + '",' + '"' + gr.assigned_to.getDisplayValue() + '"';
21+
csvData = csvData + "\r\n";
22+
}
23+
24+
var grAttachment = new GlideSysAttachment();
25+
grAttachment.write(event, fileName, 'application/csv', csvData);
26+
27+
})(current, template, email, email_action, event);

0 commit comments

Comments
 (0)