August 6, 2021

How to prevent certain profile users to download files in Salesforce?

Sample Code:
public class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {

    public Sfc.ContentDownloadHandler getContentDownloadHandler( List<ID> listIds, Sfc.ContentDownloadContext context ) {
    
        Sfc.ContentDownloadHandler contentDownloadHandler = new Sfc.ContentDownloadHandler();
        
        system.debug( 'Inside the Content Download' );     
        Id profileId = UserInfo.getProfileId();
        
        if( String.isNotBlank( profileId ) ) {
        
            Profile objProfile = [ SELECT Id, Name FROM Profile WHERE Id =: profileId ];
            
            if ( objProfile != null && objProfile.Name == 'System Administrator' ) {
            
                contentDownloadHandler.isDownloadAllowed = false;
                contentDownloadHandler.downloadErrorMessage = 'Downloading a file is not allowed for your profile';
                return contentDownloadHandler;
            
            }
        
        }
    
        contentDownloadHandler.isDownloadAllowed = true;
        return contentDownloadHandler;
    
    }

}
 
Sample Code:


No comments:

Post a Comment