Validate the excel template automatically, when the user submits the request. Validate if it has the columns as mentioned

mahfooz1
Tera Contributor

On submitting the request after attaching the attachment, if user has attached file other that xlsx format then request should not be submitted.
Also, if user has attached xlsx file then Validate the excel template automatically, when the user submits the request. Validate if it has the columns as mentioned.

1 ACCEPTED SOLUTION

mr18
Tera Guru
Tera Guru

Please find the below Business Rule:

Table: sc_cart_item

When: before, insert

var gr1 = new GlideRecord('sys_attachment');
gr1.orderByDesc('sys_created_on');
gr1.setLimit(1);
//gr1.addQuery('table_sys_id', gr1.sys_id);
gr1.addQuery('table_name','sc_cart_item');
gr1.addQuery('file_name', 'CONTAINS', '.xlsx');
gr1.addEncodedQuery('sys_created_onRELATIVEGT@minute@ago@1');
gr1.query();

if(gr1.next())
{
//gs.addInfoMessage('Excel: inside while: attachment: ' + gr1.file_name);
var header_count = 0;


var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
// use attachment sys id of an excel file attachment
var attachmentStream = attachment.getContentStream(gr1.sys_id);
parser.parse(attachmentStream);
//retrieve the column headers
var headers = parser.getColumnHeaders();

var a = headers[0];
var b = headers[1];
var c = headers[2];
var d = headers[3];
var e = headers[4];
var f = headers[5];
var g = headers[6];
var h = headers[7];
var i = headers [8];
var j = headers[9];
var k = headers[10];
var l = headers[11];
var m = headers[12];
//gs.addInfoMessage('Column in the attachment contains: ' + a + ', ' + b + ', ' + c + ', ' + d + ', ' + e + ', ' + f + ', ' + g + ', ' + h + ', ' + i + ', ' + j + ', ' + k + ', ' + l) ;
//var obj = {};
// while(parser.next()) {
// var row = parser.getRow();
// //print row value for both columns
// gs.addInfoMessage(row[key] + ': ' + row[value]) ;
// obj[row[key]] = row[value];

if(a!='Asset tag' || b!='Serial number' || c!='Model category' || d!='Model' || e!='Owned by' || f!='Managed by group' || g!='Managed by' || h!='Assigned to' || i!='Company' || j!='State' || k!='Substate' || l!='Location' || m!='Cost')
{
gs.addErrorMessage('The columns used does not match with the template. Kindly review the template');
current.setAbortAction(true);
}
}

View solution in original post

6 REPLIES 6

Deepak Ingale1
Mega Sage

Hi,

 

You can use the GlideExcelParser class to read the content of EXCEL file

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=GEPS-GlideExcelParser

 

Note: Please mark reply as correct if it has answered your question or mark it as helpful if you found reply as helpful

AbhishekGardade
Giga Sage

Hello Mahfooz,

Below is the code for Fetching Contents from Excel File

var parser = new sn_impex.GlideExcelParser();

var attachment = new GlideSysAttachment();

// use attachment sys id of an excel file attachment

var attachmentStream = attachment.getContentStream('f18e8e0a1b2f3300364d32a3cc4bcb99');

parser.parse(attachmentStream);

//retrieve the column headers

var headers = parser.getColumnHeaders();

var key = headers[0];

var value = headers[1];

//var obj = {};

while(parser.next()) {

                        var row = parser.getRow();

                        //print row value for both columns

                        gs.log(row[key] + ': ' + row[value]) ;

               //         obj[row[key]] = row[value];

}

Please mark as Correct Answer and Helpful, if applicable.
Thanks!
Abhishek Gardade
Hexaware Technologies

Thank you,
Abhishek Gardade

Hi Abhishek

Can you help me in solving the below requiremnt as you mentioned above 

please help me in suggesting the code to do it

i had source field names shown below

when i try to attach the excel sheet with different header name the transform map should not insert data to my target table 

Can you suggest me the code 

 the values of headers are not matching with source field names

so it should not allow to run transform

 

Thank You

Shashank 

 

 

Hi Abhishek,

After fetching value from excel, can we check that value in a table.