+ Start a Discussion
PeacePeace 

List of field names which are external id

Hi,

 

I am new to Salesforce. Please help me in implementing following

 

In my Apex code, i have name of selected object in a Variable
I need a piece of code which returns name of the fields which are external ids in that selected object.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Sonali BhardwajSonali Bhardwaj
String typeName = 'Account'; Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName); Map<String, Schema.SObjectField> M = targetType.getDescribe().fields.getMap(); Set<String> externalIdFields = new Set<String>(); for (String k : M.keySet()) { if(M.get(k).getDescribe().isExternalID()) { externalIdFields.add(k); } } 

 typeName must have your object name, and in externalIdFields you will get all external ids for the given object.

All Answers

Sonali BhardwajSonali Bhardwaj
String typeName = 'Account'; Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName); Map<String, Schema.SObjectField> M = targetType.getDescribe().fields.getMap(); Set<String> externalIdFields = new Set<String>(); for (String k : M.keySet()) { if(M.get(k).getDescribe().isExternalID()) { externalIdFields.add(k); } } 

 typeName must have your object name, and in externalIdFields you will get all external ids for the given object.

This was selected as the best answer
PeacePeace

Thanks Sonali