
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2015 08:06 AM
I'm trying to take an attachment (an image), which is added to a catalog item and via a workflow script add the image to an image (type) field on a custom table.
Here is my script...
var brandName = current.variables.brand_name.getDisplayValue();
var logo = '';
copyAttachment();
function addBrandRecord(){
var findBrand = new GlideRecord('u_brand');
findBrand.addQuery('name', brandName);
findBrand.query();
while (findBrand.next()) {
findBrand.street = current.variables.address;
findBrand.phone = current.variables.phone;
findBrand.website = current.variables.website;
findBrand.u_twitter = current.variables.twitter;
findBrand.u_facebook = current.variables.facebook;
findBrand.u_linkedin = current.variables.linkedin;
findBrand.u_other_links = current.variables.other_links;
findBrand.u_description = current.variables.description;
findBrand.u_current_offers = current.variables.current_offers;
findBrand.u_brand_state = "Awaiting Approval";
findBrand.u_logo_icon = logo;
findBrand.update();
}
}
function copyAttachment() {
var grLogo = new GlideRecord('sys_attachment');
grLogo.addQuery('table_sys_id', current.sys_id);
grLogo.query();
if (grLogo.next()) {
var sa = new GlideSysAttachment();
sa.getContent(grLogo);
sa.getContentBase64(grLogo);
var grImg = new GlideRecord('db_image');
grImg.initialize();
grImg.category = "general";
grImg.name = String(grLogo.file_name);
grImg.image = sa.getContent(grLogo);
grImg.insert();
logo = grImg.image.sys_id + ".iix";
gs.log('Attachment image = ' + grImg.image, 'DC');
gs.log('att href = ' + grLogo.file_name.href, 'DC');
gs.log('Attachment = ' + logo, 'DC');
addBrandRecord();
}
}
I can get the attachment fine but the getContent (& getContent64) comes back as undefined in my logs.
Has anyone done this before?
Can I transfer an attachment to an image field?
Is there another way to do this?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2015 09:07 AM
This is my solution if anyone is interested...
copyAttachment();
function copyAttachment() {
var findBrand = new GlideRecord('u_brand');
findBrand.addQuery('name', brandName);
findBrand.query();
while (findBrand.next()) {
var grLogo = new GlideRecord('sys_attachment');
grLogo.addQuery('table_sys_id', current.sys_id);
grLogo.query();
if (grLogo.next()) {
createImage(String(grLogo.sys_id), 'u_logo_icon', 'u_brand', String(findBrand.sys_id));
}
}
}
/*
attachmentID: sys_id of attachment containing the picture to be copied
fieldName: name of the image field to be used
tableName: name of the table containing the image field
tableID: sys_id of the record being copied to
example call:
createImage('551c4cf16f102100758ecb512e3ee47b', 'mobile_picture', 'sc_cat_item_producer', '29a39e830a0a0b27007d1e200ad52253');
*/
function createImage(attachmentID, fieldName, tableName, tableID) {var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.get(attachmentID);
var fields = attachmentGR.getFields();
var imageGR = new GlideRecord('sys_attachment');
imageGR.initialize();
imageGR.compressed = attachmentGR.compressed;
imageGR.content_type = attachmentGR.content_type;
imageGR.size_bites = attachmentGR.size_bites;
imageGR.size_compressed = attachmentGR.size_compressed;
imageGR.file_name = fieldName;
imageGR.table_name = 'ZZ_YY' + tableName;
imageGR.table_sys_id = tableID;
var imageID = imageGR.insert();
copyAttachmentContent(attachmentID, imageID);
}
/*
oldID: sys_id of existing attachment
newID: sys_id of newly created attachment
*/
function copyAttachmentContent(oldID, newID) {var oldGR = new GlideRecord('sys_attachment_doc');
oldGR.addQuery('sys_attachment', oldID);
oldGR.query();
while (oldGR.next()) {
var newGR = new GlideRecord('sys_attachment_doc');
newGR.initialize();
newGR.data = oldGR.data;
newGR.length = oldGR.length;
newGR.position = oldGR.position;
newGR.sys_attachment = newID;
newGR.insert();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2015 09:07 AM
This is my solution if anyone is interested...
copyAttachment();
function copyAttachment() {
var findBrand = new GlideRecord('u_brand');
findBrand.addQuery('name', brandName);
findBrand.query();
while (findBrand.next()) {
var grLogo = new GlideRecord('sys_attachment');
grLogo.addQuery('table_sys_id', current.sys_id);
grLogo.query();
if (grLogo.next()) {
createImage(String(grLogo.sys_id), 'u_logo_icon', 'u_brand', String(findBrand.sys_id));
}
}
}
/*
attachmentID: sys_id of attachment containing the picture to be copied
fieldName: name of the image field to be used
tableName: name of the table containing the image field
tableID: sys_id of the record being copied to
example call:
createImage('551c4cf16f102100758ecb512e3ee47b', 'mobile_picture', 'sc_cat_item_producer', '29a39e830a0a0b27007d1e200ad52253');
*/
function createImage(attachmentID, fieldName, tableName, tableID) {var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.get(attachmentID);
var fields = attachmentGR.getFields();
var imageGR = new GlideRecord('sys_attachment');
imageGR.initialize();
imageGR.compressed = attachmentGR.compressed;
imageGR.content_type = attachmentGR.content_type;
imageGR.size_bites = attachmentGR.size_bites;
imageGR.size_compressed = attachmentGR.size_compressed;
imageGR.file_name = fieldName;
imageGR.table_name = 'ZZ_YY' + tableName;
imageGR.table_sys_id = tableID;
var imageID = imageGR.insert();
copyAttachmentContent(attachmentID, imageID);
}
/*
oldID: sys_id of existing attachment
newID: sys_id of newly created attachment
*/
function copyAttachmentContent(oldID, newID) {var oldGR = new GlideRecord('sys_attachment_doc');
oldGR.addQuery('sys_attachment', oldID);
oldGR.query();
while (oldGR.next()) {
var newGR = new GlideRecord('sys_attachment_doc');
newGR.initialize();
newGR.data = oldGR.data;
newGR.length = oldGR.length;
newGR.position = oldGR.position;
newGR.sys_attachment = newID;
newGR.insert();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2016 07:33 AM
Hi Daryll,
the script work perfect.
the only issue i see here is that the Image being broken.
i can just see the below:
i have used the sysid directly: createImage('accc839f4f862200172734a18110c757'(sys id of the Image(db_image) records), 'u_image_survey', 'incident', '0e0a439f4f862200172734a18110c7a1');
any update from you hugely appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2015 12:53 AM
hello
your script dont create an image but an attachment..
if i create a image field on a table, nothing is diplayed.
stephane

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2015 01:11 AM
Images are handled as attachments in ServiceNow.
When you use line 12...
createImage(String(grLogo.sys_id), 'u_logo_icon', 'u_brand', String(findBrand.sys_id));
This sets which field you are placing the image in so breaking this down...
createImage(<Source Sys_id>, <target field>, <target table>, <target record sys_id>);
I hope that helps, I have it working (just be aware of the format of the images and that it is an image type that ServiceNow can handle)