function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aditya Pavan lakshmiAditya Pavan lakshmi 

I want to restrict the user for creating a Lead.How can I achieve this ?

Hi All,

I want to ristrict the user on creating a Lead?How can I achieve this ?

The scenario will be like this a user can create only 15 leads.after that an error message will display that you reached the max level.
JyothsnaJyothsna (Salesforce Developers) 
Hi,

you can go to the users profile and remove the Create permission for Leads under object settings.

Go to Setup -> Manage Users -> Profiles
Select Your Users Profile
Click on Object Settings
Click on Lead
Click on Edit
Under Object Permissions section, disable Create.

Hope this helps you!
Best Regards,
Jyothsna
 
Mahesh DMahesh D
Hi Aditya,

Please find the below code:
 
trigger LeadTrigger on Lead (before insert) {
    if(Trigger.isBefore) {
		Integer exisLeadCount = [Select count() from Lead where CreatedById =: UserInfo.getUserId()];
		
		for(Lead l: Trigger.new) {
			exisLeadCount++;
			if(exisLeadCount > 15) {
				l.adderror('Number of Leads Limit is crossed.');
			}
		}
    }   
}

I already tested it in my DE org and it looks good.

Regards,
Mahesh


 
Naval Sharma4Naval Sharma4
Hi Aditya,

You can achieve this using trigger. Try the below code.
Trigger leadTrigger on Lead(after insert){
    Set<Id> createdByIds = new Set<Id>();
    for(Lead lead : Trigger.new){
        createdByIds.add(lead.createdBy);   
    }
    Map<String, Integer> createdLeadByUsers = new Map<String, Integer>();
    for(AggregateResult ar : [SELECT COUNT(Id) totalLead, createdById CreatedBy FROM Lead WHERE CreatedById in : createdByIds 
    GROUP BY CreatedById HAVING COUNT(Id) >= 15
     ORDER BY COUNT(Id) DESC]){
         createdLeadByUsers.put(String.valueOf(ar.get('CreatedBy')), Integer.valueOf(ar.get('totalLead')));
    }
    for(Lead lead : Trigger.new){
        if(createdLeadByUsers.containsKey(lead.createdBy))
            lead.addError('You have reached the max limit');  
    }
}

 
Mahesh DMahesh D
Hi Naval,

I have a question here,

The number of Leads created by user is 14 and if the same user is trying to create 10 leads at a time. What will happen to the trigger code.

Please do let me know if my thinking is wrong.

Regards,
Mahesh