This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroboSchema2.js
223 lines (171 loc) · 6.11 KB
/
roboSchema2.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var fs = require('fs');
var csv2json = require('csvtojson');
var converter = require('json-2-csv');
var rimraf = require("rimraf");
//wipe out the source dir first
rimraf.sync("./src");
//create the dirs
fs.mkdirSync("./src");
fs.mkdirSync("./src/objects");
//load the CSV
var bulkFields;
csv2json()
.fromFile('./transformed.csv')
.then((jsonObj)=>{
bulkFields = jsonObj;
createPackage();
})
function createPackage() {
//console.log(bulkFields);
var fieldsByObject = {};
//rip through the rows and pivot on object
for (var i=0; i<bulkFields.length; i++) {
var row = bulkFields[i];
var objName = row['Object'];
if (!(objName in fieldsByObject)) {
fieldsByObject[objName] = [];
}
fieldsByObject[objName].push(row);
}
//console.log(bulkFields);
//console.log('fieldsByObject', fieldsByObject);
//create the package.xml and the object XML
var packageTemplate = getXMLtemplate();
var memberTemplate = ' <members>{!field}</members>';
var fields = [];
var objects = [];
for (obj in fieldsByObject) {
var memberO = memberTemplate;
memberO = memberO.replace('{!field}', obj);
objects.push(memberO);
var rows = fieldsByObject[obj];
var fieldsForObject = [];
for (var i=0; i<rows.length; i++) {
var row = rows[i];
var objField = obj + '.' + row['Field Name'];
var member = memberTemplate;
member = member.replace('{!field}', objField);
fields.push(member);
fieldsForObject.push(createXMLfieldDef(row));
}
var xmlForObject = getObjectTemplate();
var objectFieldXML = fieldsForObject.join('\n') + '\n';
xmlForObject = xmlForObject.replace('{!label}', rows[0]['Object Label']);
xmlForObject = xmlForObject.replace('{!labelPlural}', rows[0]['Object Plural']);
xmlForObject = xmlForObject.replace('{!fields}', objectFieldXML);
fs.writeFileSync('./src/objects/'+obj+'.object', xmlForObject);
}
var fieldsXML = fields.join('\n') + '\n';
var objectsXML = objects.join('\n') + '\n';
var packageXML = packageTemplate.replace('{!fields}', fieldsXML).replace('{!objects}', objectsXML);
fs.writeFileSync('./src/package.xml', packageXML);
}
function createXMLfieldDef(row) {
/*
//build something like this for each field
<fields>
<fullName>end_time__c</fullName>
<externalId>false</externalId>
<label>End Time</label>
<required>false</required>
<trackHistory>false</trackHistory>
<trackTrending>false</trackTrending>
<type>DateTime</type>
</fields>
*/
function buildXMLfromJSON(fieldDef) {
var x = ' <fields>\n';
for (var key in fieldDef) {
x += ' <' + key + '>' + fieldDef[key] + '</' + key + '>\n';
}
x += ' </fields>';
//console.log('buildXMLfromJSON', x);
return x;
};
//start with some defaults that all fields have
var fieldDef = {};
fieldDef.fullName = row['Field Name'];
fieldDef.label = row['Field Label'];
fieldDef.type = row['Type'];
fieldDef.externalId = (row['External Id'] == '' ? 'false' : row['External Id']);
fieldDef.required = (row['Required'] == '' ? 'false' : row['Required']);
fieldDef.trackHistory = (row['Track Field History'] == '' ? 'false' : row['Track Field History']);
fieldDef.trackTrending = 'false';
fieldDef.unique = (row['Unique'] == '' ? 'false' : row['Unique']);
if (row['Type'] == 'Currency' || row['Type'] == 'Number' || row['Type'] == 'Percent') {
fieldDef.precision = row['Length'];
fieldDef.scale = row['Decimal Places'];
}
if (row['Type'] == 'Text' || row['Type'] == 'LongTextArea') {
fieldDef.length = row['Length'];
}
if (row['Type'] == 'Checkbox') {
fieldDef.defaultValue = 'false';
}
if (row['Type'] == 'LongTextArea') {
fieldDef.visibleLines = (row['Visible Lines'] == '' ? '3' : row['Visible Lines']);
}
if (row['Type'] == 'Lookup') {
delete fieldDef.trackHistory;
delete fieldDef.unique;
fieldDef.deleteConstraint = 'SetNull';
fieldDef.referenceTo = row['Reference To'];
fieldDef.relationshipLabel = row['Relationship Label'];
fieldDef.relationshipName = row['Relationship Name'];
}
if (row['Type'] == 'MasterDetail') {
delete fieldDef.required;
delete fieldDef.trackHistory;
delete fieldDef.unique;
fieldDef.referenceTo = row['Reference To'];
fieldDef.relationshipLabel = row['Relationship Label'];
fieldDef.relationshipName = row['Relationship Name'];
fieldDef.relationshipOrder = 0;
fieldDef.reparentableMasterDetail = 'false';
fieldDef.writeRequiresMasterRead = 'false';
}
return buildXMLfromJSON(fieldDef);
}
function getXMLtemplate() {
var template =
'<?xml version="1.0" encoding="UTF-8"?>\n'+
'<Package xmlns="http://soap.sforce.com/2006/04/metadata">\n'+
' <types>\n{!fields}'+
' <name>CustomField</name>\n'+
' </types>\n'+
' <types>\n{!objects}'+
' <name>CustomObject</name>\n'+
' </types>\n'+
' <version>42.0</version>\n'+
'</Package>';
return template;
}
function getObjectTemplate() {
var template =
'<?xml version="1.0" encoding="UTF-8"?>\n'+
'<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">\n'+
' <allowInChatterGroups>false</allowInChatterGroups>\n'+
' <compactLayoutAssignment>SYSTEM</compactLayoutAssignment>\n'+
' <deploymentStatus>Deployed</deploymentStatus>\n'+
' <enableActivities>false</enableActivities>\n'+
' <enableBulkApi>true</enableBulkApi>\n'+
' <enableFeeds>false</enableFeeds>\n'+
' <enableHistory>false</enableHistory>\n'+
' <enableReports>false</enableReports>\n'+
' <enableSearch>false</enableSearch>\n'+
' <enableSharing>true</enableSharing>\n'+
' <enableStreamingApi>true</enableStreamingApi>\n'+
'{!fields}'+
' <label>{!label}</label>\n'+
' <nameField>\n'+
' <displayFormat>{0000000}</displayFormat>\n'+
' <label>{!label} Name</label>\n'+
' <type>AutoNumber</type>\n'+
' </nameField>\n'+
' <pluralLabel>{!labelPlural}</pluralLabel>\n'+
' <searchLayouts/>\n'+
' <sharingModel>ReadWrite</sharingModel>\n'+
' <visibility>Public</visibility>\n'+
'</CustomObject>';
return template
}