Last active
October 8, 2018 19:13
-
-
Save martyychang/5ba8d84edcf4d1ee9aae42521c3fce60 to your computer and use it in GitHub Desktop.
Generate a list of all fields and data types for a given object and send the metadata in CSV format via email
This file contains hidden or 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
// README: This is the only line you need to edit (generally speaking) | |
// This is the object for which I want to extract the field describes. | |
// Running this script will send the resulting field list to your user's email, | |
// with the fields as an attached CSV file. | |
Schema.DescribeSObjectResult objectDescribe = Schema.SObjectType.OpportunityLineItem; | |
/** | |
* Wrapper class to return CSV rows | |
*/ | |
public class FieldCsvWriter { | |
private Schema.DescribeFieldResult fieldDescribe { get; set; } | |
private Schema.DescribeSObjectResult objectDescribe { get; set; } | |
public FieldCsvWriter( | |
Schema.DescribeFieldResult fieldDescribe, | |
Schema.DescribeSObjectResult objectDescribe | |
) { | |
this.fieldDescribe = fieldDescribe; | |
this.objectDescribe = objectDescribe; | |
} | |
public String escape(String value) { | |
return '"' + value + '"'; | |
} | |
public List<String> getCsvRow() { | |
// Construct the list | |
List<String> row = new List<String>(); | |
row.add(this.escape(this.getRowId())); | |
row.add(this.escape(getName())); | |
row.add(this.escape(this.getObjectUniqueName())); | |
row.add(this.escape(this.getLabel())); | |
row.add(this.escape(this.getDataType())); | |
// Return the row | |
return row; | |
} | |
private String getDataType() { | |
String dataType = '#UNKNOWN'; | |
// Get Display Type for comparison | |
Schema.DisplayType fieldDisplayType = this.fieldDescribe.getType(); | |
if (fieldDisplayType == Schema.DisplayType.ADDRESS) { | |
dataType = 'Address'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.BOOLEAN) { | |
dataType = 'Checkbox'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.CURRENCY) { | |
dataType = 'Currency('; | |
dataType += this.fieldDescribe.getPrecision(); | |
dataType += ', '; | |
dataType += this.fieldDescribe.getScale(); | |
dataType += ')'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.DATE) { | |
dataType = 'Date'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.DATETIME) { | |
dataType = 'Date/Time'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.DOUBLE) { | |
dataType = 'Number('; | |
dataType += this.fieldDescribe.getPrecision(); | |
dataType += ', '; | |
dataType += this.fieldDescribe.getScale(); | |
dataType += ')'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.EMAIL) { | |
dataType = 'Email'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.ID) { | |
dataType = 'Lookup(' + objectDescribe.getLabel() + ')'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.INTEGER) { | |
dataType = 'Number('; | |
dataType += this.fieldDescribe.getPrecision(); | |
dataType += ', '; | |
dataType += 0; | |
dataType += ')'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.PHONE) { | |
dataType = 'Phone'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.PERCENT) { | |
dataType = 'Percent('; | |
dataType += this.fieldDescribe.getPrecision(); | |
dataType += ', '; | |
dataType += this.fieldDescribe.getScale(); | |
dataType += ')'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.PICKLIST) { | |
dataType = 'Picklist'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.REFERENCE) { | |
// TODO: Support polymorphic fields | |
dataType = 'Lookup(' + this.fieldDescribe.getReferenceTo()[0].getDescribe().getLabel() + ')'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.STRING) { | |
dataType = 'Text(' + this.fieldDescribe.getLength() + ')'; | |
} | |
else if (fieldDisplayType == Schema.DisplayType.TEXTAREA) { | |
dataType = 'Text Area'; | |
// Check to see whether it's actually a Long Text Area | |
if (this.fieldDescribe.getLength() > 255) { | |
dataType = 'Long Text Area(' + this.fieldDescribe.getLength() + ')'; | |
} | |
} | |
else if (fieldDisplayType == Schema.DisplayType.URL) { | |
dataType = 'URL(' + this.fieldDescribe.getLength() + ')'; | |
} | |
else { | |
dataType += '|' + fieldDisplayType.name(); | |
} | |
// Return the data type | |
return dataType; | |
} | |
private String getLabel() { | |
return fieldDescribe.getLabel(); | |
} | |
private String getName() { | |
return fieldDescribe.getName(); | |
} | |
private String getObjectUniqueName() { | |
return 'sfdc.' + objectDescribe.getName(); | |
} | |
private String getRowId() { | |
return ''; | |
} | |
} | |
/** | |
* Writer to produce CSV text | |
*/ | |
public class CsvWriter { | |
private String fieldDelimiter { get; set; } | |
private String recordDelimiter { get; set; } | |
private List<List<String>> rows { get; set; } | |
public CsvWriter(List<List<String>> rows) { | |
this.rows = rows; | |
this.fieldDelimiter = ','; | |
this.recordDelimiter = '\n'; | |
} | |
public String getPlainText() { | |
// Collapse the nested list into a single list | |
List<String> rowTexts = new List<String>(); | |
for (List<String> eachRow : this.rows) { | |
rowTexts.add(String.join(eachRow, this.fieldDelimiter)); | |
} | |
// Return the joined rows | |
return String.join(rowTexts, this.recordDelimiter); | |
} | |
} | |
// Get a list of all the fields | |
List<List<String>> csvRows = new List<List<String>>(); | |
// Compile the list into CSV rows, | |
// adding each row into the master list to send | |
Map<String, Schema.SObjectField> fieldMap = objectDescribe.fields.getMap(); | |
for (Schema.SObjectField eachField : fieldMap.values()) { | |
// Construct the field writer | |
FieldCsvWriter writer = new FieldCsvWriter( | |
eachField.getDescribe(), | |
objectDescribe | |
); | |
// Dump the field info to a list of text | |
csvRows.add(writer.getCsvRow()); | |
} | |
// Construct the email | |
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); | |
mail.setSubject(objectDescribe.getName() + ' fields'); | |
mail.setPlainTextBody('Attached as CSV'); | |
Messaging.EmailFileAttachment csvFile = | |
new Messaging.EmailFileAttachment(); | |
csvFile.filename = objectDescribe.getName() + '.csv'; | |
csvFile.contenttype = 'text/csv'; | |
csvFile.body = Blob.valueOf(new CsvWriter(csvRows).getPlainText()); | |
// Attach the CSV file | |
mail.setFileAttachments(new List<Messaging.EmailFileAttachment> { csvFile }); | |
// Send the email to myself | |
mail.setTargetObjectId(UserInfo.getUserId()); | |
mail.setSaveAsActivity(false); | |
Messaging.sendEmail(new List<Messaging.Email> { mail }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment