Skip to content

Commit 6b01c99

Browse files
authored
Merge branch 'main' into get-key-value-from-json
2 parents d0aa25e + 5d284c5 commit 6b01c99

File tree

11 files changed

+193
-0
lines changed

11 files changed

+193
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
In ServiceNow, Open catalog client Scripts [catalog_script_client] and paste the code snippet of [spModalSweetAlerts.js] file.
2+
3+
Setup:
4+
1. A catalog item having variable name Rewards[rewards] of type 'Select Box'(include none as true) and 2 choices(Yes and No)
5+
2. A Single line type field named 'Reward Selected' [reward_selected] which will hold the value selected by user from the spModal popup.
6+
3. The onLoad catalog client script setup as below:
7+
4. Type: onChange
8+
5. Variable: rewards (as per step 1)
9+
6. Script: [[spModalSweetAlerts.js]]
10+
11+
12+
13+
Screenshots:
14+
15+
16+
<img width="1338" height="268" alt="image" src="https://github.com/user-attachments/assets/f7f22b83-7e0e-47bb-bbed-2a8f38783a4d" />
17+
18+
19+
Rewards selected as 'Yes'
20+
21+
<img width="1353" height="327" alt="image" src="https://github.com/user-attachments/assets/1bb55339-36b4-4a9c-8b65-2b254b87cf5b" />
22+
23+
From the spModal popup select anyone of the reward, it should be populated in the Reward Selected field.
24+
Along with that a message shows the same of the selection.
25+
26+
<img width="1350" height="319" alt="image" src="https://github.com/user-attachments/assets/1b23c766-51f8-4b01-9073-f836f390deb2" />
27+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function onChange(control, oldValue, newValue) {
2+
if (newValue == 'Yes') {
3+
spModal.open({
4+
title: "Reward Type",
5+
message: "Please select the category of Reward",
6+
buttons: [{
7+
label: "Star Performer",
8+
value: "Star Performer"
9+
},
10+
{
11+
label: "Emerging Player",
12+
value: "Emerging Player"
13+
},
14+
{
15+
label: "High Five Award",
16+
value: "High Five Award"
17+
},
18+
{
19+
label: "Rising Star",
20+
value: "Rising Star"
21+
}
22+
]
23+
}).then(function(choice) {
24+
if (choice && choice.value) {
25+
g_form.addInfoMessage('Selected Reward: '+ choice.label);
26+
g_form.setValue('reward_selected', choice.value);
27+
}
28+
});
29+
} else {
30+
g_form.clearValue('reward_selected');
31+
}
32+
}
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
2+
var fieldToHide = 'subcategory'; // I have taken subcategory as an example
3+
if (newValue === '') {
4+
g_form.setDisplay(fieldToHide, false);
5+
return;
6+
}
7+
var ga = new GlideAjax('NumberOfDependentChoices');
8+
ga.addParam('sysparm_name', 'getCountOfDependentChoices');
9+
ga.addParam('sysparm_tableName', g_form.getTableName());
10+
ga.addParam('sysparm_element', fieldToHide);
11+
ga.addParam('sysparm_choiceName', newValue);
12+
ga.getXMLAnswer(callBack);
13+
14+
function callBack(answer) {
15+
g_form.setDisplay(fieldToHide, parseInt(answer) > 0 ? true : false);
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var NumberOfDependentChoices = Class.create();
2+
NumberOfDependentChoices.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
getCountOfDependentChoices: function() {
4+
var dependentChoiceCount = 0;
5+
var choiceName = this.getParameter('sysparm_choiceName');
6+
var tableName = this.getParameter('sysparm_tableName');
7+
var element = this.getParameter('sysparm_element');
8+
var choiceCountGa = new GlideAggregate('sys_choice');
9+
choiceCountGa.addAggregate('COUNT');
10+
choiceCountGa.addQuery('dependent_value',choiceName);
11+
choiceCountGa.addQuery('inactive','false');
12+
choiceCountGa.addQuery('name',tableName);
13+
choiceCountGa.addQuery('element',element);
14+
choiceCountGa.query();
15+
while(choiceCountGa.next()){
16+
dependentChoiceCount = choiceCountGa.getAggregate('COUNT');
17+
}
18+
return dependentChoiceCount;
19+
},
20+
type: 'NumberOfDependentChoices'
21+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Hide the dependent choice field when there are no available options for the selected parent choice.
2+
3+
For example, if a selected category on the incident form has no subcategories, then the subcategory field should be hidden.
4+
5+
The file NumberOfDependentChoices.js is a client callable script include file which has a method which returns number of dependent choices for a selected choice of parent choice field.
6+
7+
HideDepnedentField.js is client script which hides the dependent choice field(ex:subcategory field on incident form) if there are no active choices to show for a selected choices of it's dependent field (example: category on incident form)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
This business rule is designed for ServiceNow to prevent a parent incident from being closed or resolved while it still has active child incidents.
2+
If a user attempts to set the parent incident's state to "Resolved," "Closed," or "Cancelled," the rule will query for any related child incidents that are still open.
3+
If open children are found, the update will be aborted, and an error message will be displayed to the user.
4+
5+
Navigate to System Definition > Business Rules in the ServiceNow filter navigator.
6+
Click New.
7+
Fill out the form with the following details:
8+
Name: Prevent Parent Closure with Open Children
9+
Table: Incident [incident]
10+
Advanced: true
11+
When: before
12+
Update: Check this box.
13+
In the When to run tab, set the Condition field:
14+
current.state.changesTo(7) || current.state.changesTo(6) || current.state.changesTo(8) //The state values are: 6 (Resolved), 7 (Closed), 8 (Cancelled).
15+
Note: The state values (6, 7, 8) may vary based on your instance configuration.
16+
In the Advanced tab, paste the provided script into the Script field.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
4+
//When the Incident state values are set to: 6 (Resolved), 7 (Closed), 8 (Cancelled).
5+
// The `previous.state` check prevents the script from running when a closed ticket is re-closed.
6+
if ((current.state == '6' || current.state == '7' || current.state == '8') && current.state != previous.state) {
7+
8+
// Use GlideAggregate to efficiently count child incidents that are not yet closed.
9+
var ga = new GlideAggregate('incident');
10+
ga.addQuery('parent_incident', current.sys_id);
11+
ga.addActiveQuery();
12+
ga.addAggregate('COUNT');
13+
ga.query();
14+
var childCount = 0;
15+
if (ga.next()) {
16+
// Retrieve the aggregated count.
17+
childCount = ga.getAggregate('COUNT');
18+
}
19+
// If open child incidents are found, abort the parent's closure and display an error.
20+
if (childCount > 0) {
21+
gs.addErrorMessage('Cannot close this incident. ' + childCount + ' child incidents are still open.');
22+
current.setAbortAction(true);
23+
}
24+
}
25+
26+
})(current, previous);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
//Encode Before Saving Trigger: Before Insert/Update
4+
5+
(function executeRule(current, previous) {
6+
if (current.fieldName.changes()) {
7+
var plainText = current.fieldName + '';
8+
current.fieldName = GlideStringUtil.base64Encode(plainText);
9+
}
10+
})(current, previous);
11+
//If the field is new or updated, encodes its value to Base64. Saves the encoded string to the database.
12+
13+
// For Example User enters Hello World in fieldName.
14+
//Before saving : System encodes and stores SGVsbG8gV29ybGQ= in the database.
15+
//When the record is viewed : Display rule decodes it back to Hello World for the user.
16+
17+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Decode on Display Trigger: Before Display
2+
////Decodes fieldName from Base64. Shows decoded value on the form.
3+
4+
(function executeRule(current) {
5+
if (current.fieldName) { // field name can be anything
6+
try {
7+
var decoded = GlideStringUtil.base64Decode(current.fieldName);
8+
current.setDisplayValue('fieldName', decoded);
9+
} catch (ex) {
10+
current.setDisplayValue('fieldName', '[Invalid Base64]');
11+
}
12+
}
13+
})(current);
14+
15+
//If the field is new or updated, encodes its value to Base64. Saves the encoded string to the database.
16+
17+
// For Example User enters Hello World in fieldName.
18+
//Before saving : System encodes and stores SGVsbG8gV29ybGQ= in the database.
19+
//When the record is viewed : Display rule decodes it back to Hello World for the user.

0 commit comments

Comments
 (0)