-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathimportSetScript.js
62 lines (54 loc) · 1.81 KB
/
importSetScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var importSet = importData();
processData(importSet);
function importData() {
var rec_id = current.sys_id;
var file_type = 'Excel';
//insert data source
var ds = new GlideRecord('sys_data_source');
ds.initialize();
ds.name = 'Universal_Relief_Request_uploader';
ds.import_set_table_label = 'Universal Relief Request Import';
ds.import_set_table_name = 'u_universal_relief_request_import';
ds.header_row = 2;
ds.type = 'File';
ds.format = file_type;
ds.file_retrieval_method = 'Attachment';
ds.insert();
//in this example I'm moving the attachment to the data source record, but you can copy it so it stays on the original record which is probably better!
var table_name = ds.getTableName();
var table_sys_id = ds.sys_id;
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', rec_id+'');
att.addQuery('table_name', 'sc_req_item');
att.addEncodedQuery('file_nameENDSWITH.xlsx^ORfile_nameENDSWITH.xls');
att.orderByDesc('sys_created_on');
att.query();
if (att.next()) {
att.table_name = table_name;
att.table_sys_id = table_sys_id;
att.update();
} else {
return 'ERROR';
}
//create the import set
var iset = new GlideRecord('sys_import_set');
iset.initialize();
iset.short_description = ds.name;
iset.mode = 'asynchronous';
iset.state = 'loaded';
iset.data_source = ds.sys_id;
iset.table_name = ds.import_set_table_name;
var importSetID = iset.insert();
//load the data into the import set
var isl = new GlideImportSetLoader();
isl.loadImportSetTable(iset, ds);
return importSetID+'';
}
function processData(importSetID) {
var import_id = importSetID;
//start the transform
var transformWorker = new GlideImportSetTransformerWorker(import_id+'', '82577941139a36009317b1e32244b04c'); //transform map
transformWorker.setBackground(false);
transformWorker.start();
return true;
}