+ Start a Discussion
ErikNelke1ErikNelke1 

apex trigger with and condition based on record count

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.

Thanks!

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 14];
            if(caselist.size()==14) {
                  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);   
            }
        
        }
    } 
Best Answer chosen by ErikNelke1
SarvaniSarvani
Hello ErikNelke,

I have modified the code which works only if caselist size is exactly 15.
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);   
            }
        
        }
    }
Hope this helps,

Thanks,
Sarvani.




 

All Answers

SarvaniSarvani
Hello ErikNelke,

I have modified the code which works only if caselist size is exactly 15.
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);   
            }
        
        }
    }
Hope this helps,

Thanks,
Sarvani.




 
This was selected as the best answer
ErikNelke1ErikNelke1
Thanks! it worked.