Help with Script Include Reference Qualifier for List Collector Variable

Brett21
Tera Guru

Hi Community,

 

My script include is NOT return the sys_id of my array which is ' var arr='' '.  In the log statements I am getting the list of sys_ids in "gs.info('CIs 1:' + linux.sys_id.toString());" but the log statement is empty for "gs.info('CIs 2:' + arr.toString());" leaving my list collector variable empty

 

My requirement is when a user selects either Unix or Windows via Radio BUtton the list collect ServerName should populate those CI classes.  I am using a script include that returns the sys_ids and calling in my variable the reference qual. 

List Collector variable:

Brett21_0-1738856783266.png

 

Script Include:

var deInstallServerType = Class.create();
deInstallServerType.prototype = {

    serverType: function(selectedVal) {

        var selectedVal = current.variables.idServerType; //Radio Button Unix/Linux or Windows
        gs.info("ServerType " + current.variables.idServerType + " var " + selectedVal);
        var arr = '';

        if (selectedVal == "stUnixLinux") {
            var linux = new GlideRecord("cmdb_ci_linux_server");
    linux.addEncodedQuery('nameISNOTEMPTY^sys_domain=dff860dcdbc02700cb2d70c08c9619ac^support_groupISNOTEMPTY^install_status=1^ORinstall_status=101');
            linux.query();
            while (linux.next()) {
                arr.push(linux.sys_id.toString());
                gs.info('CIs 1:' + linux.sys_id.toString());
            }
            gs.info('CIs 2:' + arr.toString());
            return "sys_idIN" + arr.toString();

        }

        if (selectedVal == "stWindows")

        {
            var win = new GlideRecord("cmdb_ci_win_server");
            win.addEncodedQuery('nameISNOTEMPTY^sys_domain=dff860dcdbc02700cb2d70c08c9619ac^sys_class_name=cmdb_ci_vmware_instance^ORsys_class_name=cmdb_ci_win_server^install_status=1^ORinstall_status=101');
            win.query();
            while (win.next()) {
                arr.push(win.sys_id.toString());
                gs.addInfoMessage('IM Here 2');
            }
            return "sys_idIN" + arr.toString();
        }
    },

    type: 'deInstallServerType'
};

  

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Brett21 You did a small mistake while declaring the array at line number 7. I have fixed the same in the following script.

 

var deInstallServerType = Class.create();
deInstallServerType.prototype = {

    serverType: function(selectedVal) {

        var selectedVal = current.variables.idServerType; //Radio Button Unix/Linux or Windows
        gs.info("ServerType " + current.variables.idServerType + " var " + selectedVal);
        var arr = []; //The array is initialised with square brackets.

        if (selectedVal == "stUnixLinux") {
            var linux = new GlideRecord("cmdb_ci_linux_server");
    linux.addEncodedQuery('nameISNOTEMPTY^sys_domain=dff860dcdbc02700cb2d70c08c9619ac^support_groupISNOTEMPTY^install_status=1^ORinstall_status=101');
            linux.query();
            while (linux.next()) {
                arr.push(linux.sys_id.toString());
                gs.info('CIs 1:' + linux.sys_id.toString());
            }
            gs.info('CIs 2:' + arr.toString());
            return "sys_idIN" + arr.toString();

        }

        if (selectedVal == "stWindows")

        {
            var win = new GlideRecord("cmdb_ci_win_server");
            win.addEncodedQuery('nameISNOTEMPTY^sys_domain=dff860dcdbc02700cb2d70c08c9619ac^sys_class_name=cmdb_ci_vmware_instance^ORsys_class_name=cmdb_ci_win_server^install_status=1^ORinstall_status=101');
            win.query();
            while (win.next()) {
                arr.push(win.sys_id.toString());
                gs.addInfoMessage('IM Here 2');
            }
            return "sys_idIN" + arr.toString();
        }
    },

    type: 'deInstallServerType'
};

Hope this fixes your issue.

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@Brett21 You did a small mistake while declaring the array at line number 7. I have fixed the same in the following script.

 

var deInstallServerType = Class.create();
deInstallServerType.prototype = {

    serverType: function(selectedVal) {

        var selectedVal = current.variables.idServerType; //Radio Button Unix/Linux or Windows
        gs.info("ServerType " + current.variables.idServerType + " var " + selectedVal);
        var arr = []; //The array is initialised with square brackets.

        if (selectedVal == "stUnixLinux") {
            var linux = new GlideRecord("cmdb_ci_linux_server");
    linux.addEncodedQuery('nameISNOTEMPTY^sys_domain=dff860dcdbc02700cb2d70c08c9619ac^support_groupISNOTEMPTY^install_status=1^ORinstall_status=101');
            linux.query();
            while (linux.next()) {
                arr.push(linux.sys_id.toString());
                gs.info('CIs 1:' + linux.sys_id.toString());
            }
            gs.info('CIs 2:' + arr.toString());
            return "sys_idIN" + arr.toString();

        }

        if (selectedVal == "stWindows")

        {
            var win = new GlideRecord("cmdb_ci_win_server");
            win.addEncodedQuery('nameISNOTEMPTY^sys_domain=dff860dcdbc02700cb2d70c08c9619ac^sys_class_name=cmdb_ci_vmware_instance^ORsys_class_name=cmdb_ci_win_server^install_status=1^ORinstall_status=101');
            win.query();
            while (win.next()) {
                arr.push(win.sys_id.toString());
                gs.addInfoMessage('IM Here 2');
            }
            return "sys_idIN" + arr.toString();
        }
    },

    type: 'deInstallServerType'
};

Hope this fixes your issue.

Ankur Bawiskar
Tera Patron
Tera Patron

@Brett21 

As mentioned by Sandeep your array declaration is the issue.

please fix that and share the updates.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Bhupender
Tera Expert

Hi @Brett21,

Your script is mostly correct, but there's a small mistake. Instead of declaring an array, you've accidentally declared a string:

```javascript
var arr = ' ';
```

Please replace this line with the following to correctly declare an array:

```javascript
var arr = [];
```

Strings do not support the `.push` method, which is why this change is necessary for your script to function properly.

Make this minor adjustment, and your script should work well!

Best regards,
Bhupender Bhadana

If this resolves your issue, please let me know, and I’d be happy to help!

Bhupender
Tera Expert

Hi @Brett21,

I noticed a small issue in your script. You accidentally declared a string instead of an array with this line:

```javascript
var arr = '';
```

To fix this, you should change it to:

```javascript
var arr = [];
```

The `.push` method doesn’t work with strings, so making this modification will allow your script to function properly.

Thank you,
Bhupender Bhadana

Please mark this as helpful if it resolves your issue.