Create RITM from REQ records on submitting REQ with Excel file.

Anish Reghu
Kilo Sage
Kilo Sage

SCENARIO:

A user submits a request from a catalog item, which results in a REQ record. While submitting the request, the user uploads an excel file, which contains the value to be parsed and populated into the RITM.

Note - Each row in the excel has to generate a new RITM.

 

Following has been my approach:

- Validate if the REQ submitted has attachments in the Condition field.

current.hasAttachments();

Excel Content-Type is application/vnd.ms-excel - how to validate? - Then the script to create the RITMs:

var grRITM = new GlideRecord("sc_req_item");
grRITM.initialize();

var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
var attSysID = <NEED_HELP_TO_READ_ATTACHMENT_SYS_ID_FROM_REQUEST_SUBMITTED>;

var attachmentStream = attachment.getContentStream("attSysID");
parser.setSource(attachmentStream);
var list_sheet_name = parser.getSheetNames();
parser.setSheetName(list_sheet_name[i]);

var j = 0;                           //Row counter
var rowVal = [];                       //Array for fetching rowValues

// Parse each worksheet set using setSheetName()
    if (parser.parse()) {

        //retrieve the column headers, assuming excel has only 3 columnns.

        var headers = parser.getColumnHeaders(); 
        var h1 = headers[0];
        var h2 = headers[1];
        var h3 = headers[2];

        // Iterate over each row in the worksheet

        while (parser.next()) {
            j++;
            var row = parser.getRow(); 
            rowVal[j].push(row[h1], row[h2], row[h3]);
        }
    } else
        gs.info(parser.getErrorMessage());

// return rowVal;
}



Now, I want the RITM initialized to be saved with the description populating these extracted values from the excel.

grRITM.description = 
"Input Label 1:"+rowVal[1].row[h1];
+"\nInput Label 2:"+rowVal[2].row[h2];
+"\nInput Label 3:"+rowVal[3].row[h3];

This is with the assumption that the values are in the excel in the below seen format:

find_real_file.png

Kindly suggest, where I have made a mistake and what better should I do to get this work?

 

Regards,

Anish

1 ACCEPTED SOLUTION

It's done. https://code.jace.pro/?id=2022-05-08T12-04-05

 

Thanks Ankit for your help.

 

Cheers to all readers,

Anish

Slowly learning to Script in ServiceNow. 😉

 

View solution in original post

12 REPLIES 12

Hey Vivi,

Thanks for highlighting, was a minor issue with the link, fixed it, please check and let me know.

 

Cheers,

Anish

Oh, that's great, I have opened it now, thank you so much for your fast answer, Anish!

Vivi Brasil

Cheers, no worries...

Wish you success with your piece of work, you are trying to achieve.

 

Best regards,

Anish