Created
May 24, 2011 21:38
-
-
Save fractastical/989771 to your computer and use it in GitHub Desktop.
@fractastical Field Details from fieldnames and object Name
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
public void getLabelsAndWidthsfromFields(List<String> fieldNames, String objectName) | |
{ | |
labels = new List<String>(); | |
widths = new List<String>(); | |
try { | |
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); | |
SObjectType sot = gd.get(objectName); | |
Map<String, SObjectField> fields = sot.getDescribe().fields.getMap(); | |
List<SObjectField> fieldtokens = fields.values(); | |
for(String fn : fieldNames) | |
{ | |
SObjectField field; | |
List<String> fnParts = fn.split('[.]'); | |
if(fnParts.size() == 2) | |
{ | |
String outerObjectName = fnParts.get(0); | |
if(outerObjectName.endsWith('__r')) | |
outerObjectName = outerObjectName.substring(0,outerObjectName.length() - 1) + 'c'; | |
SObjectType sotOuter = gd.get(outerObjectName); | |
Map<String, SObjectField> outerFields = sotOuter.getDescribe().fields.getMap(); | |
field = outerFields.get(fnParts.get(1)); | |
} | |
else | |
field = fields.get(fn); | |
Schema.DescribeFieldResult fieldDescribe = field.getDescribe(); | |
labels.add(fieldDescribe.getLabel()); | |
widths.add(String.valueOf(fieldDescribe.getLength() * 3)); | |
} | |
} | |
catch (Exception e) | |
{ | |
System.debug('UNABLE TO GENERATE LABELS:' + e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment