Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# Clear all fields on a catalog item form

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.
This function clears all editable fields on a form, except those explicitly excluded.
It works on both the native platform (Classic UI) and Service Portal / Mobile.
Typically used with an OnChange catalog client script when you want to clear all fields after a certain variable changes.

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
The function returns an array of the field names that were cleared, which can be used for logging or further processing.

### Example
### Exclusion Support

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

All fields on the form **except** field1 and field2 will be cleared.
### Example
```
clearFields(['short_description', 'priority']);
```
// Clears all fields except 'short_description' and 'priority'
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
/**SNDOC
@name clearFields
@description Clear/reset all fields on a form
@param {Array} [dontClearFieldsArray] - Fields to not clear
@example
clearFields(['field1', 'field2']);
*/
/**
* Clears or resets all editable fields on a form, except those explicitly excluded.
* Compatible with Classic UI and Service Portal/Mobile.
* Intended for use in onChange client scripts.
*
* @function clearFields
* @param {Array} dontClearFieldsArray - Array of field names to exclude from clearing.
* @returns {Array} - Array of field names that were cleared.
*
* @example
* // Clears all fields except 'short_description' and 'priority'
* clearFields(['short_description', 'priority']);
*/
function clearFields(dontClearFieldsArray) {
// Ensure the exclusion list is defined and is an array
dontClearFieldsArray = Array.isArray(dontClearFieldsArray) ? dontClearFieldsArray : [];

function clearFields(dontClearFieldsArray){
// Helper function to check if a field should be cleared
function shouldClear(fieldName) {
return dontClearFieldsArray.indexOf(fieldName) === -1;
}

try{ // Classic UI
var pFields = g_form.nameMap;
pFields.forEach(function(field){
if(dontClearFieldsArray.indexOf(field.prettyName) == -1){
g_form.clearValue(field.prettyName);
}
});
}catch(e){ // Service Portal or Mobile
var fields = g_form.getEditableFields();
fields.forEach(function(field){
if(dontClearFieldsArray.indexOf(fields) == -1){
g_form.clearValue(field);
}
});
}
}
var clearedFields = [];

try {
// Classic UI: use g_form.nameMap to get all fields
var allFields = g_form.nameMap;
allFields.forEach(function(field) {
var fieldName = field.prettyName;
if (shouldClear(fieldName)) {
g_form.clearValue(fieldName);
clearedFields.push(fieldName);
}
});
} catch (e) {
// Service Portal or Mobile: use getEditableFields()
var editableFields = g_form.getEditableFields();
editableFields.forEach(function(fieldName) {
if (shouldClear(fieldName)) {
g_form.clearValue(fieldName);
clearedFields.push(fieldName);
}
});
}

return clearedFields;
}
Loading