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

do you think is ti possible to preview other document like .word or .ppt ?





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:07, Stephane Lemoine <


I would imagine this is possible although I've never tried it.


One for another thread maybe.


I know you can copy the attachment to be an attachment on another table, which you could then preview with the built in view option on the attachments.



Using this ...



GlideSysAttachment.copy('sourcetable', 'sys_id', 'destinationtable', 'sys_id');


kazidon
Giga Contributor

I know this thread is super old, but I'm hoping someone will reply! I am using the above script and trying to get it to update the photo field in sys_user from a catalog client script. This is the script I am using and was wondering if anyone could help me out.

copyAttachment();



function copyAttachment() {


     var grLogo = new GlideRecord('sys_attachment');


     grLogo.addQuery('table_sys_id', current.sys_id);


     grLogo.query();


     if (grLogo.next()) {


     createImage(String(grLogo.sys_id), 'photo', 'sys_user', String('d9bf509e13361f80d9ff30ded144b093'));


   }


}



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


   }


}

samadam
Kilo Sage
kazidon
 
Were you able to figure this out?