Pre submission: Copy Attachment from attachment variable on catalog to data Source

Revanth111
Kilo Explorer

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);
            }
        }

 

0 REPLIES 0