Created
September 28, 2018 12:01
-
-
Save swapnilshrikhande/83fd593da3031b2c9b529122cc49d2fb to your computer and use it in GitHub Desktop.
Get Field List By Type
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 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