Script is Returning a number of Undefined results

Derek28
Tera Contributor

I have a script where we are checking the Computer table and seeing if we have associated records via MAC and if we do, we want to update some custom fields

In the example below, we are searching 5 computers, which should match with 2 records and 3 that have no linked table so should not update.

The 2 are working fine and updating correctly, the 3 are adding undefined. Is there a way I can script this better, so where no match is found there is no update, so no undefined added.

 

 

var Query = "mac_address=xxxxx1^ORmac_address=xxxxx2^ORmac_address=xxxxx3^ORmac_address=xxxxx4^ORmac_address=xxxxx5"
var gr = new GlideRecord('cmdb_ci_computer');
gr.addEncodedQuery(Query);
gr.query();
while (gr.next())

{
   
    end.addQuery('name', gr.mac_address);
    end.setLimit(1); //
    end.query();
    while (end.next())
     
    var fabric = end.fabric_interface;
    fabric = fabric.substring(0, fabric.indexOf('||S'));

    var inter = end.fabric_interface;
    inter = inter.substring(0, inter.indexOf(','));
    var s = end.fabric_interface;
    s = s.substring(s.indexOf("|S") + 1);
    s = s.substring(0, s.indexOf(","));
    // gs.print('ACI Switch Port: ' + s);

    var v = end.dn;
    v = v.substring(v.indexOf("epg-") + 4);
    v = v.substring(0, v.indexOf("-EPG"));

    gr.setValue('u_aci_endpoint', end.sys_id);
    gr.setValue('u_aci_switch_port', s);
    gr.setValue('u_vlan', v);
    gr.update();
}


Thanks in Advance

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Derek28 

try this -> ensure you give the correct table name for some_table

var Query = "mac_address=xxxxx1^ORmac_address=xxxxx2^ORmac_address=xxxxx3^ORmac_address=xxxxx4^ORmac_address=xxxxx5"; 
var gr = new GlideRecord('cmdb_ci_computer');
gr.addEncodedQuery(Query);
gr.query();

while (gr.next()) {
    var end = new GlideRecord('some_table'); // Replace 'some_table' with the actual table name
    end.addQuery('name', gr.mac_address);
    end.setLimit(1);
    end.query();

    if (end.next()) {
        var fabric = end.fabric_interface;
        if (fabric) {
            fabric = fabric.substring(0, fabric.indexOf('||S'));
        }

        var inter = end.fabric_interface;
        if (inter) {
            inter = inter.substring(0, inter.indexOf(','));
        }

        var s = end.fabric_interface;
        if (s) {
            s = s.substring(s.indexOf("|S") + 1);
            s = s.substring(0, s.indexOf(","));
        }

        var v = end.dn;
        if (v) {
            v = v.substring(v.indexOf("epg-") + 4);
            v = v.substring(0, v.indexOf("-EPG"));
        }

        if (end.sys_id) {
            gr.setValue('u_aci_endpoint', end.sys_id);
        }
        if (s) {
            gr.setValue('u_aci_switch_port', s);
        }
        if (v) {
            gr.setValue('u_vlan', v);
        }
        gr.update();
    }
}

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Derek28 

try this -> ensure you give the correct table name for some_table

var Query = "mac_address=xxxxx1^ORmac_address=xxxxx2^ORmac_address=xxxxx3^ORmac_address=xxxxx4^ORmac_address=xxxxx5"; 
var gr = new GlideRecord('cmdb_ci_computer');
gr.addEncodedQuery(Query);
gr.query();

while (gr.next()) {
    var end = new GlideRecord('some_table'); // Replace 'some_table' with the actual table name
    end.addQuery('name', gr.mac_address);
    end.setLimit(1);
    end.query();

    if (end.next()) {
        var fabric = end.fabric_interface;
        if (fabric) {
            fabric = fabric.substring(0, fabric.indexOf('||S'));
        }

        var inter = end.fabric_interface;
        if (inter) {
            inter = inter.substring(0, inter.indexOf(','));
        }

        var s = end.fabric_interface;
        if (s) {
            s = s.substring(s.indexOf("|S") + 1);
            s = s.substring(0, s.indexOf(","));
        }

        var v = end.dn;
        if (v) {
            v = v.substring(v.indexOf("epg-") + 4);
            v = v.substring(0, v.indexOf("-EPG"));
        }

        if (end.sys_id) {
            gr.setValue('u_aci_endpoint', end.sys_id);
        }
        if (s) {
            gr.setValue('u_aci_switch_port', s);
        }
        if (v) {
            gr.setValue('u_vlan', v);
        }
        gr.update();
    }
}

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

Derek28
Tera Contributor

@Ankur Bawiskar Thanks you are a superstar, that is working!!!