No able to debug the script

Akshaykhare
Tera Contributor
I am trying to temporarily update the name field in cmdb_ci_computer table to "Test" and reupdate with the original value. But here after updating the field to "Test" it is not getting reupdated with original value. Please help me in debug


// Create a new GlideRecord object for the cmdb_ci_computer table
var gr = new GlideRecord('cmdb_ci_computer');
 
// Apply the encoded query to filter records by 'name' as 'ETADD1BL0510'
gr.addEncodedQuery("nameINETADD1BL0510");
gr.query();
 
// Loop through each record that matches the query
while (gr.next()) {
    // Get the sys_id of the 'asset' field (which is a reference to cmdb_ci_asset table)
    var assetSysId = gr.asset;  // The value is a sys_id of the related cmdb_ci_asset record
 
    // If assetSysId is empty or null, skip processing
    if (!assetSysId) {
        continue;
    }
 
    // Query the related cmdb_ci_asset record using the sys_id
    var assetGR = new GlideRecord('alm_hardware');
    if (assetGR.get(assetSysId)) {
        // Retrieve the 'status' field from the asset record (adjust if needed)
        var assetStatus = assetGR.asset_tag;  // Assuming 'status' is the field you want to check
 
        // Check if the 'status' field is 'No Asset Tag', 'No Asset Information', or empty
        if (assetStatus == 'No Asset Tag' || assetStatus == 'No Asset Information' || assetStatus =='' || assetStatus == 'NO ASSET TAG' || assetStatus == 'NO ASSET INFORMATION') {
            // Store the original 'name' value in a temp variable
            var temp = gr.name;
 
            // Update the 'name' field to 'Test'
            gr.name = 'Test';
 
            // Save the changes
            gr.update();
 
            // Restore the original name from the temp variable
            gr.name = temp;
           
            // Save the changes again
            gr.update();
        }
    } else {
        // If asset record is not found, log an error
        gs.info("No asset record found for CI " + gr.name);
    }
}
1 ACCEPTED SOLUTION

J Siva
Tera Sage

Hi @Akshaykhare 
Try below code. It'll work.

// Create a new GlideRecord object for the cmdb_ci_computer table
var temp;
var rec = new GlideRecord('cmdb_ci_computer');
rec.addEncodedQuery("nameINETADD1BL0510");
rec.query();
while (rec.next()) {
    var assetSysId = rec.asset;
    if (!assetSysId) {
        continue;
    }
    var assetGR = new GlideRecord('alm_hardware');
    if (assetGR.get(assetSysId)) {
        var assetStatus = assetGR.asset_tag;
        if (assetStatus == 'No Asset Tag' || assetStatus == 'No Asset Information' || assetStatus == '' || assetStatus == 'NO ASSET TAG' || assetStatus == 'NO ASSET INFORMATION') {
            temp = rec.name;

            // Update the 'name' field to 'Test'
            this.updateCi(rec.sys_id, "Test");

            // Restore the original name from the temp variable
            this.updateCi(rec.sys_id, temp);
        }
    } else {
        gs.info("No asset record found for CI " + rec.name);
    }
}

function updateCi(ci_id, temp_name) {
    var temp_ci = new GlideRecord('cmdb_ci_computer');
    temp_ci.get(ci_id);
    temp_ci.name = temp_name;
    temp_ci.update();
}

Hope this helps.
Regards,
Siva

View solution in original post

5 REPLIES 5

Robbie
Kilo Patron
Kilo Patron

Hi @Akshaykhare,

 

There's an execution order issue here. You're not far off. Use the below tried and tested script via my PDI to complete this.

 

// Create a new GlideRecord object for the cmdb_ci_computer table
var grCiComp = new GlideRecord('cmdb_ci_computer');

// Apply the encoded query to filter records by 'name' as 'ETADD1BL0510'
grCiComp.addEncodedQuery("nameINETADD1BL0510");
grCiComp.query();

// Loop through each record that matches the query
while (grCiComp.next()) {
    gs.info('CI renaming script. CI found: ' + grCiComp.name);
    // Get the sys_id of the 'asset' field (which is a reference to cmdb_ci_asset table)
    var assetSysId = grCiComp.asset; // The value is a sys_id of the related cmdb_ci_asset record
    gs.info('CI renaming script. Asset found via CI record: ' + grCiComp.asset);
    // If assetSysId is empty or null, skip processing
    if (!assetSysId) {
        continue;
    }

    // Query the related cmdb_ci_asset record using the sys_id
    var assetGR = new GlideRecord('alm_hardware');
    if (assetGR.get(assetSysId)) {
        gs.info('CI renaming script. Asset found via Asset lookup: ' + assetGR.asset_tag);
        // Retrieve the 'status' field from the asset record (adjust if needed)
        var assetStatus = assetGR.asset_tag; // Assuming 'status' is the field you want to check

        // Store the original 'name' value in a temp variable
        var temp = grCiComp.name;
        gs.info('CI renaming script. temp value before update: ' + temp);

        // Check if the 'status' field is 'No Asset Tag', 'No Asset Information', or empty
        if (assetStatus == 'No Asset Tag' || assetStatus == 'No Asset Information' || assetStatus == '' || assetStatus == 'NO ASSET TAG' || assetStatus == 'NO ASSET INFORMATION') {
            // Update the 'name' field to 'Test'
            updateCiName(grCiComp.sys_id, 'Test');
            
            // Restore the original name from the temp variable
            updateCiName(grCiComp.sys_id, temp);
        }
    } else {
        // If asset record is not found, log an error
        gs.info("No asset record found for CI " + grCiComp.name);
    }
}

function updateCiName(ci_sys_id, name) {
	gs.info('CI renaming script. inside updateCiName function');
    var ciNameGR = new GlideRecord('cmdb_ci_computer');
    if (ciNameGR.get(ci_sys_id)) {
        ciNameGR.name = name;
        ciNameGR.update();
		gs.info('CI renaming script. Ci value name after  update: ' + ciNameGR.name);
    }
}

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.




Thanks, Robbie