You need to sign in to do that
Don't have an account?

Trigger help to limit Number of cases to be created in a month per user
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.

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.
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.'); } } }
This is the trigger I have written according to bes practise,
And this is the trigger handler class,
Logic is almost same. I have changed some variable names according to best practise. Please save the trigger handler class before saving the trigger.
Thanks & Regards,
Prosenjit
All Answers
First line of error is actually 'Invalid Data' not 'Invalid Date'. I think this is the style of trigger by which we can see errors and your code is fine. If you want to write any trigger handlers then I can help .
Thanks and Regards,
Prosenjit
Thanks
CKR
This is the trigger I have written according to bes practise,
And this is the trigger handler class,
Logic is almost same. I have changed some variable names according to best practise. Please save the trigger handler class before saving the trigger.
Thanks & Regards,
Prosenjit
I have a Query.Where I should Restrict the user Not to create more than 200 records per week Using Trigger handler.
trigger RestrictUser on Account (before insert) {
Integer maxAccounts = 50;
if (maxAccounts != null) {
Set<Id> userIds = new Set<Id>();
Map<Id, Integer> accCountMap = new Map<Id, Integer>();
for (Account acc: trigger.new) {
userIds.add(acc.OwnerId);
accCountMap.put(acc.OwnerId,0);
}
Map<Id, User> userMap = new Map<Id, User>([select Name from User where Id in :userIds]);
for (AggregateResult result: [select count(Id),OwnerId from Account where CreatedDate = THIS_WEEK and OwnerId in :userIds group by OwnerId]) {
accCountMap.put((Id) result.get('OwnerId'), (Integer) result.get('expr0'));
}
for (Account acc: trigger.new) {
accCountMap.put(acc.OwnerId, accCountMap.get(acc.OwnerId) + 1);
if (accCountMap.get(acc.OwnerId) > maxAccounts) {
acc.addError('Too many Accounts created this month for user ' + userMap.get(acc.OwnerId).Name + '(' + acc.OwnerId + '): ' + maxAccounts);
}
}
}
}
IF You Find My answer as Best Answer Please Like it as a best answer