Skip to content

Commit 60a6895

Browse files
Merge branch 'ServiceNowDevProgram:main' into main
2 parents 49162ad + c71bf3f commit 60a6895

File tree

34 files changed

+651
-0
lines changed

34 files changed

+651
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var obj ={};
2+
obj.name = 'Mohit Kaushik';
3+
obj.email ='Mohit.1@abc.com';
4+
obj.contact = '1234567890';
5+
6+
var str = JSON.stringify(obj,null,4);
7+
8+
var encryption = GlideStringUtil.base64Encode(str);
9+
gs.info(encryption);
10+
11+
var decrypt = GlideStringUtil.base64Decode(encryption);
12+
gs.info(JSON.stringify(decrypt,null,2));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This code is an example to encrypt or decrypt the payload using base64Encode and decode methods of GlideStringUtil API.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var myGroups = [];
2+
var grGroup = new GlideRecord("sys_user_group");
3+
grGroup.addActiveQuery();
4+
grGroup.query();
5+
while (grGroup.next()) {
6+
var gaGroupMember = new GlideAggregate("sys_user_grmember");
7+
gaGroupMember.addQuery("group",grGroup.sys_id.toString());
8+
gaGroupMember.addAggregate('COUNT');
9+
gaGroupMember.query();
10+
var gm = 0;
11+
if (gaGroupMember.next()) {
12+
gm = gaGroupMember.getAggregate('COUNT');
13+
if (gm == 0)
14+
myGroups.push(grGroup.name.toString());
15+
}
16+
}
17+
gs.print(myGroups);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
**Initialize an Array:**
2+
var myGroups = [];
3+
4+
**Create a GlideRecord Object for User Groups:**
5+
var grGroup = new GlideRecord("sys_user_group");
6+
7+
**Add a Query for Active Groups:**
8+
grGroup.addActiveQuery();
9+
10+
**Execute the Query:**
11+
grGroup.query();
12+
13+
**Iterate Through Active Groups:**
14+
while (grGroup.next()) {
15+
16+
**Count Group Members:**
17+
var gaGroupMember = new GlideAggregate("sys_user_grmember");
18+
gaGroupMember.addQuery("group", grGroup.sys_id.toString());
19+
gaGroupMember.addAggregate('COUNT');
20+
gaGroupMember.query();
21+
22+
**Check Member Count:**
23+
var gm = 0;
24+
if (gaGroupMember.next()) {
25+
gm = gaGroupMember.getAggregate('COUNT');
26+
if (gm == 0)
27+
myGroups.push(grGroup.name.toString());
28+
}
29+
30+
**Print the Results:**
31+
gs.print(myGroups);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// In this before insert or update Business Rule, we are fetching a reference field value from higher-level parents in hierarchy when there is a field containing the parent record in the children and our use-case reference field is present in all the tables in hierarchy
2+
// I would be referring to "reference field name we want to populate" as "r1"
3+
// I would be referring to "reference field containing parent record" as "parent"
4+
5+
6+
(function executeRule(current, previous /*null when async*/ ) {
7+
if (current.r1 == "" && !JSUtil.nil(current.parent.r1)) // Populate 'use-case reference field' from parent's value for the reference field'
8+
current.r1 = current.parent.r1;
9+
else if (current.< reference field name we want to populate > == "" && !JSUtil.nil(current.parent.parent.r1)) // Populate 'use-case reference field' from 'parent of parent'
10+
current.r1 = current.parent.parent.r1;
11+
12+
})(current, previous);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
This is a "**before insert/update**" Business Rule
2+
We are fetching a reference field value from higher-level parents in hierarchy
3+
when there is a field containing the parent record in the children and
4+
our use-case reference field is present in all the tables in hierarchy
5+
6+
In the code, we are referring to "reference field name we want to populate" as "_r1_"
7+
In the code, we are referring to "reference field containing parent record" as "_parent_"
8+
9+
The "**JSUtil.nil**" is being used to check for empty/null value for the field.
10+
11+
12+
Through the code we are checking the empty value of the use-case reference field and dot walking to parents and fetching the value from them if it exists
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Scenario: You are wanting to send a notification to someone whenever they are added to a list of people.
2+
3+
Application: Create an event in the event registry. Then, create a business rule. Set the name and table, advanced = true.
4+
5+
When to run:
6+
When = after, update = true, conditions = watch list (replace with applicable field name) | changes
7+
8+
Advanced tab: insert the snippet
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
// Ensure the 'watch_list' (replace with applicable field name) field has been modified
4+
if (current.watch_list != previous.watch_list) {
5+
6+
// Split the current and previous watch lists into arrays
7+
var newWatchers = current.watch_list.split(',');
8+
var oldWatchers = previous.watch_list ? previous.watch_list.split(',') : [];
9+
10+
// Identify the newly added users to the watch list
11+
var addedUsers = newWatchers.filter(function (user) {
12+
return oldWatchers.indexOf(user) === -1;
13+
});
14+
15+
// Loop through the added users to trigger the event for each
16+
addedUsers.forEach(function(userID) {
17+
var email;
18+
var firstName;
19+
20+
// Try to get the user record by user ID (sys_id)
21+
var userGr = new GlideRecord('sys_user');
22+
if (userGr.get(userID)) {
23+
firstName = userGr.first_name;
24+
email = userGr.email;
25+
} else {
26+
27+
// If no user record is found, assume the userID is an email address
28+
email = userID;
29+
firstName = "Team";
30+
}
31+
32+
// Trigger the event (replace "new_member") with the current case and user information
33+
gs.eventQueue('new_member', current, email, firstName);
34+
});
35+
}
36+
})(current, previous);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Overview
2+
This script logs specific user actions (e.g: record updates and approvals) in ServiceNow into a custom table `u_user_activity_log`.
3+
4+
This provides audit capabilities and allowing developers to track user actions for compliance or analytics.
5+
6+
# How It Works
7+
The script is triggered by a Business Rule on record updates and checks for changes in specified critical fields (e.g., `state`, `approval`). When a change occurs, it logs relevant details in the `u_user_activity_log` table, including:
8+
- `u_user`: User ID
9+
- `u_action`: Type of action performed
10+
- `u_record_id`: ID of the updated record
11+
- `u_record_table`: Name of the table where the change occurred
12+
- `u_description`: Brief description of the action
13+
14+
# Implementation
15+
- Create Custom Table: Ensure `u_user_activity_log` table exists with fields like `u_user`, `u_action`, `u_record_id`, `u_record_table`, `u_description`, etc.
16+
- Configure Business Rule: Set the Business Rule to run on update and add conditions for monitoring fields (`state`, `approval`).
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Script to log user actions (updates, approvals) into a custom table
2+
(function executeRule(current, previous /*null when async*/) {
3+
// Check if key fields are modified (customize as needed)
4+
if (current.state.changes() || current.approval.changes()) {
5+
var logEntry = new GlideRecord('u_user_activity_log');
6+
logEntry.initialize();
7+
8+
// Populate log details
9+
logEntry.u_user = gs.getUserID();
10+
logEntry.u_action = current.state.changes() ? "State Change" : "Approval Change";
11+
logEntry.u_record_id = current.sys_id;
12+
logEntry.u_record_table = current.getTableName();
13+
logEntry.u_description = "User " + gs.getUserDisplayName() + " updated " + logEntry.u_action;
14+
15+
logEntry.insert();
16+
}
17+
})(current, previous);

0 commit comments

Comments
 (0)