Skip to content

Commit 3e30e85

Browse files
Merge branch 'ServiceNowDevProgram:main' into main
2 parents 7cfb10e + f16d27e commit 3e30e85

File tree

569 files changed

+13911
-812
lines changed

Some content is hidden

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

569 files changed

+13911
-812
lines changed

.github/templates/rootreadmetemplate.md

Lines changed: 0 additions & 40 deletions
This file was deleted.

.github/workflows/dprevent.yml

Lines changed: 0 additions & 35 deletions
This file was deleted.

.github/workflows/hacktrack.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ on:
1414
branches: main
1515
jobs:
1616
deployment:
17+
if: github.repository == 'ServiceNowDevProgram/code-snippets'
1718
runs-on: ubuntu-latest
1819
steps:
1920
# - name: Log payload
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function calculateHash(attachmentId){
2+
var attachmentStream = new GlideSysAttachment().getContentStream(attachmentId);
3+
var gDigest = new GlideDigest();
4+
var sha256Hash = gDigest.getSHA256HexFromInputStream(attachmentStream);
5+
if (sha256Hash) {
6+
gs.info("Hash code of the attachment file: " + sha256Hash);
7+
}
8+
}
9+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This is example of recalculate the hash code using the glide digest getSHA256HexFromInputStream method.
2+
3+
GlideDigest() -
4+
This class provides methods for creating a message digest from strings or input streams using MD5, SHA1, or SHA256 hash algorithms.
5+
6+
Docs link: https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server/no-namespace/c_GlideDigestScopedAPI#r_SGDigest-GlideDigest
7+
8+
getSHA256HexFromInputStream - function takes GlideScriptableInputStream input stream as parameter.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// To automatically delete RITM attachment, when an attachment is deleted on SCTASK.
2+
// Create a After Business Rule, and set business rule condition as Table name is "sctask", so that when an attachment is deleted on SCTASK, this business rule will run.
3+
4+
var gr = new GlideRecord("sys_attachment");
5+
var task = new GlideRecord('sc_task');
6+
if (task.get(current.table_sys_id)) {
7+
gr.addEncodedQuery("table_name=sc_req_item^table_sys_id=" + task.request_item.sys_id + "^file_nameSTARTSWITH" + current.file_name);
8+
gr.query();
9+
if(gr.next())
10+
//gr.deleteRecord();
11+
var attachment = new GlideSysAttachment();
12+
attachment.deleteAttachment(gr.getValue("sys_id"));
13+
}
14+
15+
})(current, previous);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The Delete RITM attachment business rule will automatically deletes the attachment on RITM, when the attachment is deleted on SCTASK.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
// Check RITM for attachments and pass True/False to client via scratchpad
4+
var grAttachment = new GlideRecord('sc_req_item');
5+
grAttachment.addQuery('sys_id', current.request_item);
6+
grAttachment.query();
7+
if (grAttachment.next()){
8+
g_scratchpad.hasAttachments = grAttachment.hasAttachments();
9+
}
10+
11+
})(current, previous);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function onLoad() {
2+
if (g_scratchpad.hasAttachments == true) {
3+
g_form.showFieldMsg('request_item', "Contains attachment(s)",'info');
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This feature requires a "Display" Business rule on [sc_task] to pass true/false to the scratchpad and then an "onLoad" Client Script to display a message on the Catalog Task form if the RITM has attachments.
2+
3+
The result is a message on the Catalog Task form (below the RITM field) indicating when a RITM has attachments.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var sid = 'ADD YOUR CHANGE RECORD SYS_ID HERE';
2+
3+
var chg = new GlideRecord('change_request');
4+
if(chg.get(sid)) {
5+
chg.setValue('chg_model', 'e55d0bfec343101035ae3f52c1d3ae49'); //standard change model
6+
chg.update();
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add the OOTB "Standard Change" model to an existing change record. OOTB, the Standard Change model is applied through an onDisplay business rule, and is not an available choice from the Model field. If you have migrated to use Change Models and generate some change requests with scripts, you may need to add the model with a background script if there is already an existing workflow context or if the model is not set by the generation script. The setter portion can be added to an existing generation script to prevent future need for this background script.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Background script to send approval reminder to Approver Delegates:
2+
========================================
3+
var appr = new GlideRecord('sysapproval_approver');
4+
appr.addQuery('state', 'requested');
5+
appr.addQuery('sysapproval.sys_class_name', 'sc_req_item');
6+
appr.addEncodedQuery("sysapproval.numberLIKERITM0010468");
7+
appr.query();
8+
while(appr.next())
9+
{
10+
//" approval.reminded" is a registered event to trigger notificationn email to their delegates to approve or reject approval requests when approvar not available
11+
gs.eventQueue("approval.reminded" , appr , appr.approver , appr.approver.getUserName());
12+
}
13+
14+
15+
gs.print(appr.getRowCount());
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Use Case: Approval are pending due approver unavailability
2+
3+
Solution : Assign delgates to the approver and send reminder email notification using the Approval reminder code.It will send email to delgates and they will get notified with the approval request to approve/reject per limited period.
4+
5+
Steps: Register an event and create a notification to send email to manager when the event fired/triggered.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
var ab=new GlideRecord('sysapproval_approver');
3+
ab.addQuery('sysapproval','<Approval_record_sys_id>');
4+
ab.query();
5+
if(ab.next())
6+
{
7+
ab.approver.setDisplayValue('<User_Display_Name>')';
8+
ab.update();
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Use the code to change approver for any record from background script
2+
Replace <Approval_record_sys_id> with sys_id of record for which approval is triggered
3+
Replace <User_Display_Name> with name of user who needs to be set as approver
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Use this script to check for duplicates (and delete if necessary) in a table based on 2 or more criteria.
2+
For reference: https://www.servicenow.com/community/developer-blog/search-for-duplicates-delete-based-on-2-columns/ba-p/2279274
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
deleteDuplicates('table_name', 'criteria1', 'criteria2');
2+
3+
function deleteDuplicates(tableName, field1, field2){
4+
5+
// declare an array
6+
var dupRecords = [];
7+
var duplicateCheck = new GlideAggregate(tableName);
8+
duplicateCheck.addNotNullQuery(field1);
9+
duplicateCheck.addNotNullQuery(field2);
10+
duplicateCheck.groupBy(field1);
11+
duplicateCheck.groupBy(field2);
12+
duplicateCheck.addHaving('COUNT', '>', 1); // addHaving func won't work in scope app
13+
duplicateCheck.query();
14+
while(duplicateCheck.next()) {
15+
var jsonObj = {}; // declare a json object
16+
jsonObj[field1] = duplicateCheck[field1].toString();
17+
jsonObj[field2] = duplicateCheck[field2].toString()
18+
dupRecords.push(jsonObj);
19+
}
20+
21+
var jsonString = JSON.stringify(dupRecords); // convert json object to string
22+
23+
var parser = new JSONParser();
24+
var parsedData = parser.parse(jsonString);
25+
var length = parsedData.length;
26+
27+
for(var i=0; i<length; i++){
28+
29+
var encodedQuery = field1 + '=' + parsedData[i][field1] + '^' + field2 + '=' + parsedData[i][field2];
30+
31+
var tableRec = new GlideRecord(tableName);
32+
tableRec.addEncodedQuery(encodedQuery);
33+
tableRec.query();
34+
if(tableRec.next()){
35+
gs.info('Repeated Data is: User -> ' + tableRec.getDisplayValue('user') + ' Group -> ' + tableRec.getDisplayValue('group'));
36+
tableRec.deleteRecord();
37+
}
38+
}
39+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Clone a user to another user after creating a new user from SN and run the below script from Background Script under System Definition app.
2+
The sys id's are needed from both users to run the background script.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
createUserRoles();
2+
function createUserRoles(){
3+
var gr = new GlideRecord('sys_user_has_role');
4+
gr.addQuery('user', '<old sys id for that user instance>'); //old sys_id
5+
gr.query();
6+
while(gr.next()){
7+
var gr1 = new GlideRecord('sys_user_has_role');
8+
gr1.initialize();
9+
gr1.user = '<new sys id for that user instance>';//new user sys_id
10+
gr1.role = gr.role;
11+
gr1.insert();
12+
}
13+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
function cloneUser(currentUserId, newUserId) {
2+
3+
var newUserRecordSysId = createNewUserRecord(newUserId);
4+
cloneCurrentUserFields(currentUserId, newUserId);
5+
cloneUserRoles(currentUserId, newUserRecordSysId);
6+
cloneUserGroups(currentUserId, newUserRecordSysId);
7+
}
8+
9+
function cloneCurrentUserFields(currentUserId, newUserId) {
10+
11+
var currentUserGR = new GlideRecord("sys_user");
12+
currentUserGR.addQuery("user_name", currentUserId);
13+
currentUserGR.query();
14+
15+
if (currentUserGR.next()) {
16+
var newUserGR = new GlideRecord("sys_user");
17+
newUserGR.addQuery("user_name", newUserId);
18+
newUserGR.query();
19+
if (newUserGR.next()) {
20+
var userGRU = new GlideRecordUtil();
21+
var fieldList = userGRU.getFields(currentUserGR);
22+
for (var index = 0; index < fieldList.length; index++) {
23+
var fieldName = fieldList[index];
24+
if (!newUserGR.getValue(fieldName)) {
25+
newUserGR.setValue(fieldName, currentUserGR.getValue(fieldName));
26+
newUserGR.update();
27+
}
28+
};
29+
}
30+
}
31+
}
32+
33+
function createNewUserRecord(userId) {
34+
35+
var userGR = new GlideRecord("sys_user");
36+
userGR.initialize();
37+
userGR.setValue("user_name", userId);
38+
var sysId = userGR.insert();
39+
return sysId;
40+
}
41+
42+
function cloneUserRoles(currentUserId, newUserRecordSysId) {
43+
44+
var currentUserRoleGR = new GlideRecord("sys_user_has_role");
45+
currentUserRoleGR.addQuery('user.user_name', currentUserId);
46+
currentUserRoleGR.addQuery('inherited', 'false');
47+
currentUserRoleGR.query();
48+
49+
while (currentUserRoleGR.next()) {
50+
var newUserRoleGR = new GlideRecord("sys_user_has_role");
51+
newUserRoleGR.initialize();
52+
newUserRoleGR.setValue('user', newUserRecordSysId);
53+
newUserRoleGR.setValue('role', currentUserRoleGR.getValue('role'));
54+
newUserRoleGR.insert();
55+
}
56+
}
57+
58+
function cloneUserGroups(currentUserId, newUserRecordSysId) {
59+
60+
var currentUserGroupGR = new GlideRecord("sys_user_grmember");
61+
currentUserGroupGR.addQuery('user.user_name', currentUserId);
62+
currentUserGroupGR.query();
63+
64+
while (currentUserGroupGR.next()) {
65+
var newUserGroupGR = new GlideRecord("sys_user_grmember");
66+
newUserGroupGR.initialize();
67+
newUserGroupGR.setValue('user', newUserRecordSysId);
68+
newUserGroupGR.setValue('group', currentUserGroupGR.getValue('group'));
69+
newUserGroupGR.insert();
70+
}
71+
}
72+
73+
cloneUser('currentUserId', 'newUserId'); //currentUserId: Id of the you that we want to clone, newUserId: Id of the new user record.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Clone User with Roles and Groups
2+
I have created a script in ServiceNow to replicate a user's profile. This script not only duplicates the user's data but also replicates the roles and groups assigned to that user.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is the simple code to run independently from AES -> Background Scripts module and convert the datetime from one time zone to other
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var strConvertedDateTime=new GlideScheduleDateTime("2022-03-03 06:30:00").convertTimeZone("CET", "IST"); // Instantiate the object by passing the timezones
2+
var gdtConvertedDateTime = new GlideDateTime(strConvertedDateTime); //Call the method to convert the date time from CET to IST
3+
gs.info(gdtConvertedDateTime); //Print the converted value
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1.Take any comma separated string to be displayed in columns
2+
2.Apply above logic
3+
3.update to field /Print /Use this in mail scripts to include in the notification.
4+
4. we can also add this arryUtils script include (to convert arry elements to colums)
5+
6+
![image](https://user-images.githubusercontent.com/42912180/195672456-f282c82e-8516-4925-9e71-c80da8329ef3.png)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var Str = "Element1,Element2,Element3,Element4,Element5,Element6";
2+
result =[];
3+
var myArray = Str.split(",");
4+
for(var i =0; i<=myArray.length;i++){
5+
result.push(myArray[i]);
6+
}
7+
var output= "\n" +result.join("\n");
8+
gs.info(output);

0 commit comments

Comments
 (0)