Copy Attachment from attachment type variable to Data Source using Client callable script include

Revanth111
Mega Contributor

I have a attachment variable on the catalog form and I created a on change catalog client script that calls Script Include to Copy the attachment from  the variable to data source in script include.

 

 

Tried multiple script and it doesn't do anything

 

var attachmentSysId = this.getParameter('sysparm_attachment_id');
        var dataSourceSysId = 'a73774132f4c8710b456202bcfa4e3cc';
        var gsa = new GlideSysAttachment();
        var grAtt = new GlideRecord('sys_attachment');
        grAtt.addQuery('table_sys_id', dataSourceSysId);
        grAtt.query();
        while (grAtt.next()) {
            grAtt.deleteRecord();
        }
        //Copy New attachment to Data source and transfor it
        var grAttachment = new GlideRecord('sys_attachment');
        if (grAttachment.get(attachmentSysId)) {
            var grDataSource = new GlideRecord("sys_data_source");
            if (grDataSource.get(dataSourceSysId)) {
                //Copy Attachment to Data Source
                // gsa.copy(grAttachment.table_name, grAttachment.table_sys_id, 'sys_data_source', dataSourceSysId);
              //gsa.copy('ZZ_YYsc_cart_item', attachmentSysId, 'sys_data_source', dataSourceSysId);
//gsa.copy('sys_attachment',attachmentSysId,  'sys_data_source',dataSourceSysId);
1 ACCEPTED SOLUTION

shivakumarpatil
Tera Expert

Hi @Revanth111 ,

GlideSysAttachment.copy() may not work when sys_attachment is the source.
Please try using GlideSysAttachment.writeContentStream() as shown below—it should correctly add the attachment to your Data Source.

var attachmentSysId = this.getParameter('sysparm_attachment_id');
        var dataSourceSysId = 'a73774132f4c8710b456202bcfa4e3cc';
        var gsa = new GlideSysAttachment();
        var grAtt = new GlideRecord('sys_attachment');
        grAtt.addQuery('table_sys_id', dataSourceSysId);
        grAtt.query();
        while (grAtt.next()) {
            grAtt.deleteRecord();
        }
        //Copy New attachment to Data source and transfor it
        var grAttachment = new GlideRecord('sys_attachment');
        if (grAttachment.get(attachmentSysId)) {
            var grDataSource = new GlideRecord("sys_data_source");
            if (grDataSource.get(dataSourceSysId)) {
                
    var fileName = grAttachment.getValue('file_name');
                var contentType = grAttachment.getValue('content_type');
                var sourceAttachmentSysId = grAttachment.getValue('sys_id');

   gsa.writeContentStream(
                    grDataSource,
                    fileName,
                    contentType,
                    gsa.getContentStream(sourceAttachmentSysId));

            }
        }
 

View solution in original post

4 REPLIES 4

J Siva
Kilo Patron

Hi @Revanth111 
We have OOB "Copy Attachment" action in the flow designer.
You can use that to copy your attachment form the RITM variable to the Datasource.

JSiva_0-1776145724658.png

 

Note: 

Before copying the attachment, it is a best practice to delete the existing attachments from the data source record.


Regards,
Siva

shivakumarpatil
Tera Expert

Hi @Revanth111 ,

GlideSysAttachment.copy() may not work when sys_attachment is the source.
Please try using GlideSysAttachment.writeContentStream() as shown below—it should correctly add the attachment to your Data Source.

var attachmentSysId = this.getParameter('sysparm_attachment_id');
        var dataSourceSysId = 'a73774132f4c8710b456202bcfa4e3cc';
        var gsa = new GlideSysAttachment();
        var grAtt = new GlideRecord('sys_attachment');
        grAtt.addQuery('table_sys_id', dataSourceSysId);
        grAtt.query();
        while (grAtt.next()) {
            grAtt.deleteRecord();
        }
        //Copy New attachment to Data source and transfor it
        var grAttachment = new GlideRecord('sys_attachment');
        if (grAttachment.get(attachmentSysId)) {
            var grDataSource = new GlideRecord("sys_data_source");
            if (grDataSource.get(dataSourceSysId)) {
                
    var fileName = grAttachment.getValue('file_name');
                var contentType = grAttachment.getValue('content_type');
                var sourceAttachmentSysId = grAttachment.getValue('sys_id');

   gsa.writeContentStream(
                    grDataSource,
                    fileName,
                    contentType,
                    gsa.getContentStream(sourceAttachmentSysId));

            }
        }
 

This fix also helped to solve a different issue with the old attachment which was deleted in attachment table for table name = sys_data_source, however is still on zz_yy_sc_cart_item and I couldn't find a way to query to delete it is getting copied over to Data Source  in place of new attachment.

After this fix,  even the old zz_yy_Sc_cart_item still exists in attachments table  , but is not getting copied over to data source.

Thanks

Ankur Bawiskar
Tera Patron

@Revanth111 

what's your actual business requirement?

share that, may be we might share better approach to you

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader