From 5d9ed2aec3602dfdf117754fa93d128b4d6d2e06 Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Mon, 30 Oct 2023 12:20:48 +0530 Subject: [PATCH 01/13] CheckNewRecord Check with new and existing incident records in the ServiceNow instance. --- GlideRecord/CheckNewRecord | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 GlideRecord/CheckNewRecord diff --git a/GlideRecord/CheckNewRecord b/GlideRecord/CheckNewRecord new file mode 100644 index 0000000000..6e29644cf5 --- /dev/null +++ b/GlideRecord/CheckNewRecord @@ -0,0 +1,16 @@ +var gr = new GlideRecord('incident'); + + +if (gr.isNewRecord()) { + gs.log("This is a new incident record."); +} else { + gs.log("This is an existing incident record."); +} +gr.addQuery('active', true); +gr.query(); + +while (gr.next()) { + + gs.log('Incident Number: ' + gr.number); + gs.log('Short Description: ' + gr.short_description); +} From f9ecae335cdb795cdee765f6d5036de505e50e2a Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Mon, 30 Oct 2023 12:27:42 +0530 Subject: [PATCH 02/13] FlashEmptyField Name: FlashEmptyField Table: Select the table where the field is located (e.g., Incident). Applies: Specify the field you want to validate (e.g., short_description). --- Client Scripts/FlashEmptyField | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Client Scripts/FlashEmptyField diff --git a/Client Scripts/FlashEmptyField b/Client Scripts/FlashEmptyField new file mode 100644 index 0000000000..976dcf6f75 --- /dev/null +++ b/Client Scripts/FlashEmptyField @@ -0,0 +1,15 @@ +function onLoad() { + // Get the field element + var field = g_form.getControl('short_description'); + + // Check if the field is empty + if (g_form.getValue('short_description').trim() === '') { + // Apply CSS style to flash the field + field.style.backgroundColor = 'red'; + + // Set a timeout to reset the background color after a few seconds + setTimeout(function() { + field.style.backgroundColor = ''; + }, 2000); // Change '2000' to set the duration in milliseconds + } +} From 31336821684c1671f06d9288015de0ad4eb7c1e2 Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Mon, 30 Oct 2023 12:40:28 +0530 Subject: [PATCH 03/13] PreventFutureDateSelection Table: Select the table where the date field is located (e.g., Incident). Applies: Specify the date field you want to validate (e.g., opened_at). when a user tries to select a future date in the specified date field, the script will prevent it and display an alert. You should replace 'your_date_field' with the actual name of the date field in your instance. --- Client Scripts/PreventFutureDateSelection | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Client Scripts/PreventFutureDateSelection diff --git a/Client Scripts/PreventFutureDateSelection b/Client Scripts/PreventFutureDateSelection new file mode 100644 index 0000000000..8748c2a666 --- /dev/null +++ b/Client Scripts/PreventFutureDateSelection @@ -0,0 +1,13 @@ +function onChange(control, oldValue, newValue, isLoading) { + if (isLoading || newValue == '') { + return; + } + + var selectedDate = new GlideDate(newValue); + var currentDate = new GlideDate(); + + if (selectedDate > currentDate) { + g_form.clearValue('your_date_field'); // Replace 'your_date_field' with the actual field name + alert('Future dates are not allowed.'); + } +} From 4540849508c0e63f68e19f79e6812c8bd29cbd38 Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Mon, 30 Oct 2023 12:44:51 +0530 Subject: [PATCH 04/13] Create OpenGooglePage Table: Specify the table on which you want this UI action to appear (e.g., Incident). Order: Set the desired order for the UI action's appearance in the list. Active: Check the box to make the UI action active. Table: Select the table where you want to create the UI action (e.g., Incident). --- UI Actions/OpenGooglePage | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 UI Actions/OpenGooglePage diff --git a/UI Actions/OpenGooglePage b/UI Actions/OpenGooglePage new file mode 100644 index 0000000000..697480c249 --- /dev/null +++ b/UI Actions/OpenGooglePage @@ -0,0 +1,11 @@ +(function executeUIAction(current, parent) { + var url = "https://www.google.com"; + var openInNewTab = true; + + if (openInNewTab) { + var win = window.open(url, "_blank"); + win.focus(); + } else { + window.location.href = url; + } +})(current, current); From d6502248f1d9cb13c8a2c72d05375bbb8cf19817 Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Mon, 30 Oct 2023 13:03:51 +0530 Subject: [PATCH 05/13] GetAllActiveIncident This script will get all the Incident which are active --- Background Scripts/GetAllActiveIncident | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Background Scripts/GetAllActiveIncident diff --git a/Background Scripts/GetAllActiveIncident b/Background Scripts/GetAllActiveIncident new file mode 100644 index 0000000000..6f3466e762 --- /dev/null +++ b/Background Scripts/GetAllActiveIncident @@ -0,0 +1,8 @@ +var op = new GlideRecord('incident'); +op.addActiveQuery(); +op.query(); +while(op.next()) +{ +gs.log('Incident Number :'+op.number); +gs.log('Incident Description : '+op.short_description); +} From 5f75d06ff74d6dad43a7cf503a2855e818a2a1a1 Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Tue, 31 Oct 2023 00:43:17 +0530 Subject: [PATCH 06/13] Create UpdatePriorityBasedOnImpact.js we're update the priority of incidents based on their impact. --- Background Scripts/UpdatePriorityBasedOnImpact.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Background Scripts/UpdatePriorityBasedOnImpact.js diff --git a/Background Scripts/UpdatePriorityBasedOnImpact.js b/Background Scripts/UpdatePriorityBasedOnImpact.js new file mode 100644 index 0000000000..a69e719c7c --- /dev/null +++ b/Background Scripts/UpdatePriorityBasedOnImpact.js @@ -0,0 +1,15 @@ + +var gr = new GlideRecord('incident'); +gr.addQuery('active', true); +gr.addQuery('impact', '2'); +gr.query(); +while (gr.next()) { + + if (gr.impact == '2') { + gr.priority = '1'; + } else { + gr.priority = '2'; + } + gr.update(); +} + From 5811d4fcaceb5aaaaffdeff5ceac7a2c10985bb3 Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Tue, 31 Oct 2023 00:47:55 +0530 Subject: [PATCH 07/13] Create CreateIncident.js we're creating new incident records --- Background Scripts/CreateIncident.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Background Scripts/CreateIncident.js diff --git a/Background Scripts/CreateIncident.js b/Background Scripts/CreateIncident.js new file mode 100644 index 0000000000..83a3227bbb --- /dev/null +++ b/Background Scripts/CreateIncident.js @@ -0,0 +1,7 @@ + +var gr = new GlideRecord('incident'); +gr.initialize(); +gr.short_description = 'New Incident'; +gr.description = 'This is a new incident created by a background script.'; +gr.insert(); +gs.log('New incident created with sys_id: ' + gr.sys_id); From 3ee74cc495f67c4d03a7aad3776e24f2ae9ea34a Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Tue, 31 Oct 2023 00:53:48 +0530 Subject: [PATCH 08/13] Create UpdateIncident.js Update Incident whose state is open so update the priority --- Background Scripts/Update Incident/UpdateIncident.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Background Scripts/Update Incident/UpdateIncident.js diff --git a/Background Scripts/Update Incident/UpdateIncident.js b/Background Scripts/Update Incident/UpdateIncident.js new file mode 100644 index 0000000000..a74e385615 --- /dev/null +++ b/Background Scripts/Update Incident/UpdateIncident.js @@ -0,0 +1,7 @@ +var gr = new GlideRecord('incident'); +gr.addQuery('state', '1'); +gr.query(); +while (gr.next()) { + gr.priority = '2'; + gr.update(); +} From 6a809de6db229c5c47a593597306185985d1d8d4 Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Tue, 31 Oct 2023 01:01:02 +0530 Subject: [PATCH 09/13] Create DeleteIncident.js Delete only those incident which are closed --- .../Delete Closed Incident/DeleteIncident.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Background Scripts/Delete Closed Incident/DeleteIncident.js diff --git a/Background Scripts/Delete Closed Incident/DeleteIncident.js b/Background Scripts/Delete Closed Incident/DeleteIncident.js new file mode 100644 index 0000000000..f39e58b0c3 --- /dev/null +++ b/Background Scripts/Delete Closed Incident/DeleteIncident.js @@ -0,0 +1,8 @@ +var gr = new GlideRecord('incident'); +gr.addQuery('state', '7'); +gr.query(); +var count = 0; +while (gr.next()) { + gr.deleteRecord(); + count++; +} From 98771cd8c85697b6170597710cf215a5f685766c Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Tue, 31 Oct 2023 01:02:56 +0530 Subject: [PATCH 10/13] Create readme.md --- Background Scripts/Delete Closed Incident/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Background Scripts/Delete Closed Incident/readme.md diff --git a/Background Scripts/Delete Closed Incident/readme.md b/Background Scripts/Delete Closed Incident/readme.md new file mode 100644 index 0000000000..74ee0fa4e5 --- /dev/null +++ b/Background Scripts/Delete Closed Incident/readme.md @@ -0,0 +1 @@ +Delete All those incident which are closed so this snippet is basically for that From 702f0eae8023a2880684e6dc1a1ea5a17de11476 Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:34:52 +0530 Subject: [PATCH 11/13] Create CurrentMonth.js --- Background Scripts/Get Current Month/CurrentMonth.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Background Scripts/Get Current Month/CurrentMonth.js diff --git a/Background Scripts/Get Current Month/CurrentMonth.js b/Background Scripts/Get Current Month/CurrentMonth.js new file mode 100644 index 0000000000..fc3bbd42e3 --- /dev/null +++ b/Background Scripts/Get Current Month/CurrentMonth.js @@ -0,0 +1,9 @@ +var currentDate = new GlideDate(); +var monthNumber = currentDate.getMonth(); +var monthNames = [ + "Jan", "Feb", "Mar", "Apr", "May", "June", + "July", "Aug", "Sept", "OCT", "Nov", "Dec" +]; + +var monthName = monthNames[monthNumber - 1]; +gs.log(monthName); From 553c5fe6a790ce93d4c2cfcd70c6411d13a290ef Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:39:31 +0530 Subject: [PATCH 12/13] Create readme.md --- Background Scripts/Get Current Month/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Background Scripts/Get Current Month/readme.md diff --git a/Background Scripts/Get Current Month/readme.md b/Background Scripts/Get Current Month/readme.md new file mode 100644 index 0000000000..6b69e96c63 --- /dev/null +++ b/Background Scripts/Get Current Month/readme.md @@ -0,0 +1 @@ +Code Snippets is just to get the current month name From 01947f04c7f2d8517de222fc51644f14b22c5883 Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Fri, 3 Oct 2025 12:39:42 +0530 Subject: [PATCH 13/13] Create Incident with priority as P1 The script uses the GlideRecord API to interact with the incident table and inserts a new record with the specified values --- Incident with priority as P1 | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Incident with priority as P1 diff --git a/Incident with priority as P1 b/Incident with priority as P1 new file mode 100644 index 0000000000..acd5f1b4c0 --- /dev/null +++ b/Incident with priority as P1 @@ -0,0 +1,11 @@ +// This script can be used in a Script Include or Business Rule +var incidentGR = new GlideRecord('incident'); +incidentGR.initialize(); +incidentGR.short_description = 'Critical Incident - Immediate Attention Required'; +incidentGR.description = 'This is a Priority 1 incident created via script.'; +incidentGR.priority = 1; // Priority 1 +incidentGR.impact = 1; // High impact +incidentGR.urgency = 1; // High urgency +incidentGR.caller_id = gs.getUserID(); // Sets the current user as the caller +incidentGR.category = 'network'; // Example category +incidentGR.insert();