Clone CIs from one class to another one

Sandro Mattei
Mega Guru

Hi.

I'm trying to implement an independent population/update method to maintain a part of my company's CMDB not based in Discovery (but on Robust Transform maps and REST). I'll keep ip updated/maintained by Coalesce fields instead of IRE rules or whatever... and to maintanin it in an easy way, decide to maintain only one table.

I'm looking for clone items in cmdb_ci_linux_server to cmdb_ci_vm_instance (I already create custom fields to adjust the ones existing in linux one). The old ones will keep as Archive data, because of this I want to clone, not move.

Is there any way to clone CIs by script? or I need to create a new item and copy field by field...?

 

Thanks in advance.

BR.

2 ACCEPTED SOLUTIONS

Community Alums
Not applicable

Hi @Sandro Mattei ,

I tried you problem in my PDI and it works for me, please refer below steps 

Create onBefore BR and add table cmdb_ci_linux_server and add below code 

var gr = new GlideRecord('cmdb_ci_vm_instance');
	gr.initialize();
	gr.name = current.name;
// you can add as many fields here 
	gr.insert();

Image for reference 

SarthakKashya2_0-1713945610868.png

SarthakKashya2_1-1713945625330.png

 

Result image 

When I updated record in cmdb_ci_linux_server this table 

SarthakKashya2_2-1713945662185.png

Clone record is created in cmdb_ci_vm_instance table

SarthakKashya2_3-1713945714096.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

View solution in original post

AnthonyJones
Giga Contributor

@Sandro Mattei wrote:

Hi.

I'm trying to implement an independent population/update method to maintain a part of my company's CMDB not based in Discovery (but on Robust Transform maps and REST). I'll keep ip updated/maintained by Coalesce fields instead of IRE rules or whatever... and to maintanin it in an easy way, decide to maintain only one table.

I'm looking for clone items in cmdb_ci_linux_server to cmdb_ci_vm_instance (I already create custom fields to adjust the ones existing in linux one). The old ones will keep as Archive data, because of this I want to clone, not move.

Is there any way to clone CIs by script? or I need to create a new item and copy field by field...?

 

Thanks in advance.

BR.


Cloning Configuration Items (CIs) in ServiceNow can be achieved through scripting. While there isn’t a built-in “clone” function, you can create a script that duplicates the record by copying its field values from one table to another. Here’s a conceptual example of how you might approach this with a script:

// This is a conceptual example and may require adjustments to fit your specific instance and needs.

// Define the source and target tables
var sourceTable = 'cmdb_ci_linux_server';
var targetTable = 'cmdb_ci_vm_instance';

// Query the source table for the records you want to clone
var grSource = new GlideRecord(sourceTable);
grSource.query(); // Add any conditions to the query if needed
while (grSource.next()) {
    // Create a new record in the target table
    var grTarget = new GlideRecord(targetTable);
    grTarget.initialize();
    
    // Copy fields from the source record to the target record
    grTarget.name = grSource.name;
    grTarget.short_description = grSource.short_description;
    // ... copy other fields as needed
    
    // Save the new target record
    grTarget.insert();
}

This script initializes a new record in the target table for each record in the source table and copies the relevant fields. You would need to adjust the field assignments to match your custom fields and any other specific logic required for your CMDB structure.

Remember to test your script thoroughly in a non-production environment to ensure it behaves as expected and doesn’t impact the integrity of your CMDB.



View solution in original post

2 REPLIES 2

Community Alums
Not applicable

Hi @Sandro Mattei ,

I tried you problem in my PDI and it works for me, please refer below steps 

Create onBefore BR and add table cmdb_ci_linux_server and add below code 

var gr = new GlideRecord('cmdb_ci_vm_instance');
	gr.initialize();
	gr.name = current.name;
// you can add as many fields here 
	gr.insert();

Image for reference 

SarthakKashya2_0-1713945610868.png

SarthakKashya2_1-1713945625330.png

 

Result image 

When I updated record in cmdb_ci_linux_server this table 

SarthakKashya2_2-1713945662185.png

Clone record is created in cmdb_ci_vm_instance table

SarthakKashya2_3-1713945714096.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

AnthonyJones
Giga Contributor

@Sandro Mattei wrote:

Hi.

I'm trying to implement an independent population/update method to maintain a part of my company's CMDB not based in Discovery (but on Robust Transform maps and REST). I'll keep ip updated/maintained by Coalesce fields instead of IRE rules or whatever... and to maintanin it in an easy way, decide to maintain only one table.

I'm looking for clone items in cmdb_ci_linux_server to cmdb_ci_vm_instance (I already create custom fields to adjust the ones existing in linux one). The old ones will keep as Archive data, because of this I want to clone, not move.

Is there any way to clone CIs by script? or I need to create a new item and copy field by field...?

 

Thanks in advance.

BR.


Cloning Configuration Items (CIs) in ServiceNow can be achieved through scripting. While there isn’t a built-in “clone” function, you can create a script that duplicates the record by copying its field values from one table to another. Here’s a conceptual example of how you might approach this with a script:

// This is a conceptual example and may require adjustments to fit your specific instance and needs.

// Define the source and target tables
var sourceTable = 'cmdb_ci_linux_server';
var targetTable = 'cmdb_ci_vm_instance';

// Query the source table for the records you want to clone
var grSource = new GlideRecord(sourceTable);
grSource.query(); // Add any conditions to the query if needed
while (grSource.next()) {
    // Create a new record in the target table
    var grTarget = new GlideRecord(targetTable);
    grTarget.initialize();
    
    // Copy fields from the source record to the target record
    grTarget.name = grSource.name;
    grTarget.short_description = grSource.short_description;
    // ... copy other fields as needed
    
    // Save the new target record
    grTarget.insert();
}

This script initializes a new record in the target table for each record in the source table and copies the relevant fields. You would need to adjust the field assignments to match your custom fields and any other specific logic required for your CMDB structure.

Remember to test your script thoroughly in a non-production environment to ensure it behaves as expected and doesn’t impact the integrity of your CMDB.