Help on scheduled job to update server data

pawan2
Tera Expert

Hello Gents,

 

I need your knowledge on this.

We have application services in our platform. We need to bring couple of data that we have in the Application service into the related mapped servers. We basically need to run it weekly. I started with this script. But need your help to figure out the ideal way.

 

// To load all the application service data which we have the data
var rel = new GlideRecord('cmdb_rel_ci');
rel.addQuery('parent.sys_class_name=cmdb_ci_service_auto');
rel.addQuery('parent.u_temp_criticality!=^ORparent.u_business_value!=');
rel.addQuery('child.sys_class_name=cmdb_ci_win_server^ORchild.sys_class_name=cmdb_ci_unix_server');
rel.query();
//gs.info("Total records: "+rel.getRowCount());

//As per the above query, I got the list of records that we need to update via the scheduled job. Now I need to figure out how to update each service-related data in the mapped server.

Below is the fields that I need to bring from the app service into mapped server.

1.u_temp_criticality

2.u_business_value

3.u_reviewed_count

 

var server = new GlideRecord('cmdb_ci_server'); // query the server table to load the data that we need to update.
server.addQuery('sysclass_name=cmdb_ci_win_server^ORsys_class_name=cmdb_ci_unix_server');

..............

 

Appreciate your support.

Thanks,

P

 

2 ACCEPTED SOLUTIONS

Appanna M
Tera Guru

Hello @pawan2 ,

 

Try with the below once.

 

// Step 1: Load all the application service data
var rel = new GlideRecord('cmdb_rel_ci');
rel.addQuery('parent.sys_class_name', 'cmdb_ci_service_auto');
rel.addQuery('parent.u_temp_criticality', '!=', '');
rel.addQuery('parent.u_business_value', '!=', '');
rel.addQuery('child.sys_class_name', 'IN', 'cmdb_ci_win_server,cmdb_ci_unix_server');
rel.query();

// Step 2: Loop through each relationship and update the child server
while (rel.next()) {
    var parentService = rel.parent.getRefRecord(); // Get the parent application service
    var childServer = rel.child.getRefRecord(); // Get the child server

    // Only proceed if both parent service and child server records are valid
    if (parentService.isValidRecord() && childServer.isValidRecord()) {
        // Step 3: Update the server with data from the application service
        childServer.u_temp_criticality = parentService.u_temp_criticality;
        childServer.u_business_value = parentService.u_business_value;
        childServer.u_reviewed_count = parentService.u_reviewed_count;

        // Update the child server record
        childServer.update();
    }
}

// Log the completion of the script
gs.debug('Application service data has been updated for mapped servers.');

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

View solution in original post

Thanks a lot @Appanna M . You are a genius. This is working perfectly.

View solution in original post

4 REPLIES 4

Appanna M
Tera Guru

Hello @pawan2 ,

 

Try with the below once.

 

// Step 1: Load all the application service data
var rel = new GlideRecord('cmdb_rel_ci');
rel.addQuery('parent.sys_class_name', 'cmdb_ci_service_auto');
rel.addQuery('parent.u_temp_criticality', '!=', '');
rel.addQuery('parent.u_business_value', '!=', '');
rel.addQuery('child.sys_class_name', 'IN', 'cmdb_ci_win_server,cmdb_ci_unix_server');
rel.query();

// Step 2: Loop through each relationship and update the child server
while (rel.next()) {
    var parentService = rel.parent.getRefRecord(); // Get the parent application service
    var childServer = rel.child.getRefRecord(); // Get the child server

    // Only proceed if both parent service and child server records are valid
    if (parentService.isValidRecord() && childServer.isValidRecord()) {
        // Step 3: Update the server with data from the application service
        childServer.u_temp_criticality = parentService.u_temp_criticality;
        childServer.u_business_value = parentService.u_business_value;
        childServer.u_reviewed_count = parentService.u_reviewed_count;

        // Update the child server record
        childServer.update();
    }
}

// Log the completion of the script
gs.debug('Application service data has been updated for mapped servers.');

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

Thanks a lot for the prompt support Appanna. Let me give it a try quickly. Much appreciated 🙂

Thanks a lot @Appanna M . You are a genius. This is working perfectly.

Hello @pawan2 ,

 

Thanks for your kind words. 

 

Regards,