+ Start a Discussion

Dynamic APEX with a describe call workaround

Take a look at the following code snippet and tell me what you think, is this a bad idea. I could use describe calls but I might hit the 100 describe call limit. I don't have anyone to bounce ideas off of at the office so I thought some of you would have some good input.


obj is a sobject from a dynamic soql call.
fieldName is a string that can be the api name of a field on
obj or the api name of a field on a parent sobject of obj.
(exp. obj could be a Contact record and field name could be
something like: 'Account.Name' or 'myParent__r.myGrandparent__r.myField__c')
private String getFieldValue(Sobject obj, String fieldName) { Object fieldValue; if(fieldName.contains('.')) { for(String s : fieldName.split('\\.')) { try { obj = obj.getSObject(s); } catch(system.SObjectException e) { fieldValue = obj.get(s); } } } else { fieldValue = obj.get(fieldName); } if(fieldValue != null) { if(fieldValue instanceof Datetime) { return getFormatedDatetime( (Datetime) fieldValue); } else if(fieldValue instanceof String) { return (String) fieldValue; } else if ... { ... // and so on } } return null; }