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

I changed the old ci name.  Here's the new script:

var oldAssetID = 'BUILD01';
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());
        }
}
 
Here's the result:
KeithM1_0-1712001064100.png

Here's the attributes of the old PC that we are copying:

KeithM1_1-1712001133970.png

 

add the below line after the first if condition and check if same record is coming back via glide query

 

gs.print('Old AssetID: ' + asset.name);
 
tested on my PDI, the glide query result is not the same what we are passing the name value.
 
AshishKMishra_0-1712020926647.png

 


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

I added it in 2 places - after the first if/next statement and then at the end.  Here's the results:

KeithM1_0-1712068376171.png

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.

yes, so find out some other parameter ( either unique or along with name ).


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