Skip to content

Instantly share code, notes, and snippets.

@pcon
Created November 11, 2014 14:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcon/8afe91eee8fde2d0fd15 to your computer and use it in GitHub Desktop.
Save pcon/8afe91eee8fde2d0fd15 to your computer and use it in GitHub Desktop.
Trigger code to make it so that a User can only create X number of cases per month
trigger MaxCases on Case (before insert) {
Integer maxCases = null;
CaseSettings__c settings = CaseSettings__c.getValues('default');
if (settings != null) {
maxCases = Integer.valueOf(settings.MaxCases__c);
}
if (maxCases != null) {
Set<Id> userIds = new Set<Id>();
Map<Id, Integer> caseCountMap = new Map<Id, Integer>();
for (Case c: trigger.new) {
userIds.add(c.OwnerId);
caseCountMap.put(c.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 Case
where CreatedDate = THIS_MONTH and
OwnerId in :userIds
group by OwnerId
]) {
caseCountMap.put((Id) result.get('OwnerId'), (Integer) result.get('expr0'));
}
for (Case c: trigger.new) {
caseCountMap.put(c.OwnerId, caseCountMap.get(c.OwnerId) + 1);
if (caseCountMap.get(c.OwnerId) > maxCases) {
c.addError('Too many cases created this month for user ' + userMap.get(c.OwnerId).Name + '(' + c.OwnerId + '): ' + maxCases);
}
}
}
}
@miragedeb
Copy link

Hey Patrick,
I see a problem with your code and I might be wrong so please correct me. In line 35 and 36, why are you incrementing the number of cases before checking for the >maxCases condition?

From what I see in your code (as a problem), the number of cases in caseCountMap will always be incremented as soon as you enter the Trigger.new loop after line 35. Shouldn't you put that in an else condition like this?

for (Case c: trigger.new) {

        if (caseCountMap.get(c.OwnerId) > maxCases) {
            c.addError('Too many cases created this month for user ' + userMap.get(c.OwnerId).Name + '(' + c.OwnerId + '): ' + maxCases);
        }

else {
caseCountMap.put(c.OwnerId, caseCountMap.get(c.OwnerId) + 1);
}

@pcon
Copy link
Author

pcon commented Oct 17, 2015

Sorry @miragedeb I just now saw your comment (weird)... The reason I'm incrementing before is because if the current case pushes over the max number of cases then i want it to error. Otherwise I'd be allowed to add maxCases + 1 The else statement you have would make sense if i was checking if it was equal to maxCases

@Ramu06
Copy link

Ramu06 commented Jul 25, 2016

what difference between with sharing and without sharing and why we should use in apex class .plz help me

@Ramu06
Copy link

Ramu06 commented Jul 25, 2016

i want above trigger on custom object

@cleverlemming
Copy link

Was it necessary to initiate caseCountMap on line 15 with value of 0 when you replace all of the values in caseCountMap on line 32 using the same set of UserID's?

@gurditta
Copy link

This can be very simple by just one query:

trigger MaxCases on Case (before insert) {
    Integer maxCases = null;
    CaseSettings__c settings = CaseSettings__c.getValues('default');
    
    if (settings != null) {
        maxCases = Integer.valueOf(settings.MaxCases__c);
    }
    Map<Id,AggregateResult> caseCountMap = new Map<id,AggregateResult>([select count(Id), OwnerId Id, Owner.Name from Case where CreatedDate = THIS_MONTH group by OwnerId,Owner.Name having count(id) >: maxCases]);
    //{00Gg0000001aNhLEAU=AggregateResult:{expr0=7, Id=00Gg0000001aNhLEAU, Name=Test}}
    for (Case c: trigger.new) {
        AggregateResult ar = caseCountMap.get(c.OwnerId);
        if (ar != null) {
            c.addError('Too many cases created this month for user ' + ar.get('Name') + '(' + c.OwnerId + '): ' + maxCases);
        }
    }
}

@ssssm83
Copy link

ssssm83 commented Mar 12, 2019

Hi,

I just want to know what values you have given in default in custom settings?
CaseSettings__c settings = CaseSettings__c.getValues('default');
It shows null values for me.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment