Upload Affected CI's for a Change Request from a .csv or .txt attachment
When I had this requirement at the start, I had very less information to work on.Just a class called
SysAttachment and a method called getBytes(Inherited from the Object class).
I am handling the entire conversion of the entire attachment into an array of strings in the UI action itself. You can use a Script Include and call this Script Include from the UI action if you choose to make this client callable.
Name of the UI Action : AddCI's
//This will only get the CI names from a csv sheet. You can do similar thing for a .txt file.
var gr = new GlideRecord('sys_attachment');gr.addQuery('table_sys_id', current.sys_id);gr.addQuery('file_name','UploadCI.csv');gr.addQuery('content_type','application/vnd.ms-excel');gr.query();if (gr.next()){ var sa = new Packages.com.glide.ui.SysAttachment(); var binData = sa.getBytes(gr); var string = new Packages.java.lang.String(binData); gs.log(string);var arr = string.split("\n"); for(i=0; i.toString(); var len = str.length(); var newStr = str.substring(0,len-2); var grA = new GlideRecord("cmdb_ci"); grA.addQuery("name",'STARTSWITH',newStr); grA.query(); if(grA.next()){ taskCI.ci_item = grA.sys_id; taskCI.insert(); } else { gs.addInfoMessage(" There is no CI by the name:"+arr); } } action.setRedirectURL(current);}else{ gs.addInfoMessage("You haven't attached any file by the name UploadCI.csv"); action.setRedirectURL(current);
}
Also, As this is a one Column CSV i am just splitting it once using "\n". If you have more than one column you can split it twice, once using a "\n" the next time using a ","( if the delimiter is a comma).
https://www.servicenow.com/community/in-other-news/upload-affected-ci-s-for-a-change-request-from-a-csv-or-txt/ba-p/2276012