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
04-01-2024 12:52 PM
I changed the old ci name. Here's the new script:
Here's the attributes of the old PC that we are copying:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2024 06:20 PM - edited 04-01-2024 06:22 PM
add the below line after the first if condition and check if same record is coming back via glide query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 07:35 AM
I added it in 2 places - after the first if/next statement and then at the end. Here's the results:
Looks like I'm getting the same results as you are. So, for whatever reason, the old Asset value is not making it inside the first if/next loop.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 08:26 AM
yes, so find out some other parameter ( either unique or along with name ).