jason_petty
Tera Expert

The method below was originally published on Oct 25, 2012, is an outdated process

Please see the following post for help! >   Loading data from an email attachment


I had a number of people respond to this post with the same problem and together with my colleague I re-wrote the instructions to make it work.

I had a customer that wanted to have IBM Maximo product send an email to ServiceNow and have a CSV/XLS file attached to the email automatically create a new Data Source, load the data, then transform it all automatically just from receiving the email. Here is what I did to get that to work:


1. Create a Data Source Manually first so you can set up the Import Set Table and the Transform Map that you will use later. This is necessary since you need a Transform Map defined before you send the email inbound to ServiceNow. This Data Source can be deleted after creating the Import Set Table and Transform Map.
2. Get a sample CSV/XLS file and attach it to the Data Source and load the data at least once.
3. Define your Transform Map and set it up so the data is getting transformed into the right table in the format that you need.
4. Create a user that you will use for basic authentication with no roles and has a password.
5. Create a REST Message that points to your own instance for the REST endpoint: https://instancename/sys_import.do as the url named "Inbound Import Set Poster"
6. Check the box for "Use basic auth" and set the username and password from step #4
7. Edit the "get" function and add the following function parameters:

8. Create an Inbound Email Action with the following settings:

HERE IS THE CODE TO COPY:



if (current.getTableName() == "sys_data_source") {

      current.name = email.subject.substring(3);
      current.import_set_table_label = email.body.import_set_table_label;
      current.import_set_table_name = email.body.import_set_table_name;
      current.file_retrieval_method = email.body.file_retrieval_method;
      current.format = email.body.format;

      current.update();

var r = new RESTMessage('Inbound Import Set Poster', 'get');
r.setStringParameter('tableLabel', encodeURIComponent(current.name));
r.setStringParameter('customDataSource', current.sys_id);
              r.setStringParameter('tableName', current.import_set_table_name);
r.execute();
}


9. The Condition can be anything you want but the Target table needs to be sys_data_source.
10. Enable the inbound email settings in your instance to receive email and process them. See WIKI http://wiki.servicenow.com/index.php?title=Configuring_Email for this.
11. Now send an email like this to the email instancename@unix.service-now.com (The "unix" part of this avoids any email formatting issues with email clients):

  1. To: instancename@unix.service-now.com
  2. Subject: DS:Test Data Source 1
  3. Body:


              import_set_table_label:Test Import Set Table
              import_set_table_name:u_test_import_set_table
              file_retrieval_method:Attachment
              format:CSV

NOTE: CSV would be Excel if you are using an XLS file

12. Wait until the email is processed and it should have created your Data Source with whatever is after the "DS:" in the subject
13. Make sure the import_set_table_name is set to whatever you named your import set table when you created your Data Source in step 1 (This is very important since the Transform Map is linked to this table)
14. The format is set to either Excel or CSV.
15. You need to attach the file you want to attach to the Data Source.

>


The method above was originally published on Oct 25, 2012, is an outdated process

Please see the following post for help! >   Loading data from an email attachment


68 Comments
Eli Guttman
Tera Guru

HI Matt,



Thank you very much! i was able to set is and it is working great!



i had a small typo


tobrien
Kilo Guru

Hello from Helsinki...



I followed the original instructions and was able to get the XLS Attachment imported and transformed using the following script:



if (current.getTableName() == "sys_data_source") {  


   


      current.name = email.subject.substring(3);  


      current.import_set_table_label = email.body.import_set_table_label;  


      current.import_set_table_name = email.body.import_set_table_name;  


      current.file_retrieval_method = email.body.file_retrieval_method;  


      current.format = email.body.format;  




      current.update();  


 


    var r = new RESTMessage('Inbound Import Set Poster', 'get');  


    r.setStringParameter('tableLabel', encodeURIComponent(current.name));  


    r.setStringParameter('customDataSource', current.sys_id);  


    r.setStringParameter('tableName', current.import_set_table_name);  


    r.execute();  


 




  var myNewDataSource = current.sys_id;


  gs.log ('TD002 - Data source inserted with sys_id - ' + myNewDataSource);


  var gr2 = new GlideRecord ('scheduled_import_set');


  gr2.query();


  if (gr2.next()) {


            gs.log ('TDI003 - Found Scheduled Import definition');


            gr2.data_source = myNewDataSource;


            gr2.update();


            gs.log ('TDI004 - Scheduled Import definition updated to point to data source just added');


            SncTriggerSynchronizer.executeNow(gr2);


  } else {


            gs.log('TDI005 - ERROR - Unable to locate scheduled import definition. Please contact your system administrator');


  }




  gs.log ('TDI007 - Inbound email processing complete');


}


bianca_vaccarin
ServiceNow Employee

michael.ritchie wrote up an updated method for Geneva, Helsinki, and Istanbul releases Loading data from an email attachment


manojrajuchemir
Kilo Contributor

when i am trying to read Excel content ,I am seeing the data as like below



<��


��z{O*^� �7A   �yAÈ¢ >��y�S��e�����0 �V}5_#��/=t�F����;3�d�3�����i�u�f �{��'   �)M �����ޖ� �pJ� �!1� «ï¿½ ��'+� 5



Please correct my code to read Excel content line by line



var grAttach = new GlideRecord('sys_attachment');


//bec04983db3a0300e0fcd360cf961987---Text


//85071a1347c12200e0ef563dbb9a71c1---Excel


grAttach.addQuery('table_sys_id', '85071a1347c12200e0ef563dbb9a71c1');//


grAttach.orderByDesc('sys_updated_on');


grAttach.query();


var stringUtil = new GlideStringUtil();


if ( grAttach.next() ) {


var attachID = grAttach.sys_id;


// Get the contents of the file


var sa = new GlideSysAttachment();


var binData = sa.getBytes(grAttach);


gs.print(binData);


strData = Packages.java.lang.String(binData);


var csv_string = stringUtil.base64Decode(strData);


gs.print("content===>"+csv_string);


}


tobrien
Kilo Guru

I think this may be a formatting of the XL document that the iRETRIEVAL METHOD does not understand.



If you have not already, please try an XLS instead of XLSX


manojrajuchemir
Kilo Contributor

I tried but it is not working.I tried for CSV and txt ,it is working but for XLS or XLSX not working


tobrien
Kilo Guru

Based upon this ==>


var csv_string = stringUtil.base64Decode(strData);



The purpose of the mechanism presented above is to extract data from an inbound-email ATTACHMENT (CSV, XLS, XLSX) into a DATA SOURCE so that you could run a TRANSFORM and push the data into a table. It appears to me that you're trying to read the DATA directly from the attachment as a string...



This is why its working for a CSV or TXT since they have no formatting BUT an XLS has GOBs of formatting which is why you're seeing all those goofy characters.





Maitri Shah
Tera Contributor

Hello ,

this post is amazing it gets the work done but i am struggling to a scenario when on one day if there are multiple individual emails triggered with CSV files multiple data source are being created but the schedule import data is being attached to only the last data souce is there a way that it can be attached to all the data source which are created ? please help me if any one has a solution..

 

Thank you in Advanced.