Calling script include in UI action condition

shaik17
Tera Contributor

Hi all,

i have to show a UI action based on condition, i have created a background script and run it shows result. based on background i have created script include and called in UI action. But now working.

Condition: Itemized List contains configuration item or items of computer class

 

i am providing all the scripts with screenshots, Please assist me.

 

Background script:

 

var gr = new GlideRecord('sc_req_item');
if (gr.get('0a8e81ec478e5a50ef86bc8f016d4314')) { // Replace with your RITM sys_id

    var items = gr.variables.itemized_list.toString();
    var parseObj = JSON.parse(items);

    if (Array.isArray(parseObj)) {
        var hasComputerClass = false; 

        for (var count = 0; count < parseObj.length; count++) {
            var mrvs = parseObj[count];

            // Log MRVS fields
            gs.info('Item description: ' + mrvs.item_description);
            gs.info('Configuration item: ' + mrvs.configuration_item);

            // Check if the configuration_item exists
            if (mrvs.configuration_item) {
                var ciGR = new GlideRecord('cmdb_ci');
                if (ciGR.get(mrvs.configuration_item)) { // Query the CMDB for the configuration item
                    gs.info('Configuration Item Name: ' + ciGR.name + ' | Class: ' + ciGR.sys_class_name);

                    // Check if the sys_class_name is 'cmdb_ci_computer'
                    if (ciGR.sys_class_name == 'cmdb_ci_computer') {
                        hasComputerClass = true;
                        gs.info('Computer class item found: ' + ciGR.name);
                    }
                } else {
                    gs.info('Configuration Item not found in CMDB: ' + mrvs.configuration_item);
                }
            }
        }

        if (hasComputerClass) {
            gs.info('The Itemized List contains at least one Computer class configuration item.');
        } else {
            gs.info('No Computer class configuration items found in the Itemized List.');
        }
    } else {
        gs.info('The Itemized List is empty or not a valid array.');
    }
}

 

 

Result for Background script:

shaik17_0-1732628433459.png

 

Script include:

 

var ItemizedListUtils = Class.create();
ItemizedListUtils.prototype = {
    initialize: function() {},

    // Function to check if itemized list contains any CI of class cmdb_ci_computer
    hasComputerClassItems: function(ritmSysId) {
        if (!ritmSysId) {
            gs.error("RITM sys_id is required.");
            return false;
        }

        var gr = new GlideRecord('sc_req_item');
        if (!gr.get(ritmSysId)) {
            gs.error("RITM record not found for sys_id: " + ritmSysId);
            return false;
        }

        var items = gr.variables.itemized_list; // Access the MRVS field
        if (!items) {
            gs.error("Itemized List is null or undefined.");
            return false;
        }

        try {
            var parseObj = JSON.parse(items.toString());
            if (Array.isArray(parseObj)) {
                for (var count = 0; count < parseObj.length; count++) {
                    var mrvs = parseObj[count];
                    if (mrvs.configuration_item) {
                        var ciGR = new GlideRecord('cmdb_ci');
                        if (ciGR.get(mrvs.configuration_item)) {
                            if (ciGR.sys_class_name == 'cmdb_ci_computer') {
                                // Computer class item found
                                return true;
                            }
                        }
                    }
                }
            } else {
                gs.error("Itemized List is not a valid array.");
            }
        } catch (e) {
            gs.error("Error parsing Itemized List: " + e.message);
        }

        return false;
    },

    type: 'ItemizedListUtils'
};

 

 

shaik17_1-1732628511774.png

 

UI action :

shaik17_2-1732628560201.png
MRVS:

 

shaik17_0-1732629221909.png

 

 

There is anything wrong in UI action condition or script include please suggest me. also i used to check the script include with the help of background script it shows no values found even i have items in MRVS.
Thanks.

1 REPLY 1

Bhavya11
Kilo Patron

Hi @shaik17 ,

 

small changes needed as your script is excepting sysid of RITM but your passing the sys_id of catalog item.

 

 

please change below line in script include 

var items = gr.variables.itemized_list.toString();

Please change your condition like this 

 

new ItemizedListUtils().hasComputerClassItems(current.request_item) 

 

and if you want to show this button only to particular item then do like this create system property and save sysid of item there then  in ui action update like below

 

new ItemizedListUtils().hasComputerClassItems(current.request_item)  && && current.request_item.cat_item== gs.getProperty('imact.cat.item');

 

Please mark my solution as Accept, If you find it helpful.

 

 

Thanks,

BK