Skip to content

Instantly share code, notes, and snippets.

/LimitCases
Created Mar 2, 2015

Embed
What would you like to do?
trigger LimitCases on Case (before insert) {
//Extract the custom setting value into a variable
Max_Cases__c maxCase = Max_Cases__c.getInstance('maxcases');
Decimal maxNoCases = maxCase.MaxCases__c;
System.debug('The max no. of cases setting is:' + maxNoCases);
//1. Create a set of all Owner Id's whose cases are being fed for import
Map<Id,Integer> ownerCasesMap = new Map<Id,Integer>();
Set<String> ownerIds = new Set<String>();
for(Case newCase: Trigger.new) {
System.debug('Creating a set of Owner Ids');
ownerIds.add(newCase.OwnerId);
ownerCasesMap.put(newCase.OwnerId,0);
}
/*2. Create a user Map just to obtain the Owner Name using their Id
Add all userIds to a map with 0 number of cases as initial values
Salesforce doesn't allow doing Owner.name for some incomprehensible reason! */
Map<Id,User> userMap = new Map<Id,User>();
List<User> userM =[SELECT Id, Name FROM User WHERE Id IN: ownerIds];
for(User ownerUser:userM) {
userMap.put(ownerUser.Id, ownerUser);
}
//3. Use AggregateResults to get a count of all Cases pertaining to the above owners
System.debug('Creating a list of grouped results[AggregateResults] now');
List<AggregateResult> groupedResults= [SELECT OwnerId , count(Id) FROM Case
WHERE OwnerId IN:ownerIds
AND CreatedDate= THIS_MONTH
GROUP BY OwnerId];
//4. Use the previously created Map to store Owner Id and the number of Cases from above
for(AggregateResult countCase: groupedResults) {
System.debug('Entered the AggregateResult For loop');
if(countCase!=null) {
System.debug('Owner Id:' + countCase.get('OwnerId'));
System.debug('Count is' + countCase.get('expr0'));
//Map now has key-> Owner Id, value-> Case count
//This will replace the owners previous put in the Map with new values and not put anything for NULL
//This way users who didn't create any cases this month are retained in the previously created map
ownerCasesMap.put((Id)countCase.get('OwnerId'), (Integer)countCase.get('expr0'));
}
}
//Go through every Case being inserted , find the case count from above and compare to maxCases
for(Case newCase: Trigger.new) {
System.debug('Entered the main Case Trigger.new!');
//Get the number of cases associated with the Owner Id of this case
Integer numCases = ownerCasesMap.get(newCase.OwnerId);
System.debug('Number of Cases created so far by this owner:'+ numCases);
//Do not allow the user to create a case if the limit is reached
if(numCases>=maxNoCases) {
System.debug('Entered the if loop in order to show the error since numCases>=maxNoCases' );
newCase.addError('Too many cases created this month for user' + ' ' + userMap.get(newCase.OwnerId).Name + '(' + newCase.OwnerId + '):' + ' ' + maxNoCases);
}
else {
System.debug('Entered the else loop in order to let the user create the case' );
ownerCasesMap.put(newCase.OwnerId, ownerCasesMap.get(newCase.OwnerId) + 1);
System.debug('Number of cases for this owner has been incremented in the map' );
System.debug('Number of cases for this owner: ' + userMap.get(newCase.OwnerId).Name + 'is: ' +ownerCasesMap.get(newCase.OwnerId));
}
}
System.debug('Case has been inserted' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.