- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
