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

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

@J Siva : Thanks for the solution, it worked for me. Can you explain me what was the issue in my script and why it didn't work?.

Thanks for accepting my response as a solution.

Now coming to logic, you have used same gr object for multiple updates. Actually that doesn't work.

Since you are trying to update the same record multiple times, i just created one reusable function. So it'll be called everytime when you are trying to update. Each time new ci record object will be initiated.

 

Regards,

Siva

SrikrishnaB5625
Tera Expert

Hi @Akshaykhare 

Try running this script—it might work perfectly.

// 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()) {

     var sysId = gr.sys_id;

    // 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();

            gr = new GlideRecord('cmdb_ci_computer');

              if (gr.get(sysId)) {

                // Restore the original name

                gr.name = originalName;

                gr.update();

                gs.info("Restored name to original value: " + originalName);

            }

        }

    } else {

        // If asset record is not found, log an error

        gs.info("No asset record found for CI " + gr.name);

    }

}

 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway.

 

Regards,

Srikrishna