Created
September 6, 2016 17:42
-
-
Save asimpkin/e09673227d5c313f62c69bbd8f54a972 to your computer and use it in GitHub Desktop.
ServiceNow workflow script to read a TASK attachment and put the comments into a JSON format within the task.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tableName = 'sc_req_item'; | |
var sysIDOfRecord = current.sys_id; | |
//Declare a new instance of GlideSysAttachment. | |
var gsa = new GlideSysAttachment(); | |
//Get the raw bytes in the file | |
var bytesInFile = gsa.getBytes(tableName, sysIDOfRecord); | |
//Convert that jive into a string using Java/Rhino. | |
var dataAsString = Packages.java.lang.String(bytesInFile); | |
//Re-convert to a string in Javascript, cause we don't trust Rhino. | |
csv = String(dataAsString); | |
current.work_notes = csv; | |
current.u_json_data = csvJSON(csv); | |
//var csv is the CSV file with headers | |
function csvJSON(csv){ | |
var lines=csv.split("\n"); | |
var result = []; | |
var headers=lines[0].split(","); | |
for(var i=1;i<lines.length;i++){ | |
var obj = {}; | |
var currentline=lines[i].split(","); | |
for(var j=0;j<headers.length;j++){ | |
obj[headers[j].trim()] = currentline[j].trim(); | |
} | |
result.push(obj); | |
} | |
//return result; //JavaScript object | |
return JSON.stringify(result); //JSON | |
} // end function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment