Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Created September 28, 2018 12:01
Show Gist options
  • Save swapnilshrikhande/cfe188d92c5f6d3979a107bd19309d5c to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/cfe188d92c5f6d3979a107bd19309d5c to your computer and use it in GitHub Desktop.
Get Field List By Type
public Map<String,String> handleDateTypes(Map<String,String> recordMap,Type classType){
List<String> dateTimeFields = getFieldsOfType(
classType
, Schema.SOAPType.DATE
);
for( String fieldKey : recordMap.keySet() ){
if( dateTimeFields.contains(fieldKey.toLowerCase()) ){
String dateValue = recordMap.get(fieldKey);
if( String.isBlank(dateValue) || 'null'.equalsIgnoreCase(dateValue) ){
recordMap.put(fieldKey,null);
} else {
//create a date instance out of the milliseconds
Date dateInstance = Date.parse(dateValue);
//recordMap.put(fieldKey,dateInstance.toString());
}
}
}
return recordMap;
}
//list of all the fields of a particular type
public static List<String> getAllFields(Type classType){
Set<Schema.SOAPType> soapTypeSet = null;
return getFieldsOfType(classType,soapTypeSet);
}
public static List<String> getFieldsOfType(Type classType,Schema.SOAPType soapType){
return getFieldsOfType(classType,new Set<Schema.SOAPType>{soapType});
}
public static List<String> getFieldsOfType(Type classType,Set<Schema.SOAPType> soapTypeSet){
Map<String, Schema.SObjectField> fieldMap;
Schema.DescribeFieldResult fieldDescribeResult;
List<String> fieldList = new List<String>();
Schema.SObjectType sObjectType = getSObjectType(classType.toString());
if( sObjectType != null ) {
fieldMap = sObjectType.getDescribe().fields.getMap();
for(String fieldName : fieldMap.keySet()){
fieldDescribeResult = fieldMap.get(fieldName).getDescribe();
if( soapTypeSet == null) {
fieldList.add(fieldName);
} else if( soapTypeSet.contains(fieldDescribeResult.getSOAPType()) ){
fieldList.add(fieldName);
}
}
}
return fieldList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment