The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Prevent submission if number of availability in stock is less than requested item quantity

profile
Tera Contributor

· 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.
The requirement is a check in one of the maintain item called 

1. Create a Catalog Item: IT Hardware Request

· Variables to include:

o Request Type (Choice Field): Values - Laptop, Desktop, Monitor, Printer, Accessories.

o Model (Dependent Choice Field): Dynamic options based on Request Type selection.

§ Laptop: Model A, Model B, Model C

§ Desktop: Model X, Model Y, Model Z

§ Monitor: 24-inch, 27-inch, 32-inch

§ Printer: Inkjet, Laser, All-in-One

§ Accessories: Mouse, Keyboard, Docking Station

o Quantity (Integer Field): 1-5 items.

o Business Justification (Multi-line Text): Minimum 100 characters.

o Priority (Choice Field): Values - Low, Medium, High.

o Location (Reference Field): References the Location table.

o Urgency (Choice Field): Values - Standard, Urgent.

o Requested For (Reference Field): References the User table.

o Cost Center (Reference Field): References the Cost Center table.

2. Implement Client-Side (Front-End) Validations:

· Dynamic Field Visibility:

o Display the Model field dynamically based on the Request Type selection. Ensure that the user selects an appropriate model.

· Field Validation:

o Mandatory Fields: Ensure Request Type, Model, Quantity, and Business Justification are mandatory.

o Character Limit: Ensure the Business Justification has a minimum of 100 characters.

o Quantity Validation: Restrict the Quantity field to values between 1 and 5.

o Urgency-Based Priority: If Urgency is set to Urgent, automatically set Priority to High. This should be done using a client script.

· Pre-fill and Read-Only Fields:

o Pre-fill the Requested For field with the logged-in user but allow them to change it if needed.

o Make the Cost Center field read-only and auto-populate it based on the Requested For user’s department.
Now how to write script for this requirement 

· 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.

1 REPLY 1

Deepak Shaerma
Kilo Sage

hi @profile 

you'll use a Catalog Client Script with an asynchronous GlideAjax call to a Script Include that queries your stock records (probably in a custom cmdb or stock table).

Script Include Sample COdde:

var CheckStockAvailability = Class.create();
CheckStockAvailability.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkStock: function() {
        var model = this.getParameter('sysparm_model');
        var qty = parseInt(this.getParameter('sysparm_quantity'), 10);

        var gr = new GlideRecord('u_hardware_stock');
        gr.addQuery('u_model', model);
        gr.query();

        if (gr.next()) {
            var available = parseInt(gr.getValue('u_available_quantity'), 10);
            if (available >= qty) {
                return 'OK';
            } else {
                return 'Only ' + available + ' items available for model ' + model;
            }
        }
        return 'Model not found in stock';
    }
});


On Submit Cataalog Client Script:

function onSubmit() {
    var model = g_form.getValue('model');
    var quantity = parseInt(g_form.getValue('quantity'), 10);

    var ga = new GlideAjax('CheckStockAvailability');
    ga.addParam('sysparm_name', 'checkStock');
    ga.addParam('sysparm_model', model);
    ga.addParam('sysparm_quantity', quantity);

    var answer = '';
    ga.getXMLWait(function(response) {
        answer = response.responseXML.documentElement.getAttribute('answer');

        if (answer !== 'OK') {
            g_form.showFieldMsg('model', answer, 'error');
            return false;
        }
    });

    return true;
}

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma