Skip to content

Instantly share code, notes, and snippets.

@justin-lyon
Created June 16, 2022 11:58
Show Gist options
  • Save justin-lyon/7688380faddf4b7a89e66f8fb0e2c5bb to your computer and use it in GitHub Desktop.
Save justin-lyon/7688380faddf4b7a89e66f8fb0e2c5bb to your computer and use it in GitHub Desktop.
Apex Anon - Scan for External IDs
List<SObjectType> types = new List<SObjectType> {
Account.getSObjectType(),
Contact.getSObjectType(),
Opportunity.getSObjectType(),
Lead.getSObjectType()
};
List<Field> fields = new List<Field>();
for (SObjectType t : types) {
Schema.DescribeSObjectResult dsor = t.getDescribe();
Map<String, Schema.SObjectField> fieldMap = dsor.fields.getMap();
for (String key : fieldMap.keySet()) {
Schema.DescribeFieldResult dfr = fieldMap.get(key).getDescribe();
if (dfr.isExternalId()) {
fields.add(new Field(dsor.name, dfr.name, dfr.getType(), true));
}
}
}
System.debug(String.format('Found {0} External Ids', new List<Integer> { fields.size() }));
System.debug(fields);
class Field {
String sobjectName { get; set; }
String fieldName { get; set; }
DisplayType fieldType { get; set; }
Boolean isExternalId { get; set; }
Field(String obj, String f, Schema.DisplayType t, Boolean isExternalId) {
sobjectName = obj;
fieldName = f;
fieldType = t;
this.isExternalId = isExternalId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment