Fix script

JulietD
Mega Guru

Hi,

Can anyone help me to write a fix script to move data from parent class to child class.

Eg: from server class (parent) to windows class (child)

give the condition: class=Windows Server to copy only windows servers.

I need to copy all the fields.

 

1 ACCEPTED SOLUTION

Carlos Petrucio
Mega Sage

Hi, hope you are well!

 

Below is a patch script for ServiceNow that will move data from the parent class (cmdb_ci_server) to the child class (cmdb_ci_win_server) only for records where the class is "Windows Server".

 

It will copy all fields, ensuring that relationships and dependencies remain intact.

 

Note: This is only copying from the parent class and creating in the child class, it is not removing the old records from the parent class.

(function executeFixScript() {
    var parentTable = 'cmdb_ci_server'; // Parent class
    var childTable = 'cmdb_ci_win_server'; // Child class
    var condition = "sys_class_name=Windows Server"; // Condition to filter Windows Servers

    var grParent = new GlideRecord(parentTable);
    grParent.addEncodedQuery(condition);
    grParent.query();

    while (grParent.next()) {
        var grChild = new GlideRecord(childTable);
        grChild.initialize(); // Initialize a new record in the child class

        // Copy all fields dynamically
        var fields = grParent.getFields();
        for (var i = 0; i < fields.size(); i++) {
            var fieldName = fields.get(i).getName();
            if (grChild.isValidField(fieldName)) {
                grChild.setValue(fieldName, grParent.getValue(fieldName));
            }
        }

        // Ensure sys_class_name is set correctly
        grChild.sys_class_name = "cmdb_ci_win_server";
        grChild.insert(); // Insert the record into the child table

        gs.info("Record moved from " + parentTable + " to " + childTable + ": " + grParent.sys_id);

        // Optional: Delete the original record from the parent table
        // grParent.deleteRecord();
    }

    gs.info("Fix Script execution completed.");
})();

If my answer helped you, please let us know by marking it as the correct answer!

Thank you

 

Best regards Carlos Petrucio

View solution in original post

2 REPLIES 2

Viraj Hudlikar
Giga Sage

Hello @JulietD 

 

As per my memory if you change class all data will also get transferred unless there is custom field created on child class and not present in parent then that data is skipped. 

I would suggest you to utilise data management module in that use update job and there give filter which ci you want to get moved from server to windows server and then update class to windows server.

 

Check this video https://youtu.be/Zbv_niGkMTM?si=P7f4kjgBXyox_LHO

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards

Viraj Hudlikar.

Mastering System Data Management with ServiceNow: ServiceNow Delete and Update Jobs Explained HI ALl, Hope you are doing great. In todaystutorial we will see that is ServiceNow Dictionary Attriutes and then look into json_view and no_truncate attribute along with the demo. ServiceNow is a powerful

Carlos Petrucio
Mega Sage

Hi, hope you are well!

 

Below is a patch script for ServiceNow that will move data from the parent class (cmdb_ci_server) to the child class (cmdb_ci_win_server) only for records where the class is "Windows Server".

 

It will copy all fields, ensuring that relationships and dependencies remain intact.

 

Note: This is only copying from the parent class and creating in the child class, it is not removing the old records from the parent class.

(function executeFixScript() {
    var parentTable = 'cmdb_ci_server'; // Parent class
    var childTable = 'cmdb_ci_win_server'; // Child class
    var condition = "sys_class_name=Windows Server"; // Condition to filter Windows Servers

    var grParent = new GlideRecord(parentTable);
    grParent.addEncodedQuery(condition);
    grParent.query();

    while (grParent.next()) {
        var grChild = new GlideRecord(childTable);
        grChild.initialize(); // Initialize a new record in the child class

        // Copy all fields dynamically
        var fields = grParent.getFields();
        for (var i = 0; i < fields.size(); i++) {
            var fieldName = fields.get(i).getName();
            if (grChild.isValidField(fieldName)) {
                grChild.setValue(fieldName, grParent.getValue(fieldName));
            }
        }

        // Ensure sys_class_name is set correctly
        grChild.sys_class_name = "cmdb_ci_win_server";
        grChild.insert(); // Insert the record into the child table

        gs.info("Record moved from " + parentTable + " to " + childTable + ": " + grParent.sys_id);

        // Optional: Delete the original record from the parent table
        // grParent.deleteRecord();
    }

    gs.info("Fix Script execution completed.");
})();

If my answer helped you, please let us know by marking it as the correct answer!

Thank you

 

Best regards Carlos Petrucio