Function and array help

Will25
Tera Contributor

Hi all, 

 

I am struggling with this code. I am trying to collect the sys_ids of locations in the shipsLoc array, and then in the function use this to look at the cmdb_ci_server name (we have a naming convention which means the location of the device will be in the name as a three digit identifier, in this case either 'arc' or 'azu' (server name example: 123-ARC-001)).

 

So I want to look for this 3 digit identifier in the CI name, compare this to the three digit shortened name in the location table, and populate the record based on the same 3 digit identifier. 

 

Please could someone kindly have a look at the code and tell me where I am going wrong, please? 

 

It currently does populate the location field, just not with the correct location.

var ciRec = new GlideRecord('cmdb_ci_server');

function findShipLocation() {

    var shipsLoc = [];
    var abc = new GlideRecord('cmn_location');
    abc.addEncodedQuery("cmn_location_type=Ship^u_shortened_nameISNOTEMPTY");
    abc.query();

    while (abc.next()) {
        shipsLoc.push({
            'name': abc.u_shortened_name + '',
            'sys_id': abc.sys_id + ''
        });
    }
    var myOutput = JSON.stringify(shipsLoc);
    // gs.info(myOutput);

    for (var i = 0; i < shipsLoc.length; i++) {
        if (shipsLoc[i].name == ciRec.name.toLowerCase().indexOf('arc') > -1) {
			ciRec.location = shipsLoc[i].sys_id;

        } else if (shipsLoc[i].name.indexOf('azu') > -1 == ciRec.name.toLowerCase().indexOf('azu') > -1) {
            ciRec.location = shipsLoc[i].sys_id;
        }
        ciRec.company = ciRec.location.company;
    }
}

ciRec.addEncodedQuery("operational_statusNOT IN2,6");
ciRec.query();

while (ciRec.next()) {
    ciRec.autoSysFields(false); //not update sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, sys_created_on
    ciRec.setWorkflow(false);

    if (!ciRec.assigned_to && ciRec.support_group) {
        //Update if record has no Assigned To information but does have Support Group information.
        ciRec.department = 'e87f6c331ba6b5508d18ec69b04bcb4b'; 
        ciRec.owned_by = ciRec.support_group.u_cmdb_owner; //Set owned by to support group cmdb owner
        findShipLocation();

    } else if (ciRec.assigned_to && ciRec.support_group) {
        //Update if record has Assigned To and Support Group information
        ciRec.department = ciRec.assigned_to.department; // Set CI Department to Assigned To Department
        ciRec.owned_by = ciRec.support_group.u_cmdb_owner; //set owned by to support group CMDB owner
        findShipLocation();

    } else if (ciRec.assigned_to && !ciRec.support_group) {
        //Update if record has Assigned To and no Support Group information
        ciRec.department = ciRec.assigned_to.department; // Set CI Department to Assigned To Department
        findShipLocation();

    } else {
        findShipLocation();
        ciRec.department = 'e87f6c331ba6b5508d18ec69b04bcb4b';
    }
    ciRec.update();
}
gs.info("Daily BR [server] Set assigned to attributes complete");

 

5 REPLIES 5

the data was showing, but this is helpful, thank you.