Skip to content

Commit ea4bba4

Browse files
authored
Merge pull request #1976 from WillemZeiler/refactor/clean-and-add-function
Refactor/clean and add function
2 parents 04c7e4c + 5893bd4 commit ea4bba4

File tree

2 files changed

+57
-31
lines changed

2 files changed

+57
-31
lines changed
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+
}

0 commit comments

Comments
 (0)