Play Games

Search This Blog

Showing posts with label How to get Unique Id fields of an object in Apex. Show all posts
Showing posts with label How to get Unique Id fields of an object in Apex. Show all posts

Friday, March 9, 2018

How to get Unique Id fields of an object in Apex

Solution: We can check if a field is unique field by using isUnique() of Schema.DescribeFieldResult.

Sample Apex Code:
In this example We have a custom field 'Unique__c' field created on lead object which is made unique by clicking unique Id checkbox.

List<String> lstUniqueField = new List<String>();
Map<String, Schema.SObjectField> describeFields = Schema.SObjectType.Lead.fields.getMap();
for(String field : describeFields.keyset()){
Schema.DescribeFieldResult desribeResult = describeFields.get(field).getDescribe();
if(desribeResult.isUnique()) {
lstUniqueField.add(field);
}
}
System.debug('Unique fields of Lead Object'+lstUniqueField );

Output: