How to find all Unique property enabled fields within an object in Salesforce?

How to find all Unique property enabled fields within an object in Salesforce?

Using Schema.getGlobalDescribe() and Schema.SObjectField, we can find all the Unique property enabled fields within an object in Salesforce.

Use the following sample code to find all the unique property enabled fields in the Account object/entity.

Sample Code:

SObjectType objType = Schema.getGlobalDescribe().get( 
    'Account' 
);
Map < String, Schema.SObjectField > mapFields 
    = objType.getDescribe().fields.getMap();

for ( 
    String strField : mapFields.keySet() 
) {
    
    SObjectField field = mapFields.get( 
        strField 
    );
    Schema.DescribeFieldResult fieldResult = field.getDescribe();
    
    if ( fieldResult.isUnique() ) {
        
        system.debug( 
            fieldResult.getLabel() 
        );
        
    }
    
}

Leave a Reply