+ Start a Discussion
Mahesh MMahesh M 

can any one help this code?

Hi,
I have to increment with 100 in  one custom filed for very 5 minutes using batch class.
Best Answer chosen by Mahesh M
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello, 
I am assuming that field on the account Object and you have to update all the records. 
Adjut the code as per your requirement - 
global class SearchAndReplace implements Database.Batchable<sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('Select id, name, Field_To_Update__c from Account');
   }

   global void execute(Database.BatchableContext BC, List<Account> scope){
    List<Account> accListToUpdate = new List<Account>();
    for(Account Acc : scope){
	    Acc.Field_To_Update__c = Acc.Field_To_Update__c+100;
		accListToUpdateadd(Acc);
	}
	if(accListToUpdate != null && accListToUpdate.size()>0){
		update accListToUpdateadd;
	}

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

Thnaks,
Sumit Kumar Singh

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello, 
I am assuming that field on the account Object and you have to update all the records. 
Adjut the code as per your requirement - 
global class SearchAndReplace implements Database.Batchable<sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('Select id, name, Field_To_Update__c from Account');
   }

   global void execute(Database.BatchableContext BC, List<Account> scope){
    List<Account> accListToUpdate = new List<Account>();
    for(Account Acc : scope){
	    Acc.Field_To_Update__c = Acc.Field_To_Update__c+100;
		accListToUpdateadd(Acc);
	}
	if(accListToUpdate != null && accListToUpdate.size()>0){
		update accListToUpdateadd;
	}

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

Thnaks,
Sumit Kumar Singh
This was selected as the best answer
rachit rakesh7rachit rakesh7
Hi 

here is the code for a batch class that will increment the value of a field by 100 in every 5 minutes 

global with sharing class BatchLeadupdateNumberofEmp implements Database.Batchable<sObject> {
 global String query{ get; set;}

  global Database.QueryLocator start(Database.BatchableContext BC){
    
   String query = 'SELECT Id,Name,NumberOfEmployees FROM Lead';

    return Database.getQueryLocator(query);   
   }
   
global void execute(Database.BatchableContext BC, List<Lead> scope)
{
list<Lead> accList = new list<Lead>();
    
for(Lead acc: scope){
    if(acc.NumberOfEmployees!=null){
    acc.NumberOfEmployees +=100;
    accList.add(acc);
}
}

update accList;
}
 global void finish(Database.BatchableContext BC){
   
  }
  
}

Now just schedule this batch class for every 5 minutes.
Thanks
Amit Chaudhary 8Amit Chaudhary 8
Hi Mahesh Kumar 97,

Example given my Sumit (+1) good one for you.

Batch Job
global class SearchAndReplace implements Database.Batchable<sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('Select id, name, Field_To_Update__c from Account');
   }

   global void execute(Database.BatchableContext BC, List<Account> scope){
    List<Account> accListToUpdate = new List<Account>();
    for(Account Acc : scope){
	    Acc.Field_To_Update__c = Acc.Field_To_Update__c+100;
		accListToUpdateadd(Acc);
	}
	if(accListToUpdate != null && accListToUpdate.size()>0){
		update accListToUpdateadd;
	}

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

But to schedule the Batch in every 5 min you need to create one scheduler class like below
global class scheduledBatchable implements Schedulable 
{
    global void execute(SchedulableContext sc) 
    {
		SearchAndReplace b = new SearchAndReplace(); 
		database.executebatch(b);
    }
}
To Excute scheduler in every 5 min you need to exceute below code i developer console
scheduledBatchable bs = new scheduledBatchable();
String sch = '0 6 * * * ? ';
System.schedule('My Job1', sch, bs);
Check below post for more detail
http://srinusfdc.blogspot.in/2012/10/scheduling-batch-class-for-every-5.html
or
System.schedule('Scheduled Job 1', '0 0 * * * ?', new scheduledBatchable());
System.schedule('Scheduled Job 2', '0 15 * * * ?', new scheduledBatchable());
System.schedule('Scheduled Job 3', '0 30 * * * ?', new scheduledBatchable());
System.schedule('Scheduled Job 4', '0 45 * * * ?', new scheduledBatchable());
Let us k now if this will help you