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:
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:
No comments:
Post a Comment