- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 07:37 AM
I want to load data of attachment from RITM to a table.
Is it possible.
Basically it means i want to load data in service now table when raised an RITM and attached any file.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 08:28 AM
Okay. So Assuming you know how to setup Data Sources, Import Sets and Transform Maps. Below are the steps you can follow to implement this.
1. Create your Data Source of Type File and Format is CSV. (Configure other parameters as needed)
2. Create your Transform Map.
3. Now that you have a Data Source and a Transform Map setup.
use the below function Calls in a UI Action/Business Rule to Kick off the Process
var ImportUtilObj = new Import_Util();
var areAttachmentsValid = ImportUtilObj.validateAttachments(current.getTableName(),current.sys_id);
if(areAttachmentsValid)
{
var dataSource=ImportUtilObj.getDataSourceByName('your Data Source name');
var transformMap= ImportUtilObj.getTransformMapByName('your Transform Map name');
ImportUtilObj.loadAttachmentforDataSource(dataSource,current.getTableName(),current.sys_id);
var importSetRec=ImportUtilObj.importAndTransformCSV(dataSource,transformMap);
}
4. Step 3 is dependent on the below Script Include Named: Import_Util
var Import_Util = Class.create();
Import_Util.prototype = {
initialize: function() {},
getDataSourceByName: function(dataSourceName) {
//get the DataSource
var dataSource = new GlideRecord('sys_data_source');
dataSource.get('name', dataSourceName);
return dataSource;
},
loadAttachmentforDataSource: function(dataSource, table_name, table_sys_id) {
//delete all the attachments before attaching a new CSV file
var attach = new GlideSysAttachment();
attach.deleteAll(dataSource);
//Attach the new CSV after deleting all the old CSV'
GlideSysAttachment.copy(table_name, table_sys_id, 'sys_data_source', dataSource.sys_id);
},
importAndTransformCSV: function(dataSource, transformMap) {
// Process data source file
var loader = new GlideImportSetLoader();
var importSetRec = loader.getImportSetGr(dataSource);
var ranload = loader.loadImportSetTable(importSetRec, dataSource);
importSetRec.state = "loaded";
importSetRec.update();
// Transform import set
var transformWorker = new GlideImportSetTransformerWorker(importSetRec.sys_id, transformMap.sys_id);
transformWorker.setBackground(true); // if you want to run it in Foreground then set it to False
transformWorker.start();
return importSetRec;
},
getTransformMapByName: function(transformMapName) {
//get the DataSource
var transformMap = new GlideRecord('sys_transform_map');
transformMap.get('name', transformMapName);
return transformMap;
},
validateAttachments: function(table_name, table_sys_id) {
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name', table_name);
attachment.addQuery('table_sys_id', table_sys_id);
attachment.query();
var multiple_attchmentCheck = attachment.getRowCount();
if (multiple_attchmentCheck > 1) {
gs.addErrorMessage('Please attach only one CSV file, multiple attachments are not supported');
attachment.deleteMultiple();
return false;
} else if (multiple_attchmentCheck == 0) {
gs.addErrorMessage('Please attach atleast one CSV file');
return false;
} else
return true;
},
type: 'Import_Util'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 08:10 AM
Can you provide any example, it will be helpful to me.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 08:28 AM
Okay. So Assuming you know how to setup Data Sources, Import Sets and Transform Maps. Below are the steps you can follow to implement this.
1. Create your Data Source of Type File and Format is CSV. (Configure other parameters as needed)
2. Create your Transform Map.
3. Now that you have a Data Source and a Transform Map setup.
use the below function Calls in a UI Action/Business Rule to Kick off the Process
var ImportUtilObj = new Import_Util();
var areAttachmentsValid = ImportUtilObj.validateAttachments(current.getTableName(),current.sys_id);
if(areAttachmentsValid)
{
var dataSource=ImportUtilObj.getDataSourceByName('your Data Source name');
var transformMap= ImportUtilObj.getTransformMapByName('your Transform Map name');
ImportUtilObj.loadAttachmentforDataSource(dataSource,current.getTableName(),current.sys_id);
var importSetRec=ImportUtilObj.importAndTransformCSV(dataSource,transformMap);
}
4. Step 3 is dependent on the below Script Include Named: Import_Util
var Import_Util = Class.create();
Import_Util.prototype = {
initialize: function() {},
getDataSourceByName: function(dataSourceName) {
//get the DataSource
var dataSource = new GlideRecord('sys_data_source');
dataSource.get('name', dataSourceName);
return dataSource;
},
loadAttachmentforDataSource: function(dataSource, table_name, table_sys_id) {
//delete all the attachments before attaching a new CSV file
var attach = new GlideSysAttachment();
attach.deleteAll(dataSource);
//Attach the new CSV after deleting all the old CSV'
GlideSysAttachment.copy(table_name, table_sys_id, 'sys_data_source', dataSource.sys_id);
},
importAndTransformCSV: function(dataSource, transformMap) {
// Process data source file
var loader = new GlideImportSetLoader();
var importSetRec = loader.getImportSetGr(dataSource);
var ranload = loader.loadImportSetTable(importSetRec, dataSource);
importSetRec.state = "loaded";
importSetRec.update();
// Transform import set
var transformWorker = new GlideImportSetTransformerWorker(importSetRec.sys_id, transformMap.sys_id);
transformWorker.setBackground(true); // if you want to run it in Foreground then set it to False
transformWorker.start();
return importSetRec;
},
getTransformMapByName: function(transformMapName) {
//get the DataSource
var transformMap = new GlideRecord('sys_transform_map');
transformMap.get('name', transformMapName);
return transformMap;
},
validateAttachments: function(table_name, table_sys_id) {
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name', table_name);
attachment.addQuery('table_sys_id', table_sys_id);
attachment.query();
var multiple_attchmentCheck = attachment.getRowCount();
if (multiple_attchmentCheck > 1) {
gs.addErrorMessage('Please attach only one CSV file, multiple attachments are not supported');
attachment.deleteMultiple();
return false;
} else if (multiple_attchmentCheck == 0) {
gs.addErrorMessage('Please attach atleast one CSV file');
return false;
} else
return true;
},
type: 'Import_Util'
};

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 01:49 PM
Please Mark the Answer as Helpful/Correct if it answered your question.So that others may help others in future.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2021 02:55 AM
Hi Aman In script Include how to check whether the attached file is csv or not(which is attached in service catalog).Please help me with that