November 8, 2021

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

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() );
    }
    
}

No comments:

Post a Comment