Skip to content

Commit 119664a

Browse files
authored
Pre-Validation for Hardware Availability in Catalog Requests (#2622)
* Create Readme.JS * Create submit validation client script.js * Create on submit scriptinclude.JS * Update submit validation client script * Rename Readme.JS to Readme.md * Rename submit validation client script to submit validation client script.js
1 parent 329be68 commit 119664a

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var HardwareValidationUtils = Class.create();
2+
HardwareValidationUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
4+
validateHardware: function() {
5+
var hardware = this.getParameter('sysparm_hardware');
6+
var qty = parseInt(this.getParameter('sysparm_quantity'), 10);
7+
8+
if (!hardware || isNaN(qty)) {
9+
return 'Invalid input!';
10+
}
11+
12+
var gr = new GlideRecord('u_hardware_inventory');
13+
if (gr.get(hardware)) {
14+
var availableQty = parseInt(gr.getValue('available_quantity'), 10);
15+
if (availableQty >= qty) {
16+
return 'OK';
17+
} else {
18+
return 'Not enough stock available!';
19+
}
20+
}
21+
return 'Hardware not found!';
22+
},
23+
24+
type: 'HardwareValidationUtils'
25+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function onSubmit() {
2+
3+
4+
var hardware = g_form.getValue('hardware_name');
5+
var qty = g_form.getValue('quantity');
6+
7+
8+
var ga = new GlideAjax('HardwareValidationUtils');
9+
ga.addParam('sysparm_name', 'validateHardware');
10+
ga.addParam('sysparm_hardware', hardware);
11+
ga.addParam('sysparm_quantity', qty);
12+
13+
14+
ga.getXMLAnswer(function(response) {
15+
16+
17+
if (response !== 'OK') {
18+
alert(response);
19+
20+
g_form.addErrorMessage(response); // Optional inline error
21+
g_form.setSubmit(false); // Prevent submission in Service Portal
22+
} else {
23+
24+
g_form.setSubmit(true); // Allow submission
25+
}
26+
});
27+
28+
return false;
29+
}

0 commit comments

Comments
 (0)