• Salesforce BuzzLogic
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
  • User Deatails should be passed
  • Count of records should not exceed
  • user must create only 100 records not more than that per week.
  • Created date is this_week
  • Restrict the user from creating more than 100 records per week
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.');
    }
}
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.');
    }
}
Hi All
Here is my trigger to limit a user to create only 10 cases in a month,it is half working accordingly, but i would like to put the logic in the apex class which is currently in the trigger, more over my trigger is restricting the users to create cases when they exceeded specified limit but i am seeing the first line of error 'Invalid Date' which is unexpected along with the custom error message that i have written in the trigger.
User-added image
 
Trigger LimitNoOfCases on case(Before insert){
    If (Trigger.IsInsert){
    Integer MonthNumber = Date.Today().Month();
    Integer YearNumber = Date.Today().Year();
    Integer MaxCases = Integer.valueof(System.Label.MaxCasesLimit);//configured maximum number of cases per user by creating Custom label.
    Id Xuser = UserInfo.Getuserid();
    User Usr = [select name from user where id=:userinfo.getuserid()];
    List<Case> LstCase = [select id,CreatedById,CreatedDate from case where CALENDAR_YEAR(CreatedDate) =:YearNumber and CALENDAR_MONTH(CreatedDate)=:monthnumber and CreatedById=:UserInfo.GetUserId()];
 
    If (Lstcase.Size()>=MaxCases)         
   {   
      Trigger.New[0].addError('Dear user you have exceeded the maximum number of cases allowed this month.');
   }  
     
  }
}

 
  • June 15, 2016
  • Like
  • 0