Created
June 16, 2022 11:58
-
-
Save justin-lyon/7688380faddf4b7a89e66f8fb0e2c5bb to your computer and use it in GitHub Desktop.
Apex Anon - Scan for External IDs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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