- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @Revanth111 ,
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));
}
}- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
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.
Note:
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @Revanth111 ,
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));
}
}- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
what's your actual business requirement?
share that, may be we might share better approach to you
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
