Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

UI action to add attachment

MaharshiC
Tera Contributor

Hi ,

 

I want to create a UI action which will show a popup to add attachment and then from the attachment I want to create the records in the table. Each row in the excel should create a new record. I have written a script which creates the record from the attachment but I am not able to create the UI action for the pop up to add attachment and also this script should be added to the UI action.

var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
// Use the attachment sys_id of an Excel file .
// Returns a GlideScriptableInputStream object given the sys_id of an attachment.

var attachmentStream = attachment.getContentStream("f577649cc3855210afe2ffbc0501311a");
// Set the source to be parsed
parser.setSource(attachmentStream);
// Get the worksheet names to be parsed in the Excel document
var list_sheet_name = parser.getSheetNames();
gs.info(" Sheet Names " + list_sheet_name.join(", "));
// Iterate over each worksheet in the Excel workbook
for (var i = 0; i < list_sheet_name.length; i++) {
gs.info("**************************************************************************************");
gs.info("Sheet name: " + list_sheet_name[i]);
// Set the worksheet name to be parsed
parser.setSheetName(list_sheet_name[i]);
// Parse each worksheet set using setSheetName()
if (parser.parse()) {
//retrieve the column headers
var headers = parser.getColumnHeaders();
var header1 = headers[0];
var header2 = headers[1];
var header3 = headers[2];
 
 
// Iterate over each row in the worksheet
while (parser.next()) {
var row = parser.getRow();
//print row value for both columns
var inc= new GlideRecord('task_cmdb_ci_service');
inc.initialize();
inc.setDisplayValue('cmdb_ci_service',row[header1]);

 inc.insert();
gs.info('AAAAefgh'+row[header1]);
gs.info('efgh'+row[header1] + '\t|| ' + row[header2] + '\t||' + row[header3]);
}
} else
gs.info(parser.getErrorMessage());
}
Regards,
Maharshi
4 REPLIES 4

Mani A
Tera Guru

use glideDailogWindow in UI action and create a new UI page to create a view to show attachment button 

MaharshiC
Tera Contributor

Hi mani,

Thanks for the response. After the record is created I want to add the script so that it inserts the record in my table based on the rows in the attached excel.

var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
// Use the attachment sys_id of an Excel file .
// Returns a GlideScriptableInputStream object given the sys_id of an attachment.

 

var attachmentStream = attachment.getContentStream("f577649cc3855210afe2ffbc0501311a");
// Set the source to be parsed
parser.setSource(attachmentStream);
// Get the worksheet names to be parsed in the Excel document
var list_sheet_name = parser.getSheetNames();
gs.info(" Sheet Names " + list_sheet_name.join(", "));
// Iterate over each worksheet in the Excel workbook
for (var i = 0; i < list_sheet_name.length; i++) {
gs.info("**************************************************************************************");
gs.info("Sheet name: " + list_sheet_name[i]);
// Set the worksheet name to be parsed
parser.setSheetName(list_sheet_name[i]);
// Parse each worksheet set using setSheetName()
if (parser.parse()) {
//retrieve the column headers
var headers = parser.getColumnHeaders();
var header1 = headers[0];
var header2 = headers[1];
var header3 = headers[2];
 
 
// Iterate over each row in the worksheet
while (parser.next()) {
var row = parser.getRow();
//print row value for both columns
var inc= new GlideRecord('task_cmdb_ci_service');
inc.initialize();
inc.setDisplayValue('cmdb_ci_service',row[header1]);

 

 inc.insert();
gs.info('AAAAefgh'+row[header1]);
gs.info('efgh'+row[header1] + '\t|| ' + row[header2] + '\t||' + row[header3]);
}
else
gs.info(parser.getErrorMessage());
}
Writing  a script in the sys_attachment table is not an option so where should we add this script. Also the sys_id should be dynamic

sample code :
 
 
var attach = new GlideRecord('sys_attachment');
attach.addQuery('table_name', 'Table_name'); ( ui action)
attach.addQuery('table_sys_id', 'TABLE_SYS_ID'); ( created ui action sys_id)
attach.query();
attach.next();

var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();

var attachmentStream = attachment.getContentStream(attach.sys_id);
parser.parse(attachmentStream);

//retrieve the column headers
var headers = parser.getColumnHeaders();
var header1 = headers[0];
var header2 = headers[1];
while (parser.next()) {
    var row = parser.getRow();

Marc Mouries
ServiceNow Employee
ServiceNow Employee

Instead of using a UI Action in the legacy UI what about using a modern workspace and trigger a flow when users add an excel file to the record?