+ Start a Discussion
Matheus AntonioMatheus Antonio 

How to prevent account creation by using profile and recordtype?

Hello everyone!

I've been thinking about how to prevent account creation by using profile and recordtype. In the object Account I have two recordtypes: "Example 1" and "Example 2". In my ORG there are 2 profiles such as "System Administrator" and also "Customer Service" .What I need to do is basically:

-  If I'm logged in with profile  "System Administrator" or "Customer Service" I'm allowed to create an account "Example 1" or "Example 2"(recordtype) on Salesforce. 

Thank you guys in advance. Have a good one. 
Best Answer chosen by Matheus Antonio
Hemant_SoniHemant_Soni
Hi Matheus, 
Please Try Below Trigger to Prevent
// Trigger On Account Object
trigger AccountTrigger on Account (before insert) {
    AccountTriggerHandler.onBeforeInsert(trigger.new);
}

// Trigger Handler 
public class AccountTriggerHandler {
    public static void onBeforeInsert(List<Account> newList){
      preventAccountCreation(newList);  
    }
    
    public static void preventAccountCreation(List<Account> newList){
        Set<Id> setValidRecordTypeId = new Set<Id>();
        setValidRecordTypeId.add(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Company').getRecordTypeId());
        setValidRecordTypeId.add(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Institute').getRecordTypeId());// Add Record Type Here
        Set<String> setValidProfileId = new Set<String>();
        for(Profile oProfile : [SELECT Id, Name FROM Profile Where Name IN ('System Admin Custom', 'System Administrator')]){//Add Profile Name
            setValidProfileId.add(oProfile.Id);   
        }
        if(setValidProfileId.size() > 0 && setValidRecordTypeId.size() > 0){
            for(Account oAccount : newList){
                system.debug('################## oAccount : '+oAccount.RecordTypeId);
                if(!setValidProfileId.contains(UserInfo.getProfileId()) || !setValidRecordTypeId.contains(oAccount.RecordTypeId)){
                    oAccount.addError('You are authorized to create Account'); // Add Error Message
                }
            } 
        }
    }
}

Please Update Code According to your recordType Name name and profile name.
Please let me know if you face any issue. You can email me on "sonihemant.jaipur@gmail.com". 

Thanks
Hemant

All Answers

Hemant_SoniHemant_Soni
Hi Matheus, 
Please Try Below Trigger to Prevent
// Trigger On Account Object
trigger AccountTrigger on Account (before insert) {
    AccountTriggerHandler.onBeforeInsert(trigger.new);
}

// Trigger Handler 
public class AccountTriggerHandler {
    public static void onBeforeInsert(List<Account> newList){
      preventAccountCreation(newList);  
    }
    
    public static void preventAccountCreation(List<Account> newList){
        Set<Id> setValidRecordTypeId = new Set<Id>();
        setValidRecordTypeId.add(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Company').getRecordTypeId());
        setValidRecordTypeId.add(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Institute').getRecordTypeId());// Add Record Type Here
        Set<String> setValidProfileId = new Set<String>();
        for(Profile oProfile : [SELECT Id, Name FROM Profile Where Name IN ('System Admin Custom', 'System Administrator')]){//Add Profile Name
            setValidProfileId.add(oProfile.Id);   
        }
        if(setValidProfileId.size() > 0 && setValidRecordTypeId.size() > 0){
            for(Account oAccount : newList){
                system.debug('################## oAccount : '+oAccount.RecordTypeId);
                if(!setValidProfileId.contains(UserInfo.getProfileId()) || !setValidRecordTypeId.contains(oAccount.RecordTypeId)){
                    oAccount.addError('You are authorized to create Account'); // Add Error Message
                }
            } 
        }
    }
}

Please Update Code According to your recordType Name name and profile name.
Please let me know if you face any issue. You can email me on "sonihemant.jaipur@gmail.com". 

Thanks
Hemant
This was selected as the best answer
Gabriel C FerreiraGabriel C Ferreira
Hi Matheus,

You can make this in each profile, defining the record type each profile have access.

Go to Setup -> Profiles -> Your Profile.
Go to Record Type Settings Section and click Edit next to "Account"

User-added image
Change the Record Type between Available and Select columns, according with the profile permissions.

If it helped solve your solution, please mark as best answer.

Best Regards
Matheus AntonioMatheus Antonio
You guys helped me a lot! Thank you so much. Both solutions worked!