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

Like I said you are best to use the original method (marked as correct above) and place the attachment into an image field first before using the UI Macro.


The GlideRecord in the UI Macro will then reference the table you placed the image on


ok i test




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 11:06, dconway <community-no-reply@servicenow.com>


If you are having difficulties post you code up here and I'll take a look for you.


it is OK for image...


i have first to copy all attachments on ticket (for example) into a staging


table with a field image.


and secondly create a UI MACRO/formatter to display



but do you think it is possible with word document?






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 11:21, dconway <community-no-reply@servicenow.com>


Hi.. 

Have you got this requirement. Even I have similar requirement to display the image in UI macro /formatter.

Please suggest