+ Start a Discussion
ErikNelke1ErikNelke1 

apex trigger with and condition based on record count in a queue

Hi,

I need some help to adjust this trigger so that it only fires off when the record count is > 14 and <  16. The user group only wants to be notified the first time the queue hits 15 cases so this will limit the notifications. I'm not sure how to add the range logic to the code.

This is the code we have but the trigger is firing each time new case hits the queue and is not respecting the limits we tried.

Any ideas or help on why this happens and how to resolve it would be appreciated.

trigger Notificationexceedingqueuelimit on Case (after insert,after update) {
    list<group> queuelist= [SELECT id  FROM Group where DeveloperName = 'CPS_Default_Queue'and Type = 'Queue' limit 1]; 
    
    
    
    if(queuelist.size()>0){
        boolean isqueuecase = false;
        for(case cs : trigger.new){
            if(cs.ownerid == queuelist[0].id){
                isqueuecase = true;
            }
        }
        if(isqueuecase = true){
            list<case> caselist = [select id from case where ownerid =:queuelist[0].id limit 16];
            if(caselist.size()>14 && caselist.size()<16 ){
                  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
                  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                  List<String> sendTo = new List<String>();
                  sendTo.add('Mymail.com' );
                  mail.setHtmlBody('Hi, <br/> Default Queue Limit Exceeded. The count has exceeded 15 cases. <br/><br/> Thanks, <br/>Salesforce Admin');
                  mail.setSubject('Case Limit Exceeded');
                  mail.setToAddresses(sendTo);
                  mails.add(mail);   
                  Messaging.sendEmail(mails);   
            }
        
        }
    }
Avishek Nanda 14Avishek Nanda 14
Hi Erik,

If i understand you correctly the notification has to be send first time when the queue limit hits as per the above creieria per the given date or date. 
In this case you could create a custom setting. The Custom Setting Would contan three fields. Name(Text), Date(Date or Date and time) & Notification Sent (CheckBox). Name would be the Queue name.. You could implement the same if you have to have the same logic for multiple queue. When you queue hits the limit for the first time update the custom setting data to True and Date of Today and in trigger you can check for the date and Boolean value for the given date to send another notification. 

Let me know your thoughts ?

Regards,
Avishek