Cannot add table to import set
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2023 07:54 AM
This BR runs until this log gs.log("File name inserted into sys_data_source for " + latestFilename + " by user " + currentUserId);
, and then it gives me false in if(ranout). I am trying to get file from user attachment in Change Request and then parse it into a table,
(function executeRule(current, previous /* , g */) {
// Get the current user's sys_id
var currentUserId = gs.getUserID();
gs.log("Business Rule Running" + currentUserId);
var gr = new GlideRecord('sys_attachment');
var firstName = gs.getUser().getFirstName().toLowerCase();
gs.log("FirstName" + firstName);
gr.addQuery('sys_updated_by', firstName);
gr.orderByDesc('sys_created_on');
gr.query();
if (gr.next()) {
var latestFilename = gr.file_name;
// Set the retrieved filename to a field or perform other actions here
current.latest_inserted_filename = latestFilename;
// Log the message
gs.log("Latest inserted filename by user " + currentUserId + ": " + latestFilename);
var dataSourceGR = new GlideRecord('sys_data_source');
dataSourceGR.initialize();
dataSourceGR.setValue('name', latestFilename); // Set the file name in the 'name' field
dataSourceGR.insert();
gs.log("File name inserted into sys_data_source for " + latestFilename + " by user " + currentUserId);
var loader = new GlideImportSetLoader();
var importSetRec = loader.getImportSetGr(dataSourceGR);
var ranload = loader.loadImportSetTable(importSetRec, dataSourceGR);
importSetRec.state = "loaded";
importSetRec.update();
// Ensure the Import Set has been successfully loaded before creating a Transform Map
if (ranload) {
// Create a new Transform Map
var transformMapGR = new GlideRecord('sys_transform_map');
transformMapGR.initialize();
transformMapGR.setValue('name', 'Excel_transform_map'); // Set the name of your Transform Map
transformMapGR.setValue('source_table', importSetRec.getUniqueValue()); // Set the source table to the Import Set sys_id
transformMapGR.setValue('target_table', 'u_excel_data'); // Set the target table
transformMapGR.insert();
gs.log("Transform Map 'Excel_transform_map' created.");
// Trigger the transformation (uncomment and modify as needed)
/*
var transformMapSysID = transformMapGR.getUniqueValue();
var importSetRecSysID = importSetRec.getUniqueValue();
var transformWorker = new GlideImportSetTransformerWorker(importSetRecSysID, transformMapSysID);
transformWorker.setBackground(true);
transformWorker.start();
gs.log("Transformation started for " + latestFilename + " by user " + currentUserId);
*/
} else {
gs.log("Import Set could not be loaded. Check your data source.");
gs.log("ranload: " + ranload);
gs.log("Error message, if any: " + importSetRec.getLoadErrorMessage());
}
} /* else {
gs.log("Attachment not found for user " + currentUserId);
} */
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 07:20 AM - edited 09-19-2023 07:22 AM
hi @palanikumar
Please can you check this. This is what I have done now.
1. Created Data Source of Type File and Format is Excel.
2. Created Transform Map.
3. Now that I have Data Source and a Transform Map setup.
used this code in Business Rule
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var ImportUtilObj = new Import_Util();
var areAttachmentsValid = ImportUtilObj.validateAttachments(current.getTableName(),current.sys_id);
if(areAttachmentsValid)
{
gs.log("Valid Attachments");
var dataSource=ImportUtilObj.getDataSourceByName('19datasource');
var transformMap= ImportUtilObj.getTransformMapByName('19transform_map');
ImportUtilObj.loadAttachmentforDataSource(dataSource,current.getTableName(),current.sys_id);
var importSetRec=ImportUtilObj.importAndTransformExcel(dataSource,transformMap);
gs.log("import set"+importSetRec);
}
})(current, previous);
Images of Transform map and data source
This is my script include.
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);
},
importAndTransformExcel: 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 Excel file, multiple attachments are not supported');
attachment.deleteMultiple();
return false;
} else if (multiple_attchmentCheck == 0) {
gs.addErrorMessage('Please attach atleast one Excel file');
return false;
} else
return true;
},
type: 'Import_Util'
};
Can you please explain what is the issue with this ?