copy a catalog item attachment (an image) to a records image field

Daryll Conway
Giga Guru

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?

1 ACCEPTED SOLUTION

Daryll Conway
Giga Guru

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


  }


}


View solution in original post

20 REPLIES 20

Hello


My objectives is to diplay this image in a UI MACRO/Formatter


i want to display preview of all attachments stored in a ticket



if i stored the attachement in "db_image" i can i think.


have you an idea?




Stéphane LEMOINE


Consultant Expert ITSEOffer Unit IT Service Excellence


33 6 61 97 68 0 <336645549+04>9stephane.lemoine@devoteam.com


<florent.mauze@devoteam.com>[image: pattern devoteam]


<https://www.linkedin.com/company/devoteam>


<https://plus.google.com/+Devoteam-group>


<https://twitter.com/devoteam>


<http://www.devoteam.com/>[image: Innovative technology


consulting for business]



On 23 December 2015 at 10:12, dconway <community-no-reply@servicenow.com>


You are probably best to store the image in a table first then reference that in your UI Macro.


You can use <g:evaluate> to grab the image via a GlideRecord once you have it on a table.


You'll need to use the displayValue of the image to pull through the correct data.


i am not very aware of coding


have you an example of code ?


steph




Stéphane LEMOINE


Consultant Expert ITSEOffer Unit IT Service Excellence


33 6 61 97 68 0 <336645549+04>9stephane.lemoine@devoteam.com


<florent.mauze@devoteam.com>[image: pattern devoteam]


<https://www.linkedin.com/company/devoteam>


<https://plus.google.com/+Devoteam-group>


<https://twitter.com/devoteam>


<http://www.devoteam.com/>[image: Innovative technology


consulting for business]



On 23 December 2015 at 10:21, dconway <community-no-reply@servicenow.com>


For the UI Macro...



<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


<g:evaluate var="image" jelly="true" object="true">


// Gets a glide record from the table containing the image


  var image= new GlideRecord("<table with image>");


  image.addQuery("<Find your record with a query>");


  image.query();


  image;


</g:evaluate>



<j:if test="${image.next()}">


// Using the Jelly variable we can get the field (in this case u_logo_icon) and get the display value


  <img src="${image.u_logo_icon.getDisplayValue()}" />


</j:if>


// Gets a glide record from the table containing the image


var image= new GlideRecord("");




do i have to interrogate "sys_attachment"?





Stéphane LEMOINE


Consultant Expert ITSEOffer Unit IT Service Excellence


33 6 61 97 68 0 <336645549+04>9stephane.lemoine@devoteam.com


<florent.mauze@devoteam.com>[image: pattern devoteam]


<https://www.linkedin.com/company/devoteam>


<https://plus.google.com/+Devoteam-group>


<https://twitter.com/devoteam>


<http://www.devoteam.com/>[image: Innovative technology


consulting for business]



On 23 December 2015 at 10:50, dconway <community-no-reply@servicenow.com>