- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2022 06:48 AM
Hello ,
I think you need to write the deletion code after the insertion of the record or the transform happens .
Because if you delete first and then try to transform it wont fin andy attachments to parse and load at all
so remove the deletion code from top and place it at the end of the code.These below set of lines should be placed after current.setAbortAction(true);
var attID = new GlideRecord('sys_attachment');
attID.get('sys_id'); // Attachment record sys_id
//Delete existing attachment from data source
var sourceID = new GlideRecord("sys_data_source");
if(sourceID .get(dataSourceSysId)){
var attach = new GlideSysAttachment();
attach.deleteAll(sourceID );
}
And coming to the script they are using Import Set API to load the record into staging table using transform map and using the attachments attached to the data source that we gave in the script and parse the attachment to load the data.
var attachRef= new GlideSysAttachment();
attachRef.writeContentStream(sourceID ,attID.getValue("file_name"), attID.getValue("content_type"), attachRef.getContentStream(attID.getUniqueValue()));
This part of script is getting the attachment
var transformMapSysIDs= 'bedc1fb21b909d5085eb64a2604bcbea';
//Time to load excel file into import table
var loader = new GlideImportSetLoader();
var importSetRec = loader.getImportSetGr(current);
var ranload = loader.loadImportSetTable(importSetRec, current);
importSetRec.state = "loaded";
importSetRec.update();
This part of script is loading the records into staging table in loaded state .This we call it as import set API .
var transformWorker = new GlideImportSetTransformerWorker(importSetRec.sys_id,transformMapSysIDs);
transformWorker.setBackground(true);
transformWorker.start();
gs.addInfoMessage("Your import file has been submitted.");
//To avoid to create another data source we abort RP insert
current.setAbortAction('true');
This part of the code is transforming the data into the target table
Please accept the solution if it helps you and close the thread as this might help others too
Thanks