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

AshishKM
Kilo Patron
Kilo Patron

Hi @KeithM1 , 

 

Please check and use the below code for this CI attribute update.

Let me know if any issue. Ensure there is only one CI with old & new name.

 

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);


if (asset.next()) {	

	var newAsset = new GlideRecord('cmdb_ci');
		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());
		}
}

 

-Thanks,

AshishKM


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

Ashish,

 

I ran the script in my PDI and while I didn't get the error, it didn't update the new CI with the old CI information.  I ran it as a background script just as a test.  Here's what the output was:

 

*** Script: Old AssetID: *DAVID-IBM
*** Script: New AssetID: DP00288
*** Script: New Assigned To:
*** Script: New Cost Center:
*** Script: New Department:

 

The script is the same, just changed the CI's.  Also, I made sure that the fields had values in them before I ran the script.  Making progress, but not quite there yet.

What is the class of that CI. 

Use the same class name with GlideRecord() instead of cmdb_ci.


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

Ashish - this has been super helpful and I'm a lot further than what I was.  I updated the script to look at the computer class (cmdb_ci_computer) instead of cmdb_ci.  I'm getting values with the print statements.  I'm running into 2 problems:  1) the information from the old PC that the script is getting doesn't match what's in the record of the old PC.  Here's the result from the script:

KeithM1_0-1711762622698.png

Here's the values in the record of the old PC:

KeithM1_1-1711762673685.png

 

The other problem is that the new pc record isn't getting updated with the values from the old pc.  Here's the values of the new pc after running the script:

KeithM1_2-1711762741754.png

 

The new PC should have the values from the old PC.  Again, I appreciate your help but not quite there yet.