I have a custom field: strField of String datatype. I also have Case c, which has fields of type Date, Integer, String, Picklist, currency, etc. For a use case, I need to compare strField with a Case c field which is fetched at run time based on some dynamic template. I cannot convert the Case c fields to String type because for numbers or Date or etc. , it will produce incorrect results. For example

// this is wrong
if(strField == String.valueOf(c.CloseDate)){
   // Do something

// this is also wrong
if(strField == String.valueOf(c.someIntegerField)){
   // Do something

Therefore, I need to cast the strField based on the fetched Case c field Datatype. I have the Case c field definition but I do not know how to convert strField to that respective datatype.

String datatype = t.Textbaustein__r.Feld3__r.DataType; // this returns 
datatype in string format
//I want to achieve this. The below code is wrong, it is just for reference: 

if((datatype)strField == c.get(t.Textbaustein__r.Feld3__r.QualifiedAPIName))
  // Do something

1 Answer 1


If you want to follow the path dynamically, you'll want to write a method for that:

public static Object getFieldValue(SObject record, String fieldPath) {
  String[] path = fieldPath.split('\\.'); // split is regex, need to escape
  SObject temp = record;
  while(path.size()>1 && temp != null) {
    temp = temp.getSObject(path.remove(0));
  return temp == null? null: temp.get(path[0]);

Now you can use your intended logic:

if(strField == getFieldValue(t, 'Textbaustein__r.Feld3__r.QualifiedAPIName')) {


If you happen to need to know the data type dynamically, you'll have to do that as some specialized logic. You'd need to know this if, for example, the strField is always a String...

Object value = getFieldValue(...);
if(value instanceOf Boolean && value == Boolean.valueOf(strField)) {
} else if(value instanceOf Date && value == Date.valueOf(strField)) {
} else if(value instanceOf DateTime && value == DateTime.valueOf(strField)) {
} else if(value instanceOf Time && value == Time.valueOf(strField)) {
} else if(value instanceOf Decimal && value == Decimal.valueOf(strField)) {
} else if(value instanceOf String && value == strField) {

Of course, make sure that the values are of the appropriate type. I've written some patterns for common types:

static Pattern 
    boolPat = Pattern.compile('^(true|false)$'),
    decPat = Pattern.compile('^[-+]?\\d+(\\.\\d+)?$'), 
    datePat = Pattern.compile('^\\d{4}.\\d{2}.\\d{2}$'), 
    dateTimePat = Pattern.compile('^\\d{4}.\\d{2}.\\d{2} (\\d{2}:\\d{2}:\\d{2} ([-+]\\d{2}:\\d{2})?)?$'),
    timePat = Pattern.compile('^\\d+:\\d{2}:\\d{2}$');

You can use Boolean.valueOf, Decimal.valueOf, Date.valueOf, DateTime.valueOf, and Time.valueOf if the patterns match (may require some tweaking). For example:

Object value = getFieldValue(...);
if(value instanceOf Boolean && boolPat.matcher(strField).find() && Boolean.valueOf(strField) == value) {
} ...

Unfortunately, because various objects aren't consistently implemented, we're stuck with a lot of redundant code that varies only by type. Fortunately, once implemented, you can reuse these methods whenever you need them.

  • Hi @sfdcfox, Thanks a lot for your reply. I do not know the fieldPath but yes, I need to know the fieldType Dynamically, so I will try the 'instance of' as you suggested. I was trying to avoid these checks and thought if this can be achieved by just picking the datatype of Case c field (in your example, Object value) rather checking against every datatype. But seems like i will have to check against every data type. Thanks again.
    – Dev Arya
    Dec 21, 2017 at 9:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .