copy ci attributes from one CI to another
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 11:51 AM
I'm trying to copy certain attributes (i.e. assigned to, department, location) from one CI to another (trying to use this in a PC Refresh workflow). The problem I'm having is that when I try to update the record with the new information, I get a "primary key" error.
Here's my script:
var oldAssetID = '1705-7C18JH2';
var newAssetID = '2001-1SGPH13'
var asset = new GlideRecord('cmdb_ci');
asset.addActiveQuery('name', oldAssetID);
asset.query();
gs.print('Old AssetID: ' + oldAssetID);
gs.print('New AssetID: ' + newAssetID);
//while(asset.next()) {
if (asset.get(oldAssetID)){
var oldAssignedTo = asset.assigned_to; //get assigned to value of old device
gs.print('Old Assigned To: ' + asset.assigned_to);
var oldCostCenter = asset.cost_center; //get cost center value of old device
gs.print('Old Cost Center: ' + asset.cost_center);
var oldDept = asset.department; //get dept name of old asset
gs.print('Old Dept: ' + asset.department);
}
var newAsset = new GlideRecord('cmdb_ci');
newAsset.addActiveQuery('name', newAssetID);
newAsset.query();
//while(newAsset.next()) {
if (newAsset.get(newAssetID)){
newAsset.assigned_to = oldAssignedTo;
gs.print('New Assigned To: ' + newAsset.assigned_to);
newAsset.cost_center = oldCostCenter;
gs.print('New Cost Center: ' + newAsset.cost_center);
newAsset.department = oldDept;
gs.print('New Department: ' + newAsset.department);
newAsset.update();
}
Here's the error I'm getting:
*** Script: New Assigned To: 0e61011813340f00728879566144b0db
*** Script: New Cost Center: ea3db199dbc7034499d35434ce9619fc
*** Script: New Department: c17d7b28e438ce44165ea2fa95c10b6c
*** Script: Duplicate asset generation (Existing asset) prevented for CI 2001-1SGPH13
FAILED TRYING TO EXECUTE ON CONNECTION glide.8 (connpid=698838): INSERT INTO cmdb
Keep in mind that the CI already exists, I'm not trying to add a new CI. I'm just trying to update some of the attributes.
I'm open to any suggestions if there is a better way to do it. I appreciate any/all help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 07:16 PM
some good result.
Can you check and confirm if there is one and only one record with same assetID name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 07:22 PM
definitely some good results. There is only one record in the cmdb_ci_computer table with the old computer name:
and only 1 record with the new computer name:
I appreciate all your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 08:07 PM
are you trying with my updated script or your one.
Use mine once .. ( if not used yet )
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2024 08:03 AM
I'm using this script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2024 11:03 AM
var oldAssetID = '*DAVID-IBM'; // there is a * before D