How to upload/read excel file data from service portal widget to load data into service now table?

Mehak1
Giga Expert
Hi,
 

I want to upload an excel file from service now portal widget so that it reads the excel data and imports into service now tables.

Can anyone guide me how to implement this as I am new to service portal?

 

Regards,

Mehak Arora

3 REPLIES 3

Vikram3
Giga Guru

Hi Mehak

You need to create a widget dependency where you include the following three javascripts:

 

https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js

 

https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.10.8/xlsx.full.min.js

 

https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js

 

Then build a widget (with this dependency connected to it) where you include a file input field:

 

<b>Upload excel</b> <input type="file" id="file" ng-model="csvFile" onchange="angular.element(this).scope().ExcelImport(event)"/>

 

And in your client side script define a function to read the file

 

//Read attached Excel file

$scope.ExcelImport= function (event) {

        $scope.status = "Please wait - Loading data from Excel";

        var input = event.target;

        var reader = new FileReader();

        reader.onload = function(){

                  var fileData = reader.result;

                  var wb = XLSX.read(fileData, {type : 'binary'});

                  wb.SheetNames.forEach(function(sheetName){

                            if (sheetName == "Name ABC") { //Only process the sheet called "Name ABC"

                                      var rowObj =XLSX.utils.sheet_to_row_object_array(wb.Sheets[sheetName]);

                                      var jsonObj = JSON.stringify(rowObj);

                                      //Send excel json to Server

                                    c.server.get({excel: true, rowObj:rowObj, type: $scope.type}).then(function(r) {

                                                //do something with server response

                                  });

                            }

                  })

        };

        reader.readAsBinaryString(input.files[0]);

};

On the server side process the java object or (or send the json if you prefer) and insert into your import table.

verda
Mega Expert

Can you tell us what you mean by processing the object on the server side?

The object where the file contains.