The CreatorCon Call for Content is officially open! Get started here.

Copy field values between records on the same table

David Casper
Tera Guru

To start out, I've created a custom field, u_alias_for, on the cmdb_ci table, that is a reference to itself (cmdb_ci). The purpose of this is to handle the multiple acronyms and application/service name changes in the medical industry. 

Whenever someone chooses a CI that is an alias, on any form, it automatically updates to the actual CI via a client script.

What I would like to do is make sure the support_group field is the same between the alias record and the actual CI record which the alias points to. Whenever the support_group field is updated on the "parent" CI record I need that value to copy over to the alias record, IF there is an alias for the CI. 

Trying to come up with a script that does this and also a way that is most efficient. 

Thanks!

1 ACCEPTED SOLUTION

You did mark it correct, I just didn't see it in time!

You can do a background script or a fix script. I prefer fix scripts because it saves the code so you can refer back to it in the future if needed.

execute();

function execute() {

	var ci = new GlideRecord( 'cmdb_ci' );
	ci.addNotNullQuery( 'u_alias_for' ); // only get CIs that are an alias to another
	ci.query();

	while ( ci.next() ) {
		ci.support_group = ci.u_alias_for.support_group;
		ci.autoSysFields( false ); // on clean up scripts I usually don't like to update the 'updated' fields on the record... 
		ci.update();
	}

}

Give this a shot and let me know how it goes. 

 

View solution in original post

8 REPLIES 8

Glad it helped! If this was the solution to your problem don't forget to mark it as correct 🙂 

Believe I marked it. If not let me know. 

 

One last question. I would like to run a background script to update the existing CI's which are an alias so that I don't have to manually update each one. Would you be able to assist? 🙂

You did mark it correct, I just didn't see it in time!

You can do a background script or a fix script. I prefer fix scripts because it saves the code so you can refer back to it in the future if needed.

execute();

function execute() {

	var ci = new GlideRecord( 'cmdb_ci' );
	ci.addNotNullQuery( 'u_alias_for' ); // only get CIs that are an alias to another
	ci.query();

	while ( ci.next() ) {
		ci.support_group = ci.u_alias_for.support_group;
		ci.autoSysFields( false ); // on clean up scripts I usually don't like to update the 'updated' fields on the record... 
		ci.update();
	}

}

Give this a shot and let me know how it goes. 

 

Zach:

That worked as well. Thanks again for the prompt help. Very very useful!

Was wondering if you would have a moment to look at another article I posted yesterday that I'm stuck on. It's probably one of those things that you end up looking at it too much yourself and then when someone takes a peak they see the issue right away, but I had to take a break. 

https://community.servicenow.com/community?id=community_question&sys_id=41dc70d5dbeca7008e7c2926ca961956

Thanks,

David