logo

NJP

Carbon Paper 2.0

sys.properties · Nov 08, 2013 · article

It is no secret for any ServiceNow administrator that there are lots of situations where you need to copy information from one record to another or even create a new record as a copy of another record. The easiest way to do it programmatically is to execute a script similar to this:

var usr_id = 'ddc2b4edfc81b800f0d8e21d514cc2a4';
var gr = new GlideRecord('sys_user');
gr.get(usr_id);
gr.insert();

The script above finds a user record by sys_id and uses insert() method to create an exact copy. Of course, two identical user records is probably not something you would really like to have in your application. In a real-life scenario, you will most likely want to change at least the name of the user. Here is a way to do it:

var usr_id = 'ddc2b4edfc81b800f0d8e21d514cc2a4';
var gr = new GlideRecord('sys_user');
gr.get(usr_id);
gr.first_name = 'John';
gr.last_name = 'Smith';
gr.insert();

There are, however, at least three things that will not be copied by a script like this.

1. Journal fields

The contents of all journal fields in the system are actually stored in Journal Entry [sys_journal_field] table. You will need to query that table to copy Work Notes or Additional Comments.

2. Attachments

When you attach a file to any record in ServiceNow, it is saved in Attachment [sys_attachment] table. To copy attachments, use the following piece of code:

if (typeof GlideSysAttachment != 'undefined') {
// for Calgary and newer versions
GlideSysAttachment.copy('source_table', 'sys_id', 'target_table', 'sys_id');
} else {
// for Berlin and older versions
Packages.com.glide.ui.SysAttachment.copy('source_table', 'sys_id', 'target_table', 'sys_id');
}

3. Image and video fields

There are two types of image fields in ServiceNow: image (e.g. Icon field in Module records) and user_image (e.g. Photo field in User records). The former contains a link to an image. The latter contains… nothing. You can easily verify that by exporting any user record that has a photo to XML. In fact, images uploaded into user_image fields are physically stored in Attachment [sys_attachment] table, the only difference from usual attachments being ZZ_YY prefix in the Table Name attribute. The same is valid for video type fields. As a result, anything you upload to Images [db_image] or Video [db_video] tables ends up in Attachments [sys_attachment] table.

View original source

https://sys.properties/2013/11/08/103/