Posted By

d3developer on 07/28/10


Tagged


Versions (?)

@fractastical Field Details from fieldnames and object Name


 / Published in: Other
 

  1. public void getLabelsAndWidthsfromFields(List<String> fieldNames, String objectName)
  2. {
  3. labels = new List<String>();
  4. widths = new List<String>();
  5.  
  6. try {
  7. Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
  8. SObjectType sot = gd.get(objectName);
  9. Map<String, SObjectField> fields = sot.getDescribe().fields.getMap();
  10. List<SObjectField> fieldtokens = fields.values();
  11. for(String fn : fieldNames)
  12. {
  13. SObjectField field;
  14. List<String> fnParts = fn.split('[.]');
  15. if(fnParts.size() == 2)
  16. {
  17. String outerObjectName = fnParts.get(0);
  18. if(outerObjectName.endsWith('__r'))
  19. outerObjectName = outerObjectName.substring(0,outerObjectName.length() - 1) + 'c';
  20. SObjectType sotOuter = gd.get(outerObjectName);
  21. Map<String, SObjectField> outerFields = sotOuter.getDescribe().fields.getMap();
  22. field = outerFields.get(fnParts.get(1));
  23. }
  24. else
  25. field = fields.get(fn);
  26.  
  27. Schema.DescribeFieldResult fieldDescribe = field.getDescribe();
  28. labels.add(fieldDescribe.getLabel());
  29. widths.add(String.valueOf(fieldDescribe.getLength() * 3));
  30. }
  31. }
  32. catch (Exception e)
  33. {
  34. System.debug('UNABLE TO GENERATE LABELS:' + e);
  35. }
  36.  
  37. }
  38.  

Report this snippet  

You need to login to post a comment.