- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2024 12:40 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2024 01:02 AM
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
Result image
When I updated record in cmdb_ci_linux_server this table
Clone record is created in cmdb_ci_vm_instance table
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2024 03:01 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2024 01:02 AM
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
Result image
When I updated record in cmdb_ci_linux_server this table
Clone record is created in cmdb_ci_vm_instance table
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2024 03:01 AM
@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.