Skip to content

Commit c58e1a1

Browse files
Merge branch 'main' into add-incident-count-snippet
2 parents 6162b1e + ea4bba4 commit c58e1a1

4 files changed

Lines changed: 80 additions & 50 deletions

File tree

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# Clear all fields on a catalog item form
22

3-
This works on both the native platform and service portal / mobile. Typically used with an OnChange catalog client script when you would like to reset all the fields after a certain variable is changed.
3+
This function clears all editable fields on a form, except those explicitly excluded.
4+
It works on both the native platform (Classic UI) and Service Portal / Mobile.
5+
Typically used with an OnChange catalog client script when you want to clear all fields after a certain variable changes.
46

5-
This function does support an exclusion list if there are fields you would like to exclude from being reset, typically you would want to add the field that triggered to the change to the exlusion
7+
The function returns an array of the field names that were cleared, which can be used for logging or further processing.
68

7-
### Example
9+
### Exclusion Support
810

9-
```js
10-
clearFields(['field1', 'field2']);
11-
```
11+
You can pass an array of field names to exclude from being cleared.
12+
This is useful when you want to preserve the value of the field that triggered the change or other important fields.
1213

13-
All fields on the form **except** field1 and field2 will be cleared.
14+
### Example
15+
```
16+
clearFields(['short_description', 'priority']);
17+
```
18+
// Clears all fields except 'short_description' and 'priority'
Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,47 @@
1-
/**SNDOC
2-
@name clearFields
3-
@description Clear/reset all fields on a form
4-
@param {Array} [dontClearFieldsArray] - Fields to not clear
5-
@example
6-
clearFields(['field1', 'field2']);
7-
*/
1+
/**
2+
* Clears or resets all editable fields on a form, except those explicitly excluded.
3+
* Compatible with Classic UI and Service Portal/Mobile.
4+
* Intended for use in onChange client scripts.
5+
*
6+
* @function clearFields
7+
* @param {Array} dontClearFieldsArray - Array of field names to exclude from clearing.
8+
* @returns {Array} - Array of field names that were cleared.
9+
*
10+
* @example
11+
* // Clears all fields except 'short_description' and 'priority'
12+
* clearFields(['short_description', 'priority']);
13+
*/
14+
function clearFields(dontClearFieldsArray) {
15+
// Ensure the exclusion list is defined and is an array
16+
dontClearFieldsArray = Array.isArray(dontClearFieldsArray) ? dontClearFieldsArray : [];
817

9-
function clearFields(dontClearFieldsArray){
18+
// Helper function to check if a field should be cleared
19+
function shouldClear(fieldName) {
20+
return dontClearFieldsArray.indexOf(fieldName) === -1;
21+
}
1022

11-
try{ // Classic UI
12-
var pFields = g_form.nameMap;
13-
pFields.forEach(function(field){
14-
if(dontClearFieldsArray.indexOf(field.prettyName) == -1){
15-
g_form.clearValue(field.prettyName);
16-
}
17-
});
18-
}catch(e){ // Service Portal or Mobile
19-
var fields = g_form.getEditableFields();
20-
fields.forEach(function(field){
21-
if(dontClearFieldsArray.indexOf(fields) == -1){
22-
g_form.clearValue(field);
23-
}
24-
});
25-
}
26-
}
23+
var clearedFields = [];
24+
25+
try {
26+
// Classic UI: use g_form.nameMap to get all fields
27+
var allFields = g_form.nameMap;
28+
allFields.forEach(function(field) {
29+
var fieldName = field.prettyName;
30+
if (shouldClear(fieldName)) {
31+
g_form.clearValue(fieldName);
32+
clearedFields.push(fieldName);
33+
}
34+
});
35+
} catch (e) {
36+
// Service Portal or Mobile: use getEditableFields()
37+
var editableFields = g_form.getEditableFields();
38+
editableFields.forEach(function(fieldName) {
39+
if (shouldClear(fieldName)) {
40+
g_form.clearValue(fieldName);
41+
clearedFields.push(fieldName);
42+
}
43+
});
44+
}
45+
46+
return clearedFields;
47+
}
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
**Client Script**
1+
# Mandatory Field Check on Form Change
22

3-
Client script which is showing how to use g_form.mandatoryCheck() function, which allows to easily detect if any of mandatory field is not filled on record.
3+
This client script demonstrates how to use `g_form.mandatoryCheck()` to validate whether all mandatory fields on a form are filled.
44

5-
**Example configuration**
5+
It is typically used in an **onChange** catalog client script to trigger validation when a specific field changes.
66

7-
![Coniguration](ScreenShot_1.PNG)
7+
If mandatory fields are missing, the script:
8+
- Displays an info message listing the missing fields. Note: the red message is the existing functionality that will give you an error message.
9+
- Visually highlights each missing field using a flashing effect to guide the user.
810

9-
**Example execution**
11+
This approach improves user experience by clearly indicating which fields require attention.
1012

11-
![Execution](ScreenShot_2.PNG)
13+
**Example configuration**
14+
<img width="1122" height="638" alt="image" src="https://github.com/user-attachments/assets/31f86ae7-27fe-4921-8d8b-391eaa55304d" />
15+
16+
**Example execution**
17+
<img width="1029" height="495" alt="image" src="https://github.com/user-attachments/assets/b8384507-4292-41f4-9155-9be10195493e" />
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
2-
if (isLoading || newValue === '') {
3-
return;
4-
}
5-
6-
//Client script, which shows how to use mandatoryCheck() function
7-
//mandatoryCheck() allows validating if all mandatory fields are filled
8-
//If all mandatory fields are filled it return true, otherwise it returns false
2+
if (isLoading || newValue === '') {
3+
return;
4+
}
95

10-
//Check if all mandatory fields are filled on record
6+
// Check if all mandatory fields are filled
117
if (!g_form.mandatoryCheck()) {
12-
13-
//Example action when not all mandatory fields are filled - display message and remove state field
8+
//if not get all missing fields
149
var missing = g_form.getMissingFields();
15-
g_form.addInfoMessage("State field removed, because not all mandatory fields are filled: " + missing);
16-
g_form.setDisplay('state', false);
10+
//Info message displaying the fields that are missing
11+
g_form.addInfoMessage("Please complete the following mandatory fields: " + missing.join(", "));
12+
//go through missing fields and flash them
13+
missing.forEach(function (fieldName) {
14+
g_form.flash(fieldName, "#FFFACD",0); // Flash to draw attention
15+
});
1716
}
18-
1917
}

0 commit comments

Comments
 (0)