+ Start a Discussion
❤Code❤Code 

records not getting updated in apex batch

Hi ,
I am running a batch to update a field - Below is the code 


global class MassDeleteRecords implements  Database.Batchable<sObject> {
global final string query;
public String query1;

global MassDeleteRecords (String q)
{
   query1 = q;
   system.debug(query1);
   query  = 'Select Id,Description__c from Account where Round_Robin_ID__c =:query1';
   system.debug(query);
}

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

   return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Account> scope){
  List<Account> lsttoupdate = new List<Account>();
  for(Account s : scope){
     s.Description__c = 'Updated in Batch Apex';
     lsttoupdate.add(s);
     }
     update lsttoupdate;
    }
global void finish(Database.BatchableContext BC){
 
}
}

From Developer console I am running the below code - 

String q = '1';
MassDeleteRecords b = new MassDeleteRecords(q);
Database.executeBatch(b);

But i see no records has been updated. Can anybody help..
ManjunathManjunath
Hi,

Do you have records with "Round_Robin_ID__c " field value equal to 1?
Try to put System.debug in the executue method to display the processing record. Based on that we can see what is happening.

Regards,
Manjunath
❤Code❤Code
Yes i Have records in Round_Robin_ID__c as 1,2,3. So i want to create 3 batch like - 
MassDeleteRecords b = new MassDeleteRecords(1);
MassDeleteRecords b1 = new MassDeleteRecords(2);
MassDeleteRecords b2 = new MassDeleteRecords(3);

So records will be divided and processed..
❤Code❤Code
Hi Manjunath,

I have started getting the below error - 

32.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 15:37:40.033 (33354905)|EXECUTION_STARTED 15:37:40.033 (33389093)|CODE_UNIT_STARTED|[EXTERNAL]|01p900000068L6Y|MassDeleteRecords 15:37:40.074 (74654817)|METHOD_ENTRY|[1]|01p900000068L6Y|MassDeleteRecords.MassDeleteRecords() 15:37:40.074 (74670351)|METHOD_EXIT|[1]|MassDeleteRecords 15:37:40.074 (74757359)|SYSTEM_METHOD_ENTRY|[15]|Database.getQueryLocator(String) 15:37:40.076 (76808087)|EXCEPTION_THROWN|[15]|System.QueryException: Invalid bind expression type of String for column of type Decimal 15:37:40.076 (76871229)|SYSTEM_METHOD_EXIT|[15]|Database.getQueryLocator(String) 15:37:40.076 (76928610)|FATAL_ERROR|System.QueryException: Invalid bind expression type of String for column of type Decimal Class.MassDeleteRecords.start: line 15, column 1
 
❤Code❤Code
The values of Round_Robin_ID__c in Salesforce
2.0
3.0
2.0
3.0
1.0
2.0
3.0
1.0

Regards
ManjunathManjunath
Hi,
Your query is throwing error "(76808087)|EXCEPTION_THROWN|[15]|System.QueryException: Invalid bind expression type of String for column of type Decimal" .
Try to cast "query1" to decimal before building the query.

I prefer you change the constructor parameter to decimal from string.

Regards,
Manjunath