From 443c763bbf211321944d29b5579e24d7dfc7247a Mon Sep 17 00:00:00 2001 From: Willem Date: Thu, 9 Oct 2025 15:51:37 +0200 Subject: [PATCH 1/2] Update script.js --- .../Clear all fields/script.js | 69 ++++++++++++------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/Client-Side Components/Catalog Client Script/Clear all fields/script.js b/Client-Side Components/Catalog Client Script/Clear all fields/script.js index 8a5b64fb2d..4c5a12a099 100644 --- a/Client-Side Components/Catalog Client Script/Clear all fields/script.js +++ b/Client-Side Components/Catalog Client Script/Clear all fields/script.js @@ -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); - } - }); - } -} \ No newline at end of file + 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; +} From 5893bd44ffa7a076274c7a8baec2ebda5939b664 Mon Sep 17 00:00:00 2001 From: Willem Date: Thu, 9 Oct 2025 16:02:54 +0200 Subject: [PATCH 2/2] Update README.md Updated readme to better describe existing functionality and included the new functionality. Also included a more descriptive example. --- .../Clear all fields/README.md | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Client-Side Components/Catalog Client Script/Clear all fields/README.md b/Client-Side Components/Catalog Client Script/Clear all fields/README.md index d058526e5f..0a84c1175e 100644 --- a/Client-Side Components/Catalog Client Script/Clear all fields/README.md +++ b/Client-Side Components/Catalog Client Script/Clear all fields/README.md @@ -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. \ No newline at end of file +### Example +``` +clearFields(['short_description', 'priority']); +``` +// Clears all fields except 'short_description' and 'priority'