0

I have written a simple batch class to update a field.

Here is my code:

global class batchCaseUpdate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,LastChatterPostDate__c,No_of_Days_Case_Untchd_Business_Days__c FROM Case WHERE (Status = "In Progress")';
        return Database.getQueryLocator(query);

    }

    global void execute(Database.BatchableContext BC, List<Case> scope) {

         for(Case c : scope)
         {
             String BusinessHours_Id;

             Long  Mins_since_lastTouched = BusinessHours.diff (BusinessHours_Id, c.LastChatterPostDate__c, System.Now()) / 1000 / 60;
             c.No_of_Days_Case_Untchd_Business_Days__c = Mins_since_lastTouched / 24 / 60;

         }
         update scope;
    }   

    global void finish(Database.BatchableContext BC) {
    }
}

When i am executing the above batch class, it is not executing. Please can some one suggest me where the fault is?

BR

3
  • How do you know if it executes or not? add system.debug(""); message inside the execute method to see. Nov 8, 2016 at 11:02
  • Are you seeing that this Batch class is not being executed through a test class? If you have a test class, can you please provide it here.
    – TSmith
    Nov 8, 2016 at 11:04
  • got the below error, when checked from logs. Error: no viable alternative at character "" at line 4 column 1
    – venky
    Nov 8, 2016 at 11:25

3 Answers 3

1

Try below code: SOQL query was not correct. Added the escape character that will run the query as expected.

global class batchCaseUpdate implements Database.Batchable<sObject> {
  global Database.QueryLocator start(Database.BatchableContext BC) {
    String query = 'SELECT Id,LastChatterPostDate__c,No_of_Days_Case_Untchd_Business_Days__c FROM Case WHERE Status = \'In Progress\'';
    return Database.getQueryLocator(query);

  }

  global void execute(Database.BatchableContext BC, List<Case> scope) {

     for(Case c : scope)
     {
         String BusinessHours_Id;

         Long  Mins_since_lastTouched = BusinessHours.diff (BusinessHours_Id, c.LastChatterPostDate__c, System.Now()) / 1000 / 60;
         c.No_of_Days_Case_Untchd_Business_Days__c = Mins_since_lastTouched / 24 / 60;

     }
     update scope;
  }   

  global void finish(Database.BatchableContext BC) {
  }
}
8
  • How to exscape if i have multiple conditions in SOQL like.... String query = 'SELECT Id,LastChatterPostDate__c,No_of_Days_Case_Untchd_Business_Days__c FROM Case WHERE Status = In Progress || Status = New || Status = Assigned. Thanks for the earlier sugestion.
    – venky
    Nov 8, 2016 at 11:42
  • Put the condition in Or clause like Status = \'In Progress\' OR Status = \'New\' likewise for all the condition that you want.
    – RCS
    Nov 8, 2016 at 11:47
  • 'SELECT Id,LastChatterPostDate__c,No_of_Days_Case_Untchd_Business_Days__c FROM Case WHERE Status = \'In Progress\' || Status = \'New\' || Status =\' Assigned\''
    – Samir
    Nov 8, 2016 at 11:49
  • 1
    Mark the answer as done if you have completed the task
    – RCS
    Nov 8, 2016 at 11:52
  • 1
    try to debug the value that you are trying to update see if the value is correctly populated. or else create a new List<Case> caseListToUpdate put the value in the list once you assign and finally update caseListToUpdate.
    – RCS
    Nov 8, 2016 at 11:56
0

Try changing your line no 3 with the below code.Do let me know if this works or not

String Status = 'In Progress';
String query = 'SELECT Id,LastChatterPostDate__c,No_of_Days_Case_Untchd_Business_Days__c FROM Case WHERE Status = ' +Status ;

OR

String query = 'SELECT Id,LastChatterPostDate__c,No_of_Days_Case_Untchd_Business_Days__c FROM Case WHERE Status = \'In Progress\'';
0

The documentation example leads people to use dynamic SOQL when static SOQL is almost always the better choice:

public class batchCaseUpdate implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator([
                SELECT Id, LastChatterPostDate__c, No_of_Days_Case_Untchd_Business_Days__c
                FROM Case
                WHERE (Status = 'In Progress')
                ]);
    }
    ...

because:

  • the code is clearer (less noise)
  • the query can be easily laid out over multiple lines so the logic is visible without having to scroll over to the right
  • quotes don't need escaping
  • the compiler checks the query
  • if binding is needed, allows the use of complex binding expressions whereas dynamic Apex only supports simple binding expressions

Also note that public not global is usually the appropriate access modifier.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .