Last active
January 18, 2022 18:09
-
-
Save nabil-nuvolo/b1f724acb2ff51cb783b003d9e67af9d to your computer and use it in GitHub Desktop.
A series of methods that can be strung together to automate data import through Import Sets
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
// @param att {GlideRecord - sys_attachment} | |
function createDataSource(att) { | |
// Create new data source | |
var ds = new GlideRecord("sys_data_source"); | |
ds.initialize(); | |
ds.setValue("name", att.getValue("file_name")); | |
ds.setValue("import_set_table_name", "x_nuvo_eam_pi_device_import"); | |
ds.setValue("type", "File"); | |
ds.setValue("format", "CSV"); | |
ds.setValue("file_retrieval_method", "Attachment"); | |
ds.setValue("file_path", att.getValue("file_name")); | |
var newDataSource = ds.insert(); | |
// Copy project attachment to data source | |
var ga = new GlideSysAttachment(); | |
ga.copy(att.getValue("table_name"), | |
att.getValue("table_sys_id"), | |
'sys_data_source', | |
newDataSource); | |
loadDataSource(newDataSource); | |
return newDataSource; | |
} |
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
function loadDataSource(datasource_sysid) { | |
var ds = new GlideRecord("sys_data_source"); | |
ds.get(datasource_sysid); | |
var redirectStr = "sys_import.do?"; | |
redirectStr += "import_source=data_source"; | |
redirectStr += "&sysparm_data_source=" + ds.getUniqueValue(); | |
redirectStr += "&sysparm_tablename=" + ds.getValue("import_set_table_name"); | |
redirectStr += "&sysparm_recreate_table=false"; | |
redirectStr += "&sysparm_tablelabel=" + encodeURIComponent(ds.getValue("name")); | |
redirectStr += "&create_new_module=ON"; | |
redirectStr += "&sysparm_extends=sys_import_set_row"; | |
redirectStr += "&selected_application=import_sets"; | |
var resourceBase = gs.getProperty('glide.servlet.uri'); | |
var sm = new sn_ws.RESTMessageV2(); | |
var resource = resourceBase + redirectStr; | |
sm.setHttpMethod("get"); | |
sm.setEndpoint(resource); | |
try { | |
var userId = ""; // SYSTEM IMPORT USER WITH ADMIN CREDENTIAL | |
var pwd = ""; // SYSTEM IMPORT USER WITH ADMIN CREDENTIAL | |
sm.setBasicAuth(userId,pwd); | |
var response = sm.execute(); | |
var status_code = response.getStatusCode(); | |
if (!status_code.toString().startsWith("20")) { | |
gs.error("Import failed with error code " + status_code); | |
return; | |
} | |
} catch(er) { | |
gs.error("Import failed with error " + er.getMessage()); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scripts above can be used to copy a CSV file to a data source, then import that data source into an Import Set.
Once an import set is created with data from the CSV, the
GlideImportSetTransformerWorker
class can be used to import data using transform maps without user interaction.Further implementation steps are left to the developer to handle multiple transform maps, handling of errors and other issues that may arise.