I have in my SQL relationship fields. The have written this query under a custom object. ex:
SELECT name,Contact__r.Account.CreatedBy.Name FROM Four_Level_Check__c LIMIT 5
My need is to find the field type of the fields mentioned in the query. here the first field in the sql is "name", since it has no relationships attached to it, i can find the field type of "name" directly from the base object. But the second field "Contact__r.Account.CreatedBy.Name" contains a relationship schema from the custom object-->Account-->owner--> .So to find the field type of "Contact__r.Account.CreatedBy.Name" I have written a logic
String objectType;
if(objectt.contains('__r'))
{
objectType=objectt.replace('__r','');
}
else if(objectt.equals('LastModifiedBy'))
{
objectType='User';
}
else if(objectt.equals('CreatedBy'))
{
objectType='User';
}
else if(objectt.equals('Owner'))
{
objectType='User';
}
else objectType=objectt;
String FormatVal;
String lookupField=Field;
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(objectType);
Map<String, Schema.SObjectField> M= leadSchema.getDescribe().fields.getMap();
Schema.SObjectField field1 = M.get(fieldVal);
System.debug('###field1'+field1);
For my scenario this code is working fine. But will this code work for all scenarios?