Prevent submission if number of availability in stock is less than requested item quantity
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2025 05:52 AM
Implement Server-Side (Back-End) Validations:
· Stock Availability Check:
o Before the form is submitted, check if the selected Model is available in the stock. If the stock is insufficient, prevent submission and display an appropriate error message.
I am trying with this script which is not working. Please can any one able to correct it or give me a correct code:
Script Include Code:
var CheckStockAvailability = Class.create();
CheckStockAvailability.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkStock: function() {
var model = this.getParameter('sysparm_model');
var qty = parseInt(this.getParameter('sysparm_quantity1'), 10); // Use quantity1, base 10
// Validate quantity
if (isNaN(qty) || qty < 1 || qty > 5) {
return 'Invalid quantity. Must be between 1 and 5';
}
var gr = new GlideRecord('alm_asset');
gr.addQuery('display_name', model);
gr.query();
if (gr.next()) {
var available = parseInt(gr.getValue('quantity'), 10); // Check against out-of-box quantity field
if (available >= qty) {
return 'OK';
} else {
return 'Only ' + available + ' items available for model ' + model;
}
}
return 'Model not found in stock';
},
type: 'CheckStockAvailability'
});
Catalog Client Script code:
Catalog Client Script code:
function onSubmit() {
var model = g_form.getValue('model');
var quantity = parseInt(g_form.getValue('quantity1'), 10); // Use quantity1 field
// Client-side validation
if (isNaN(quantity) || quantity < 1 || quantity > 5) {
g_form.showFieldMsg('quantity1', 'Quantity must be between 1 and 5', 'error');
return false;
}
var ga = new GlideAjax('CheckStockAvailability');
ga.addParam('sysparm_name', 'checkStock');
ga.addParam('sysparm_model', model);
ga.addParam('sysparm_quantity1', quantity); // Pass quantity1
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer !== 'OK') {
g_form.showFieldMsg('quantity1', answer, 'error');
g_form.setSubmit(false); // Prevent form submission
} else {
g_form.setSubmit(true); // Allow form submission
}
});
return false; // Prevent default submission until Ajax completes
}
1 REPLY 1

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2025 07:14 AM
There is a typo in one condition operator
Replace
if (answer !== 'OK') {
with
if (answer != 'OK') {
Thank you,
Palani
Palani