copy ci attributes from one CI to another

KeithM1
Tera Expert

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.

13 REPLIES 13

some good result.

Can you check and confirm if there is one and only one record with same assetID name. 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

definitely some good results.  There is only one record in the cmdb_ci_computer table with the old computer name:

KeithM1_0-1711765262711.png

and only 1 record with the new computer name:

KeithM1_1-1711765340389.png

I appreciate all your help!

are you trying with my updated script or your one.

Use mine once .. ( if not used yet ) 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

I'm using this script:

var oldAssetID = '*DAVID-IBM';
var newAssetID = 'DP00315';

var asset = new GlideRecord('cmdb_ci_computer');
    asset.addActiveQuery('name', oldAssetID);
    asset.query();

    gs.print('Old AssetID: ' + oldAssetID);
    gs.print('New AssetID: ' + newAssetID);


if (asset.next()) {

    var newAsset = new GlideRecord('cmdb_ci_computer');
        newAsset.addActiveQuery('name', newAssetID);
        newAsset.query();
        if(newAsset.next()){
            newAsset.assigned_to = asset.assigned_to;
            newAsset.cost_center = asset.cost_center;
            newAsset.department = asset.department;
            newAsset.update(); // update the newAsset Record
           
            // print the new updated values after the update()
            gs.print('New Assigned To: ' + newAsset.assigned_to.getDisplayValue());
            gs.print('New Cost Center: ' + newAsset.cost_center);
            gs.print('New Department: ' + newAsset.department.getDisplayValue());
        }
}
 
This is the original one.  Only change was the table name (changed from cmdb_ci to cmdb_ci_computer) and the actual asset names.

var oldAssetID = '*DAVID-IBM';   // there is a * before D


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution