+ Start a Discussion
reema_agarwalreema_agarwal 

batch Apex to escalate cases

I have fields like case status and date/time opened in the cases object.

If the date/time opened is more then one hour and still the status is new and the status is not approved or rejected the case should get escalated.

On escalation the status should change to escalated and the user should get a mail of escalated case assignment.

 

I am new to apex can someone please help me to write a batch apex for this.

 

Thanks!

AmitSahuAmitSahu

Try escalation rules.

reema_agarwalreema_agarwal

I tried using escalation rules but that is not working.

 

Now i am trying to use batch apex for the same so that i can use apex scheduler i have written the apex class as follows


global class CaseEscalationRules implements Database.Batchable<sObject> {

          global Database.QueryLocator start(Database.BatchableContext BC){

                             return Database.getQueryLocator([SELECT CaseNumber, CreatedDate,Status

                                                 FROM Case ]);

                        }

 

          global void execute(Database.BatchableContext ctx, List<Sobject>scope){

                           List <Case> ca = (List<Case>)scope;

                          for(Integer i = 0; i < ca.size(); i++){

                          if(System.Today() - ca.CreatedDate >=1 && ca.Status!='Closed' )

                          {

                                   String emailMessage = 'The case '

                                            + Case.CaseNumber

                                           + ' has been ecalated '

                                           + ' Please look into the case '

                                           + 'Thankyou';

                                     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

                                 String[] toAddresses = new String[] {'reems.agarwal3@gmail.com'};

                                 mail.setToAddresses(toAddresses);

                                 mail.setSubject('Case Escalation');

                                 mail.setPlainTextBody(emailMessage);

                                 Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ mail });

                                 Case.Status = 'Escalated';

                               }


                          }
            update ca;
          }

 

          global void finish(Database.BatchableContext ctx){

                   }

  }

 

for this code i am getting an error: Error: Compile Error: Expression cannot be assigned at line -1 column -1 

 

Also can someone please help me with the 'If condition' where i need to check whether the CreatedDate of a case is more than one hour .

 

Thanks!