diff --git a/Client-Side Components/Catalog Client Script/Set and Lock Variable by Group/README.md b/Client-Side Components/Catalog Client Script/Set and Lock Variable by Group/README.md new file mode 100644 index 0000000000..e7d932757d --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Set and Lock Variable by Group/README.md @@ -0,0 +1,5 @@ +**Set and Lock Variable by Group** + +This solution provides a secure and dynamic way to control data entry on a Service Catalog form based on the user's group membership. It is typically used to pre-fill and lock certain justification or approval bypass fields for authorized users (like managers or executive staff), improving their efficiency while maintaining an accurate audit trail. + +This functionality requires a combined Client-side (Catalog Client Script) and Server-side (Script Include) approach to ensure the group check is done securely. diff --git a/Client-Side Components/Catalog Client Script/Set and Lock Variable by Group/set_lock_variable_by_grp.js b/Client-Side Components/Catalog Client Script/Set and Lock Variable by Group/set_lock_variable_by_grp.js new file mode 100644 index 0000000000..b1c04555c7 --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Set and Lock Variable by Group/set_lock_variable_by_grp.js @@ -0,0 +1,33 @@ +// onload Catalog Client Script with Catalog Name +function onLoad() { + var variableName = 'bypass_approval_reason'; + var targetGroupName = 'ServiceNow Support'; // The group authorized to skip this step + var ga = new GlideAjax('UserUtils'); + ga.addParam('sysparm_name', 'isMemberOf'); + ga.addParam('sysparm_group_name', targetGroupName); + ga.getXMLAnswer(checkAndLockVariable); + function checkAndLockVariable(response) { + var isMember = response; + if (isMember == 'true') { + var message = 'Value set and locked due to your ' + targetGroupName + ' membership.'; + var setValue = 'Bypassed by authorized ' + targetGroupName + ' member.'; + g_form.setValue(variableName, setValue); + g_form.setReadOnly(variableName, true); + g_form.showFieldMsg(variableName, message, 'info'); + } else { + g_form.setReadOnly(variableName, false); + } + } +} + +//Script Include +var UserUtils = Class.create(); +UserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { + isMemberOf: function() { + var groupName = this.getParameter('sysparm_group_name'); + var isMember = gs.getUser().isMemberOf(groupName); + return isMember.toString(); + }, + + type: 'UserUtils' +});