You need to sign in to do that
Don't have an account?
Linga_Ramin
How do I fetch all the Lookup fields of an object using Schema.DisplayType?
I have a requirement that I have to fetch all the Account Lookup fields.I'm using Schema.DisplayType for fetching Text,Picklistlist fields.But for lookup fields I don't know how to fetch.Can anyone help me?
For lookup fields please use the Reference keyword. Below is the sample code for your help:
Please let me know if you need any further help on this.
Thanks,
Abhishek Bansal.
All Answers
For lookup fields please use the Reference keyword. Below is the sample code for your help:
Please let me know if you need any further help on this.
Thanks,
Abhishek Bansal.
Try this
-I have done this in my org and it runs fine for me.
SObjectType objToken = Schema.getGlobalDescribe().get('Account');
DescribeSObjectResult objDef = objToken.getDescribe();
Map<String, SObjectField> fieldsObj = objDef.fields.getMap();
Set<String> fieldSet = fieldsObj.keySet();
DescribeFieldResult selectedField;
for(String field : fieldSet){
SObjectField fieldToken = fieldsObj.get(field);
selectedField = fieldToken.getDescribe();
if(selectedField.getType() == Schema.DisplayType.REFERENCE){
system.debug('field :: '+field);
}
}
if you need to describe all fields and wonder which ones are lookups try this:-
for(Schema.ChildRelationship cr : Account.SObjectType.getDescribe().getChildRelationships()){
System.debug(cr.getChildSObject() + '.' + cr.getField() + ' reversed is: ' + cr.getRelationshipName());
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha