diff --git a/Client-Side Components/Catalog Client Script/Onsubmit validation/Readme.md b/Client-Side Components/Catalog Client Script/Onsubmit validation/Readme.md new file mode 100644 index 0000000000..6de0662aab --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Onsubmit validation/Readme.md @@ -0,0 +1 @@ +This project adds pre-validation for hardware availability in ServiceNow Catalog Items. Before submitting a request, the system checks if the requested hardware is available in inventory and blocks submission if stock is insufficient. we can easy to extend other validations (budget, licenses, etc.).Improves user experience by validating before approval.Prevents unnecessary approvals and fulfillment. diff --git a/Client-Side Components/Catalog Client Script/Onsubmit validation/on submit scriptinclude.JS b/Client-Side Components/Catalog Client Script/Onsubmit validation/on submit scriptinclude.JS new file mode 100644 index 0000000000..bcb4631341 --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Onsubmit validation/on submit scriptinclude.JS @@ -0,0 +1,25 @@ +var HardwareValidationUtils = Class.create(); +HardwareValidationUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { + + validateHardware: function() { + var hardware = this.getParameter('sysparm_hardware'); + var qty = parseInt(this.getParameter('sysparm_quantity'), 10); + + if (!hardware || isNaN(qty)) { + return 'Invalid input!'; + } + + var gr = new GlideRecord('u_hardware_inventory'); + if (gr.get(hardware)) { + var availableQty = parseInt(gr.getValue('available_quantity'), 10); + if (availableQty >= qty) { + return 'OK'; + } else { + return 'Not enough stock available!'; + } + } + return 'Hardware not found!'; + }, + + type: 'HardwareValidationUtils' +}); diff --git a/Client-Side Components/Catalog Client Script/Onsubmit validation/submit validation client script.js b/Client-Side Components/Catalog Client Script/Onsubmit validation/submit validation client script.js new file mode 100644 index 0000000000..a521c0acfe --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Onsubmit validation/submit validation client script.js @@ -0,0 +1,29 @@ +function onSubmit() { + + + var hardware = g_form.getValue('hardware_name'); + var qty = g_form.getValue('quantity'); + + + var ga = new GlideAjax('HardwareValidationUtils'); + ga.addParam('sysparm_name', 'validateHardware'); + ga.addParam('sysparm_hardware', hardware); + ga.addParam('sysparm_quantity', qty); + + + ga.getXMLAnswer(function(response) { + + + if (response !== 'OK') { + alert(response); + + g_form.addErrorMessage(response); // Optional inline error + g_form.setSubmit(false); // Prevent submission in Service Portal + } else { + + g_form.setSubmit(true); // Allow submission + } + }); + + return false; +}