Skip to content

Commit 5e33146

Browse files
Merge branch 'ServiceNowDevProgram:main' into Email-business-rule/mail-script
2 parents 0c26675 + 7566048 commit 5e33146

File tree

68 files changed

+1455
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1455
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Background Script provides the list of installed plugins, version installed and version available for the upgrade in the instance
2+
3+
Note: We need to set the basic auth credential in order for the script to work on the instance where we are running it.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//Get the instance
2+
var instance_name = gs.getProperty("instance_name");
3+
4+
//build the endpoint
5+
var endPoint = 'https://'+instance_name+'.service-now.com/api/sn_appclient/appmanager/apps?tab_context=installed';
6+
7+
//initialize the RestMessageV2 API.
8+
var request = new sn_ws.RESTMessageV2();
9+
request.setEndpoint(endPoint);
10+
request.setHttpMethod('POST');
11+
12+
//Eg. UserName="admin", Password="admin" for this code sample.
13+
var user = 'admin';
14+
var password = '****';
15+
16+
//set the authentication
17+
request.setBasicAuth(user,password);
18+
19+
//set the request header
20+
request.setRequestHeader("Accept","application/json");
21+
22+
//invoke the API
23+
var response = request.execute();
24+
25+
//Parse the response
26+
var jsonResponse = JSON.parse(response.getBody());
27+
28+
var appsList = jsonResponse.result.apps;
29+
30+
//Print the Header for the response
31+
gs.info("Application name"+" | "+ "Assigned version"+" | " + "Latest version | " + "Hasupdate");
32+
appsList.forEach(function(app){
33+
//Print the plugin details
34+
var hasUpdate = app.update_available == 1 ? "true" : "false";
35+
gs.info(app.name+" | "+ app.assigned_version+" | " + app.latest_version+" | " + hasUpdate);
36+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Replace 'Project Workspace' with the name of the application you want to check
2+
var appName = 'Project Workspace';
3+
4+
var appRecord = new GlideRecord('sys_app');
5+
appRecord.addQuery('name', appName);
6+
appRecord.query();
7+
8+
if (appRecord.next()) {
9+
gs.print("Application Name: " + appName);
10+
gs.print("Current Version: " + appRecord.getValue('version'));
11+
} else {
12+
gs.print("The Application '" + appName + "' is not found.");
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
- Set the appName variable to the exact name of the application you’re checking (in this case as an example, Project Workspace).
2+
- This script queries the Application [sys_app] table for a record with the specified name.
3+
- If the application is found, it retrieves and prints the version. If not, it prints a message stating the application wasn’t found.
4+
- This script will find the version of a specific application and output the version in the Scripts - Background logs.
5+
6+
7+
8+
9+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This script will work to notify the users if their password is going to expire in less than 7 days.
2+
Prerequisite : You will need to create an event first.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var userGR = new GlideRecord('sys_user');
2+
userGR.query();
3+
while (userGR.next()) {
4+
var expiryDate = new GlideDateTime(userGR.password_needs_reset_by);
5+
var today = new GlideDateTime();
6+
var diff = GlideDateTime.subtract(expiryDate, today);
7+
8+
// Check the difference in days
9+
if (diff.getDays() < 7) {
10+
gs.eventQueue('password_expiry', userGR, 'Your password will expire in ' + diff.getDays() + ' days.', '');
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var emailGR = new GlideRecord('sys_email');
2+
3+
// Query for the emails you want to ignore
4+
emailGR.addQuery('state', 'ready'); // Only emails that are ready to send
5+
emailGR.addEncodedQuery("sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()"); // Optional timeline filter
6+
7+
// Set the fields to ignore and update all matching records at once
8+
emailGR.setValue('state', 'ignored'); // Set state to "ignored"
9+
emailGR.setValue('type', 'send-ignored'); // Set type to 'send-ignored'
10+
emailGR.updateMultiple(); // Bulk update all matching records
11+
12+
gs.info('All relevant emails have been marked as ignored.');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Created a background script to prevent unnecessary notifications from being sent out.
2+
It helps in managing the volume of emails being sent so that we do not send the notifications even by mistake.
3+
This script is mostly used in dev or uat to avoid any notifications being sent from lower instances.
4+
5+
We are querying the sys_email table to find all the emails with below queries:
6+
--> emails with state as "ready"
7+
--> emails that were created on today (optional query, if not added all the mails with state as "ready" will be considered for getting ignored.)
8+
9+
Post query we are setting as below:
10+
--> state of the email to "ignored"
11+
--> type of the email to "send-ignored"
12+
13+
After setting the fields we are updating the records.
14+
15+
Please be cautious while using the script in Production environment.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var updatedSysIds = [];
2+
var notUpdatedSysIds = [];
3+
4+
var gr = new GlideRecord('< table_name >');
5+
gr.addQuery('< query condition >');
6+
gr.query();
7+
8+
while (gr.next()) {
9+
10+
var relCi = new GlideRecord('cmdb_rel_ci');
11+
relCi.addQuery('child', gr.sys_id);
12+
relCi.addQuery('parent.sys_class_name', '< backend name of the table to which the reference field is referred to >');
13+
relCi.query();
14+
15+
if (relCi.next()) {
16+
// Update the reference field with the referenced table's sys_id
17+
gr.< reference field backend name > = relCi.parent.sys_id;
18+
gr.setWorkflow(false);
19+
gr.update();
20+
updatedSysIds.push(gr.sys_id.toString()); // Add to updated list
21+
}
22+
else {
23+
notUpdatedSysIds.push(gr.sys_id.toString()); // Add to not updated list
24+
}
25+
}
26+
27+
// Print the sys_ids of the records updated and not updated
28+
gs.print("Updated records sys_ids: " + updatedSysIds.join(', '));
29+
gs.print("Not updated records sys_ids: " + notUpdatedSysIds.join(', '));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This example demonstrates how it is possible to see the generated SQL query without enabling the SQL debug feture in the navigator
2+
try {
3+
gs.trace(true);
4+
var incGr = new GlideRecord("incident");
5+
incGr.setLimit(10);
6+
incGr.orderByDesc("sys_created_on");
7+
incGr.query();
8+
// TODO any other logic comes here...
9+
}
10+
finally {
11+
gs.trace(false);
12+
}

0 commit comments

Comments
 (0)