How to find External Id fields in an Salesforce Object?

How to find External Id fields in an Salesforce Object?

isExternalID() from the Schema.DescribeFieldResult can be used to find External Id fields in an Salesforce Object.

Sample Apex Code:

DescribeSObjectResult sObjectRes = 
	Account.sObjectType.getDescribe(); 
Map < String , Schema.SObjectField > mapFieldList = 
	sObjectRes.fields.getMap();    

for ( 
	Schema.SObjectField field : mapFieldList.values() 
) {    
	
	Schema.DescribeFieldResult fieldResult = 
		field.getDescribe();    
	
	if ( 
		fieldResult.isExternalID()
	) {   
		
		System.debug( 
			'Field Label is ' + 
			fieldResult.getLabel() 
		);  		
		System.debug( 
			'Field Name is ' + 
			fieldResult.getName()
		); 
		
	}  
	
}  

Output:

Leave a Reply