Your code will add name to the soql statement if any field on the object contains the string "name." Case has a field called "CreatorName" and another called "SuppliedName," which will both pass the if
statement. In your case, I'd recommend adding a method like this:
public static boolean setContainsIgnoreCase(Set<String> toSearch, String query)
{
for (String i in toSearch)
{
if (i.equalsIgnoreCase(query))
{
return true;
}
}
return false;
}
And use this instead of joining your set into a string.
Since the field names aren't actually being put in by the user, you could also just use contains
to check for them with lowercase names, like they are listed in the describe.