function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
reema_agarwalreema_agarwal 

Help to resolve the error in the batch apex code to escalate cases

Hello

 

Initially i tried to escalate cases using workflow and escalation rules but that is not working so now  I am trying to escalate the cases using batch apex in which the conditions are like:

Date/Time opened is more then one hour and still if the status is not closed then escalate it.

I have tried to write a code but i am getting an error can someone please help me to remove the error and also help me with the code.

 

global class CaseEscalation  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';
     }
    }
   }
global void finish(Database.BatchableContext ctx){
       }
    }  

This is the error i am getting
Error: Compile Error: Expression cannot be assigned at line -1 column -1    

yvk431yvk431

Here you go

 

global class CaseEscalation  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 =  new List<Case>();
       for(SObject so :scope)
       {
           Case c = (Case)so;
           ca.add(c);
       }    
       
       for(Case c : ca)
       {                      
            Double Days = Math.Floor((System.now().getTime()  - c.CreatedDate.getTime())/ (1000.0*60.0*60.0*24));                       
            if(Days > 1  &&  c.Status != 'Closed' )
             {
                 String emailMessage = 'The case  ' + c.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 });
                 c.Status = 'Escalated';
             }
        }
   }
   
    global void finish(Database.BatchableContext ctx)
    {
    }


}  

 

---yvk

reema_agarwalreema_agarwal

Hi 

 

I tried your code it has no errors. I have made some changes in it which i have highlighted in which the date/Time criteria i have made as more the one day it is not showing any errors.But the cases are not getting escalated.

I have written below the scheduler class and have also shown the debug log.Could you please help me.

 

Batch Apex Class

 

global class CaseEscalation  implements  Database.Batchable<SObject> 
{
     global Database.QueryLocator start(Database.BatchableContext  BC)
     {
         return Database.getQueryLocator('SELECT  CaseNumber, CreatedDate,Status FROM Case where CreatedDate<Today and Status!="Closed"');  
     }
      
      
     global void execute(Database.BatchableContext ctx, List<Sobject> scope)
     {
       
       
       List <Case> ca =  new List<Case>();
       for(SObject so :scope)
       {
           Case c = (Case)so;
           ca.add(c);
       }    
       
       for(Case c : ca)
       {                      
           // Double Days = Math.Floor((System.now().getTime()  - c.CreatedDate.getTime())/ (1000.0*60.0*60.0*24));                       
            //if(Days > 1  &&  c.Status != 'Closed' )
             //{
                System.Debug(c);
                 String emailMessage = 'The case  ' + c.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 });
                 c.Status = 'Escalated';
            // }
        }
        update ca;
   }
   
    global void finish(Database.BatchableContext ctx)
    {
    }


}

 

I have written the following Scheduler class for it:

 

global class scheduledBatchable implements Schedulable{
   global void execute(SchedulableContext sc) {
     CaseEscalation c = new CaseEscalation(); 
      database.executebatch(c);
   }
}

 

In developer console :

System.Schedule('Scheduler' ,'0 0 0-23 * * ?',new scheduledBatchable());

 

24.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO

Execute Anonymous: System.schedule('Scheduler', '0 0 0-23 * * ?', new scheduledBatchable());

01:38:58.070 (70655000)|EXECUTION_STARTED

01:38:58.070 (70667000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex

01:38:58.071 (71842000)|STATEMENT_EXECUTE|[1]

01:38:58.071 (71854000)|STATEMENT_EXECUTE|[1]

01:38:58.071 (71867000)|HEAP_ALLOCATE|[1]|Bytes:9

01:38:58.071 (71875000)|HEAP_ALLOCATE|[1]|Bytes:14

01:38:58.073 (73802000)|METHOD_ENTRY|[1]|01pU0000000pSEO|scheduledBatchable.scheduledBatchable()

01:38:58.073 (73814000)|STATEMENT_EXECUTE|[1]

01:38:58.074 (74048000)|SYSTEM_MODE_ENTER|false

01:38:58.074 (74078000)|HEAP_ALLOCATE|[1]|Bytes:5

01:38:58.074 (74089000)|STATEMENT_EXECUTE|[1]

01:38:58.074 (74100000)|SYSTEM_MODE_EXIT|false

01:38:58.074 (74118000)|METHOD_EXIT|[1]|scheduledBatchable

01:38:58.074 (74150000)|HEAP_ALLOCATE|[1]|Bytes:4

01:38:58.074 (74195000)|CONSTRUCTOR_ENTRY|[1]|01pU0000000pSEO|<init>()

01:38:58.074 (74237000)|VARIABLE_SCOPE_BEGIN|[1]|this|scheduledBatchable|true|false

01:38:58.074 (74313000)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x38cef644

01:38:58.074 (74356000)|SYSTEM_MODE_ENTER|false

01:38:58.074 (74368000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5

01:38:58.074 (74385000)|SYSTEM_MODE_EXIT|false

01:38:58.074 (74405000)|VARIABLE_SCOPE_BEGIN|[2]|this|system.ApexBaseClass|true|false

01:38:58.074 (74431000)|VARIABLE_ASSIGNMENT|[2]|this|{}|0x38cef644

01:38:58.074 (74452000)|SYSTEM_MODE_ENTER|false

01:38:58.074 (74460000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5

01:38:58.074 (74477000)|SYSTEM_MODE_EXIT|false

01:38:58.074 (74486000)|STATEMENT_EXECUTE|[1]

01:38:58.074 (74494000)|SYSTEM_MODE_ENTER|false

01:38:58.074 (74501000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5

01:38:58.074 (74512000)|SYSTEM_MODE_EXIT|false

01:38:58.074 (74539000)|CONSTRUCTOR_EXIT|[1]|01pU0000000pSEO|<init>()

01:38:58.074 (74588000)|SYSTEM_METHOD_ENTRY|[1]|System.schedule(String, String, APEX_OBJECT)

01:38:58.131 (131372000)|SYSTEM_METHOD_EXIT|[1]|System.schedule(String, String, APEX_OBJECT)

01:38:58.235 (133845000)|CUMULATIVE_LIMIT_USAGE

01:38:58.235|LIMIT_USAGE_FOR_NS|(default)|

  Number of SOQL queries: 0 out of 100

  Number of query rows: 0 out of 50000

  Number of SOSL queries: 0 out of 20

  Number of DML statements: 0 out of 150

  Number of DML rows: 0 out of 10000

  Number of script statements: 1 out of 200000

  Maximum heap size: 0 out of 6000000

  Number of callouts: 0 out of 10

  Number of Email Invocations: 0 out of 10

  Number of fields describes: 0 out of 100

  Number of record type describes: 0 out of 100

  Number of child relationships describes: 0 out of 100

  Number of picklist describes: 0 out of 100

  Number of future calls: 0 out of 10

 

01:38:58.235|CUMULATIVE_LIMIT_USAGE_END

 

01:38:58.133 (133887000)|CODE_UNIT_FINISHED|execute_anonymous_apex

01:38:58.133 (133895000)|EXECUTION_FINISHED

 

Thanks!

reema_agarwalreema_agarwal

I am not able to get what is the problem in it.

 

Can someone please help.