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

Based on the model populate the assets

vinod6
Tera Contributor

In this scenario, the catalog task contains the 'Model' field (table: cmdb_model) and the 'Asset' field (table: alm_asset), which are reference fields. Based on the selected model, assets should populate. I am using an advanced reference qualifier, but the script below is not working. Can you please provide the correct script

 

 

Script inculde:

 

var CostcoHAMNonstandardUtils = Class.create();
CostcoHAMNonstandardUtils.prototype = {
    initialize: function() {
    },
getAssets: function(u_asset) {
        var assets = [];
        var assetsGr = new GlideRecord("alm_asset");

        if (u_asset) {
            assetsGr.addQuery('model', u_asset);
        }

        assetsGr.query();

        while (assetsGr.next()) {
            assets.push(assetsGr.sys_id.toString());
        }

        if (assets.length > 0) {
            return 'sys_idIN' + assets.join(',');
        } else {
            return ''; // Handle case where no assets are found
        }
    },
    type: 'CostcoHAMNonstandardUtils'
};
 
Reference Qualifier:
vinod6_1-1732532471009.png

Thanks,

4 REPLIES 4

Kieran Anson
Kilo Patron

Hi,

You shouldn't need to use a script include call - you'd also run into a limit with returning a string of sys_ids as you could be returning thousands of records

 

Instead

javascript: 'model =' current.variables.name_of_variable_for_model

You then need to add the following into the variable attributes

ref_qual_elements=variable_name_for_model

Brad Bowman
Kilo Patron
Kilo Patron

Can you clarify if you have added model and asset fields to the sc_task table, or are these variables in the Catalog Item?  If they are variables, use current.variables.var_name in the reference qualifier.  Based on your scenario, it sounds like you want to pass the selected model to the Script Include, but it looks like you are passing the asset? If you are still not getting expected results you can add some temporary gs.addInfoMessage lines to the Script Include to confirm the value passed in from the qualifier, and see if any records are retrieved by the GlideRecord.

model and asset fields to the sc_task table only.

 

The approach by @Kieran Anson is better in this scenario - without the .variables. since this is a field on the table, and you need to concatenate:

javascriptt: 'model=' + current.u_model

of course with just one t in javascriptt