- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
I have a attachment variable on catalog form , w hen selected triggers a on change client script and then calls a script include to delete existing attachment on data source and copies the one that is selected on the catalog form. Tested this by printing logs , only copy logic isn't working. Tried various things that were commented out in the script. Tested this logic without deleting the manually uploaded file and script works as expected.
It isnt dynamically copying the file , attachmentSysId holds the Sysid of attachment record that is attached to attachment variable
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();
}
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);
gsa.write(
'sys_data_source',
dataSourceSysId,
grAttachment.file_name,
grAttachment.content_type,
inputStream
);
var loader = new GlideImportSetLoader();
var grImportSet = loader.getImportSetGr(grDataSource);
loader.loadImportSetTable(grImportSet, grDataSource);
grImportSet.state = "loaded";
grImportSet.update();
//Trigger the transform
var importTransformer = new GlideImportSetTransformer();
importTransformer.setSyncImport(true); // Wait for the transform to finish before continuing
importTransformer.transformAllMaps(grImportSet);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Posted twice as I didn't realize the first one is pending approval. Here is the solution from the 1st post
https://www.servicenow.com/community/virtual-agent-forum/copy-attachment-from-attachment-type-variab...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
The entry in the attachment table after copying has zz_yy_sys_data_source in it, which is why it didn't show up on the data source.
If I remove update the table name in attachment table using script , it is showing up now .
Looking for better way to do this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Posted twice as I didn't realize the first one is pending approval. Here is the solution from the 1st post
https://www.servicenow.com/community/virtual-agent-forum/copy-attachment-from-attachment-type-variab...
