Restrict Catalog Item options by Inventory Amount

Michael H1
Tera Guru

Hi,

I haven't been able to find a specific question about this, but I want to perform the following: Hide/remove certain Model options from a catalog item field based on the "In Stock" inventory on our hardware table.

I know that I could perform the following: Create a script include which queries the desired amount of "In Stock" hardware records of the desired model, then use a catalog client script to call that script include, then hide the selectable model options.

The above does not seem like the most optimal route to go, however. While we do not maintain a huge CMDB, I feel like there is a more optimal way to perform this. Does anyone have anything like the above they have implemented?

2 REPLIES 2

Sid_Takali
Kilo Patron
Kilo Patron

Hi @Michael H1 You can refer below code

Script Include:

var AvailableModels = Class.create();
AvailableModels.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getAvailableModels: function() {
        var modelList = [];
        var model = this.getParameter('sysparm_model');
        var gr = new GlideRecord('your_hardware_table');
        gr.addQuery('model', model);
        gr.addQuery('in_stock', '>', 0);
        gr.query();
        while (gr.next()) {
            modelList.push(gr.model + ':' + gr.sys_id);
        }
        return JSON.stringify(modelList);
    }
});

 

onLoad Client Script: 

function onLoad() {
    var ga = new GlideAjax('AvailableModels');
    ga.addParam('sysparm_name', 'getAvailableModels');
    ga.addParam('sysparm_model', g_form.getValue('model_field_name'));
    ga.getXMLAnswer(function(response) {
        var models = JSON.parse(response);
        var allModels = g_form.getOption('model_field_name');
        var availableModels = {};
        
        for (var i = 0; i < models.length; i++) {
            availableModels[models[i].split(':')[1]] = models[i].split(':')[0];
        }
        
        g_form.clearOptions('model_field_name');
  
        for (var id in availableModels) {
            g_form.addOption('model_field_name', id, availableModels[id]);
        }
    });
}

 

 

Anubhav24
Mega Sage
Mega Sage

Hi @Michael H1 ,

Will getReference help you for this requirement if you do not want to go the GlideAjax way.

Please mark helpful/correct if my response helped you.