Last active
October 24, 2019 00:32
-
-
Save eduglez/0b3bc782be5ea820ab1d4a0b8c9754a3 to your computer and use it in GitHub Desktop.
ServiceNow - Generate CSV Test File from Real Data retrieved from a REST call
This file contains 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
// To be used directly from the console | |
// CSV fields | |
var FIELDS = "field_1,field_2,field_3".split(","); | |
var counter = 0; | |
// Dynamic creation of data for the fields defined here | |
var GENERATED_FIELDS = { | |
field_1: function(){ | |
counter = counter + 1; | |
return "TEST_FIELD_1" + counter; | |
}, | |
field_2: function() { | |
return "FIELD_2_IS_CONSTANT"; | |
} | |
}; | |
var transformTable = jQuery.ajax({type: "GET", url: "https://YOUR_INSTANCE.service-now.com/REALDATATABLE_list.do?sysparm_query="+"&JSONv2&sysparm_action=getRecords", async: false}).responseJSON.records; | |
var csvRows = ""; | |
transformTable.forEach(function(importRow){ | |
var row = []; | |
FIELDS.forEach( | |
function(field){ | |
if(typeof GENERATED_FIELDS[field] != 'undefined'){ | |
row.push(GENERATED_FIELDS[field]()); | |
}else if(typeof importRow[field] != 'undefined'){ | |
var strField = importRow[field] + ""; | |
strField = strField.replace(/"/g, '""'); | |
if(strField.indexOf(",")!= -1 || strField.indexOf('"')!= -1){ | |
row.push('"' + strField +'"'); | |
}else{ | |
row.push(strField); | |
} | |
}else{ | |
row.push('NULL_' + field); | |
} | |
} | |
); | |
// Repeat the data if needed | |
for(var iRep = 0 ; iRep < 800; iRep = iRep + 1){ | |
csvRows = csvRows + row.join(",")+"\n"; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment