tbrosow
5/9/2016 - 9:47 PM

Data Import

Data Import

(function() {
	
	var attachmentRec = new GlideRecord('sys_attachment');
	attachmentRec.addQuery('table_name', 'sys_data_source');
	attachmentRec.addQuery('table_sys_id', current.sys_id);
	attachmentRec.query();
	
	if (attachmentRec.getRowCount() != 1) {
		gs.addErrorMessage('Please attach an import file. Please use only the format xls or csv.');
	} else if (attachmentRec.next()) {
		var fn = String(attachmentRec.file_name);
		var extension = String(fn.substring(fn.length-3,fn.length)).toUpperCase();
		
		if (extension != 'CSV') {
			gs.addErrorMessage('Only CSV files are supported: [' + attachmentRec.file_name + ']');
		} else {
			current.name = 'TL-Upload-' + String(producer.DataSheet).split(',')[0]+ '-'+new GlideDateTime().getNumericValue();
			current.import_set_table_name = String(producer.DataSheet).split(',')[1]; // 'u_tl_part_families';
			current.type = 'File';
			current.format = 'CSV';
			current.sheet_number = 1;
			current.header_row = 1;
			current.file_retrieval_method = 'Attachment';
			current.zipped = false;
			
			var redirectStr = "sys_import.do?"; // the URI
			redirectStr += "import_source=data_source"; // this is a Data Source import
			redirectStr += "&sysparm_data_source=" + current.sys_id; // the sys_id of this
			redirectStr += "&sysparm_tablename=" + current.import_set_table_name; // always create the same test
			redirectStr += "&sysparm_recreate_table=false"; // we dont want to overwrite existing
			redirectStr += "&sysparm_tablelabel=" + current.name;
			redirectStr += "&sysparm_transform_after_load=true"; // run transformation right after
			redirectStr += "&sysparm_extends=sys_import_set_row"; // always extends this
		}
		if (redirectStr != '')
			gs.setRedirect(redirectStr);
		else
			gs.addErrorMessage('Not possible to import file: ' + attachmentRec.file_name);
	} else {
		gs.addErrorMessage('Not possible to import file.');
	}
})();
//Ignore record
ignore = true;

function _log(_text) {
    log.info('TL Part Repair Method: (' + source.sys_import_row + ') : ' + _text);
}