-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
83 lines (73 loc) · 2.56 KB
/
app.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var fs = require('fs');
var xml2js = require('xml2js');
var json2csv = require('json2csv');
var fieldSchema = ['fullName','label','description','type','length','required','unique',
'trackHistory','trackTrending','trackFeedHistory','formula','pickListValues', 'defaultPicklistValue', 'pickListControllingField'];
var objectDirectory = __dirname + '/objects/';
files = fs.readdirSync(__dirname + '/objects/');
for (var i = 0; i < files.length; i++) {
analyzeObjectFile(files[i]);
}
function analyzeObjectFile(objectName) {
var parser = new xml2js.Parser();
fs.readFile(objectDirectory + objectName, function(err, data) {
parser.parseString(data, function (err, result) {
var objData = result.CustomObject.fields;
for (var i in objData) {
if (objData.hasOwnProperty(i)) {
var element = objData[i];
arrayToString(element);
}
}
json2csv({ data: objData, fields: fieldSchema}, function(err, csv) {
fs.writeFile('results/'+objectName.replace('.object','') + '.csv', csv, function(err) {
if (err){
throw err;
}
console.log(objectName + ': file saved');
});
});
});
});
}
function arrayToString(element) {
for (var variable in element) {
if (element.hasOwnProperty(variable)) {
if(element[variable][0] === 'Picklist'){
formatPickListValues(element);
}
if(typeof element[variable][0] == 'string'){
element[variable] = element[variable][0];
} else {
arrayToString(element[variable]);
}
}
}
}
function formatPickListValues(element) {
var controllingField = '';
if (!element.valueSet){
return;
}
if (!element.valueSet[0].valueSetDefinition){
return;
}
if(element.valueSet[0].hasOwnProperty('controllingField')){
controllingField = element.valueSet[0].controllingField[0];
}
var pickListValues = element.valueSet[0].valueSetDefinition[0].value;
var pickListValuesToString = '';
var defaultPicklistValue = '';
for (var i in pickListValues) {
if (pickListValues.hasOwnProperty(i)) {
pickListValuesToString += pickListValues[i].fullName[0] + ' - ';
if(pickListValues[i].default[0]){
defaultPicklistValue = pickListValues[i].fullName[0];
}
}
}
pickListValuesToString = pickListValuesToString.slice(0, -3);
element.pickListValues = pickListValuesToString;
element.defaultPicklistValue = defaultPicklistValue;
element.pickListControllingField = controllingField;
}