Do you want to perform Master Data uploads via a Catalog Item?
ServiceNow gives you the option to make use of the following capabilities:
- Catalog Items
- UI Page
- UI Macro
- Workflows
- Script Actions
- Script Includes
- Data Sources
- Import Sets
- Transform Maps
- Notifications
- Reports
In our use case, we make use of these components for the upload of CI’s and Assets.
Preparation per category
- Create a template for the upload of CI’s and Assets
- Make the import set and transform map
- Verify the upload of CI’s and Assets
Create a new catalog item for the mass upload of CI’s/Assets
- With the variables: Requested for, Category, Template, Reporting results, attachment
- Make sure you add the attachment with the master data to be uploaded in the correct template
- UI Page for download the correct template based on the selected category
- UI Macro for uploading the attachment
- Create a catalog client script OnSubmit
- To check if the attachment is added
- To check if there is only one attachment with the correct template and extension
Create a new workflow for the Catalog item
Add the following steps:
- Create Datasource, Scheduled Import and Run Transformation (Run Script that calls a Script Action)
dataSourceImport();
function dataSourceImport(){
gs.eventQueue('mass.data.uploads.catalog.item', current, current.variables.import_category, current.variables.requested_for.user_name);
} - Create Exception Report (Run Script that calls a Script Include)
createExceptionReport();
function createExceptionReport(){
var result = new MassDataLoadsUtil().phSendExceptionReport(current.variables.requested_for, current.variables.import_category, current.variables.report_results, current.variables.email_to, current.variables.import_set);
}
The called script actions will make use of a script include
- Create Datasource
//create the data source record and copy the attachment from the catalog item
var ds = new GlideRecord('sys_data_source');
ds.initialize();
ds.name = dataSource;
ds.import_set_table_name = importTable;
ds.import_set_table_label = importLabel;
ds.type= dsType;
ds.format = dsFormat;
ds.file_retrieval_method = dsRetrievalMethod;
if (dsFormat == 'Excel') {
ds.sheet_number = dsSheetNr;
ds.header_row = dsHeaderRow;
} else if (dsFormat == 'CSV'){
ds.delimiter = dsDelimiter;
}
var NewDataSource = ds.insert();
//copy attachment from the catalog item to the dataSource
GlideSysAttachment.copy('sc_req_item',current.sys_id,'sys_data_source',NewDataSource); - Schedule Import and Run Transformation
//Load the data into an import set and transform this based on the related transform map(s)
var sourceGr = new GlideRecord('sys_data_source');
sourceGr.addQuery('sys_id', source);
sourceGr.query();
// if we have our data source continue
if(!sourceGr.next()) {
return;
}
//create the import set and load this with the data from the data source
var loader = new GlideImportSetLoader();
var importSetGr = loader.getImportSetGr(sourceGr);
var ranload = loader.loadImportSetTable(importSetGr, sourceGr);
if(!ranload) {
//Failed to load import set
return;
}
//Running Transfom map(s) of the related import set
var mapName = 'IMP TTM Load Cis and Assets: ' +importCategory;
var importSetRun = new GlideImportSetRun(importSetGr.sys_id);
var importLog = new GlideImportLog(importSetRun, mapName);
var ist = new GlideImportSetTransformer();
ist.setLogger(importLog);
ist.setImportSetRun(importSetRun);
ist.setSyncImport(true);
ist.transformAllMaps(importSetGr); - Create Exception Report
//create a list report based on the input variables (all results or only errors) per category
var grRpt = new GlideRecord('sys_report');
grRpt.initialize();
grRpt.title = sTitle;
grRpt.table = sTable;
grRpt.type = sType;
grRpt.filter = sCondition;
grRpt.field_list = sFields;
grRpt.orderby_list = sOrder;
grRpt.user = inRequestedFor;
grRpt.roles = '';
var rptID = grRpt.insert(); - Send Notification with the Exception Report
//execute this report direct and send the results to the related users/groups with the report subject and body text
var userID = sEmails;
var grSchRpt = new GlideRecord('sysauto_report');
grSchRpt.initialize();
grSchRpt.address_list = sEmails;
grSchRpt.user_list = userID;
grSchRpt.output_type = sOutputType;
grSchRpt.report = sRptID;
grSchRpt.run_type = sRunType;
grSchRpt.report_title = sSubject;
grSchRpt.report_body = sBody;
grSchRpt.name = 'Auto Sending of ' + grSchRpt.report.title;
var schRptID = grSchRpt.insert(); // since it is a "once" schedule, it will be executed right away
By following above steps you should be able to trigger the upload via a catalog item!
[at]