Skip to content

Commit 5516679

Browse files
authored
Merge branch 'ServiceNowDevProgram:main' into main
2 parents ba22acd + e33c87f commit 5516679

19 files changed

Lines changed: 349 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Sets the field value to the formatted percentage string (e.g., 45 becomes 45.00%).
2+
3+
Retrieves the current value of the field.
4+
Removes any existing % symbol to avoid duplication.
5+
Validates the input to ensure it's a number.
6+
Converts the value to a float.
7+
Formats it to two decimal places and appends a % symbol.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*Sets the field value to the formatted percentage string (e.g., 45 becomes 45.00%).
2+
3+
Retrieves the current value of the field.
4+
Removes any existing % symbol to avoid duplication.
5+
Validates the input to ensure it's a number.
6+
Converts the value to a float.
7+
Formats it to two decimal places and appends a % symbol. */
8+
9+
10+
function onChange(control, oldValue, newValue, isLoading) {
11+
if (isLoading || newValue == '') {
12+
return;
13+
}
14+
15+
function formatPercent(value) {
16+
if (value === null || value === undefined || value === '' || isNaN(value)) {
17+
return "";
18+
}
19+
var num = parseFloat(value);
20+
if (isNaN(num))
21+
return "";
22+
return num.toFixed(2) + "%";
23+
}
24+
var des_amount = g_form.getValue("<variable_name>").replace("%","");
25+
26+
g_form.setValue("<variable_name>", formatPercent(des_amount));
27+
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This project implements an Auto Save Draft feature for ServiceNow Catalog Items. It automatically saves the user’s progress (form variables) every few minutes to prevent data loss if the session times out or the browser closes. it Prevents data loss during long form filling.
2+
3+
4+
5+
features
6+
Auto-save catalog form data every 2 minutes.
7+
Stores draft data in a custom table.
8+
Restores saved data when the user reopens the catalog item.
9+
Works in Service Portal
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var CatalogDraftUtils = Class.create();
2+
CatalogDraftUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
saveDraft: function() {
4+
var userId = gs.getUserID();
5+
var catalogItem = this.getParameter('sysparm_catalog_item');
6+
var draftData = this.getParameter('sysparm_draft_data');
7+
8+
var gr = new GlideRecord('u_catalog_draft');
9+
gr.addQuery('user', userId);
10+
gr.addQuery('catalog_item', catalogItem);
11+
gr.query();
12+
if (gr.next()) {
13+
gr.variables_json = draftData;
14+
gr.last_saved = new GlideDateTime();
15+
gr.update();
16+
} else {
17+
gr.initialize();
18+
gr.user = userId;
19+
gr.catalog_item = catalogItem;
20+
gr.variables_json = draftData;
21+
gr.last_saved = new GlideDateTime();
22+
gr.insert();
23+
}
24+
return 'Draft saved successfully';
25+
},
26+
type: 'CatalogDraftUtils'
27+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function onLoad() {
2+
console.log('Auto Save Draft initialized');
3+
4+
setInterval(function() {
5+
var draftData = {
6+
hardware_name: g_form.getValue('hardware_name'),
7+
quantity: g_form.getValue('quantity')
8+
};
9+
10+
var ga = new GlideAjax('CatalogDraftUtils');
11+
ga.addParam('sysparm_name', 'saveDraft');
12+
ga.addParam('sysparm_catalog_item', g_form.getValue('sys_id')); // Catalog item sys_id
13+
ga.addParam('sysparm_draft_data', JSON.stringify(draftData));
14+
15+
ga.getXMLAnswer(function(response) {
16+
console.log('Draft saved: ' + response);
17+
});
18+
}, 120000); // Every 2 minutes
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
**Details**
2+
3+
This is a on change client script on sys_template table. This script will restrict users to select defined fields while template creation.
4+
Type: OnChange
5+
Field: Template
6+
Table: sys_template
7+
8+
**Use Case**
9+
10+
There is an OOB functionality to restrict fields using "**save as template**" ACL, but it has below limitations:
11+
1. If the requirement is to restrcit more number of fields (example: 20), 20 ACLs will have to be created.
12+
2. The ACls will have instance wide effect, this script will just restrict on client side.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Type: onChnage
3+
Table: sys_template
4+
Field: Template
5+
*/
6+
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
7+
if (isLoading || newValue === '') {
8+
return;
9+
}
10+
if (g_form.getValue('table') == 'incident') { // table on which sys_template is being created.
11+
var fields = ['active', 'comments']; // array of fields to be restricted while template creation.
12+
for (var i = 0; i < fields.length; i++) {
13+
if (newValue.indexOf(fields[i]) > -1) {
14+
alert("You Cannot Add " + fields[i]); // alert if user selects the restricted field.
15+
var qry = newValue.split(fields[i]);
16+
g_form.setValue('template', qry[0] + 'EQ'); // set the template value to previous values (oldValue does not work in this case).
17+
}
18+
}
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Table Size Analysis Script
2+
3+
This script checks the number of records in selected ServiceNow tables and shows how many were created in the last 30 days.
4+
5+
## Tables Checked
6+
- `task`
7+
- `cmdb_ci`
8+
- `sc_cat_item`
9+
10+
## What It Does
11+
- Logs the start of the analysis.
12+
- Counts total records in each table.
13+
- Counts records created in the last 30 days.
14+
- Logs both counts to the system log.
15+
16+
## How to Use
17+
1. Add or remove table names in the `tablesToCheck` list.
18+
2. Run the script in a background script or scheduled job.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// === TABLE SIZE ANALYSIS SCRIPT ===
2+
// Log the start of the analysis
3+
gs.info('=== TABLE SIZE ANALYSIS ===');
4+
5+
// Define the list of tables to analyze
6+
var tablesToCheck = ['task', 'cmdb_ci', 'sc_cat_item'];
7+
8+
// Loop through each table in the list
9+
for (var i = 0; i < tablesToCheck.length; i++) {
10+
var tableName = tablesToCheck[i]; // Get current table name
11+
12+
// Create a GlideAggregate object to count total records in the table
13+
var grCount = new GlideAggregate(tableName);
14+
grCount.addAggregate('COUNT'); // Add COUNT aggregate
15+
grCount.query(); // Execute the query
16+
17+
// If the query returns a result
18+
if (grCount.next()) {
19+
var recordCount = grCount.getAggregate('COUNT'); // Get total record count
20+
gs.info('Table: ' + tableName + ' | Record count: ' + recordCount); // Log the count
21+
22+
// Optional: Analyze growth by checking records created in the last 30 days
23+
var grRecent = new GlideAggregate(tableName);
24+
grRecent.addAggregate('COUNT'); // Add COUNT aggregate
25+
grRecent.addQuery('sys_created_on', '>=', gs.daysAgo(30)); // Filter records created in last 30 days
26+
grRecent.query(); // Execute the query
27+
28+
// If the query returns a result
29+
if (grRecent.next()) {
30+
var recentCount = grRecent.getAggregate('COUNT'); // Get count of recent records
31+
gs.info(' - Records created last 30 days: ' + recentCount); // Log the recent count
32+
}
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Email Mismatch Checker
2+
3+
## Description
4+
5+
This ServiceNow background script checks for mismatches between notification devices and user records based on email addresses.
6+
7+
It looks for active `cmn_notif_device` records that:
8+
- Have a non-null `email_address`
9+
- Are linked to a user
10+
- Are of type "Email"
11+
- Are named "Primary Email"
12+
13+
Then it verifies if a matching user exists in the `sys_user` table with the same email. If no match is found, the mismatch is logged.
14+
15+
## How to Use
16+
17+
1. Go to **Scripts > Background** in your ServiceNow instance.
18+
2. Paste the script.
19+
3. Run the script.
20+
4. Check the system logs for mismatch details.
21+
22+
## Output
23+
24+
Logs the number of mismatches and details like:
25+
Mismatch: Device=<device_name>, Device Email=<email_address>

0 commit comments

Comments
 (0)