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
Salesforce BuzzLogicSalesforce BuzzLogic 

I want to restrict User from creating 50 records per week on account object

I want to restrict User from  creating 50 records per week on account object .Here is my code.I am missing the logic.

public class RestrictAccountByUser {
    public static void limitNoOfAccounts(List<Account> AccList) {
        Integer maxAccounts = 50;
        Set<Id> UserId =New Set<Id>();
          Map<Id,User> UserIds =new  Map<Id,User>([Select id from User ]);
        List<Account> thisWeekAccountList = [select id,CreatedById,CreatedDate from Account where CreatedDate = THIS_WEEK and CreatedById=:UserInfo.GetUserId()];
 Integer AccountListCount = [Select count() from Account where UserId=:UserIds'];
      
        if (thisWeekAccountList.Size()>maxAccounts)         
            AccList[0].addError('Dear user you have exceeded the maximum number of Accounts allowed this Week.');
    }
}
AnkaiahAnkaiah (Salesforce Developers) 
Hi ,

I try with below code. its working for me.
public class RestrictAccountByUser {
    public static void limitNoOfAccounts(List<Account> AccountList) {
        Integer maxAccounts = 50;
        List<Account> thisweekAccountList = [select id,CreatedById,CreatedDate from Account 
		                                      where CreatedDate=THIS_WEEK
											  AND CreatedById=:UserInfo.GetUserId()];
        
        if (thisweekAccountList.Size()>=maxAccounts)         
            AccountList[0].addError('Dear user you have exceeded the maximum number of Accounts allowed to create this week.');
    }
}
If this helps please mark it as best answer.

Thanks!!
 
Salesforce BuzzLogicSalesforce BuzzLogic
May I know where you are restricting the user not to create more than 50 records per week