Validate Quantity in stockroom before transfer: onChange Client Script

Jimmy45
Giga Guru

Hi community,

I have a requirement. On the front end (portal form) there is a need to create a catalog item to transfer either hardware or consumables from one stockroom to another. Currently these are the variables on the catalog item:

model_transfer - Reference - cmdb_model table - Select the model to transfer

trnsfr_from_stockroom - Reference - alm_stockroom - Select the stockroom you are transferring from
trnsfr_to_stockroom - Reference - alm_stockroom

trnsfr_qty - String - regex: number - How many of this model are you transferring?

 

What I am trying to do is to create an onChange Catalog Client script targeting the model_transfer field, onChange

Whatever number is entered into this field, the client script should add parameters and call the script include where it will first determine if the model_transfer field (model selected) is model category of "consumable" or "Personal Computer"


If "Consumable, query the alm_consumable table for the model that matches the value in the model_transfer field and also matches the stockroom found in the trnsfr_from_stockroom and its state = 6 (In stock) and substatus = 'available and checks to ensure that the quantity field value is greater than or equal to the quantity placed in the trnsfr_qty field on the form. If it is greater, then return "true", if it is less than, return "false"

If the model_trnsfr field has a model whose model category != Consumable, then query the alm_hardware table for the model selected in the model_trnsfr field (returns the sys_id) where the stockroom is not empty, and the stockroom matches the value in the field trnsfr_from_stockroom and its state = 6 (In stock) and substatus = 'available', while. I know that for alm_hardware I need to use getRowCount() and if the number of rows is greater than the number placed in the trnsfr_qty variable field, then it allows the entry and results in "Good Job!" BUT if the number placed in the trnsfr_qty field is greater than the row count for the query, then to throw an error message "Not enough items in stock!" and it clears the value.

My problem right now is I have (hardware) 26 items of a model I selected in the model_transfer field.
I selected the proper from stockroom

I selected the stockroom transferring to (doesn't matter)

I typed in 2 -Good Job
I typed in 20 - Good Job

I typed in 100 - Good Job (This was not supposed to return true and throw this message)

Same result with consumables.

Here is my Client script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var stockroomId = g_form.getValue('variables.trnsfr_from_stockroom');
    var modelId = g_form.getValue('variables.transfer_model');
    var requestedQuantity = g_form.getValue('variables.trnsfr_qty');
    var ga = new GlideAjax('InventorytransferUtils');
    ga.addParam('sysparm_name', 'validateTransfer');
    ga.addParam('sysparm_stockroomId', stockroomId); // Pass stockroomId as a parameter
    ga.addParam('sysparm_modelId', modelId); // Pass modelId as a parameter
    ga.addParam('sysparm_requestedQuantity', requestedQuantity); // Pass requestedQuantity as a parameter
    ga.getXML(checkResults);


    function checkResults(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'true') {
            g_form.addErrorMessage('Good Job!');
            this.document.getElementsByName('submit')[0].disabled = true;
        } else
        if (answer == 'false') {
            g_form.addErrorMessage('Not enough items in stock!');

        }

    }


}

 

 

Here is he Script Include Function:

 

    validateTransfer: function(ritm) {
        var answer;
        var stockroomId = this.getParameter('sysparm_stockroomId');
        var modelId = this.getParameter('sysparm_modelId');
        var requestedQuantity = this.getParameter('sysparm_requestedQuantity');
        var conAsset = new GlideRecord('alm_consumable');
        conAsset.addEncodedQuery('model_category=218323293743100044e0bfc8bcbe5d61^install_status=6^substatus=available^stockroom=' + stockroomId + '^model=' + modelId);
        conAsset.query();
        while (conAsset.next()) {
            if (conAsset.model_category == '218323293743100044e0bfc8bcbe5d61' && conAsset.quantity >= requestedQuantity) {
                answer = 'true'; // If the condition is met, return true.
            }
        }
        // If the loop finishes without returning true, check alm_hardware.
        var hardware = new GlideRecord('alm_hardware');
        hardware.addEncodedQuery('install_status=6^substatus=available^stockroom=' + stockroomId + '^model=' + modelId);
        hardware.query();
        if(hardware.getRowCount() >= requestedQuantity){
            answer = 'true'; // If the row count is greater, return true.
        } else 
            answer = 'false';
        // If neither condition is met, return false.
        return answer;

    },

 

 

Thanks in advance. I am active on here, so I will definitely give credit where due!  

1 ACCEPTED SOLUTION

Jimmy45
Giga Guru

Found the issue. it is not model_transfer.model_category, but is model_transfer.cmdb_model_category since it is the model that is being dot walked!

View solution in original post

5 REPLIES 5

Jimmy45
Giga Guru

Found the issue. it is not model_transfer.model_category, but is model_transfer.cmdb_model_category since it is the model that is being dot walked!