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
Venkata Sumedha singanamalaVenkata Sumedha singanamala 

how to update the status column using schedule apex

Hi All,

I have a small requirment I want to update the status column in the EMP custom tab with "Batched" and "Not Batched" I mean some column should have as "Batched" and some as"Not Batched" and this should be done at a specifed time

Please help on this. For your reference Please find the screen shot attached.

User-added image

Regards,
Seshagiri



AshlekhAshlekh
HI

If you want to wirte a batch and want to schedule a apex class then below code heps you.

Just customize query and field and object api names 

global class ExampleBatchClass implements Database.Batchable<sObject>{

        global ExampleBatchClass(){
                   // Batch Constructor
        }
       
        // Start Method
        global Database.QueryLocator start(Database.BatchableContext BC){
          String query ='select id,status from YOUROBJECT_API_NAME> ';
		 return Database.getQueryLocator(query);
        }
      
      // Execute Logic
       global void execute(Database.BatchableContext BC, List<YOUR OBJECT API NAME>scope){
				for(YOUROBJECT_API_NAME c :Scope){
					c.Status_Field_API_NAME = 'VALUE YOU want to provide';
				}
			update scope;
       }
     
       global void finish(Database.BatchableContext BC){
            // Logic to be Executed at finish,if you want to send email to you then write code here 
			//This code will execute in last when all record are updated
       }
    }

Now here is schedule class which will run batch when schedule class execte

global class scheduledMerge implements Schedulable {
   global void execute(SchedulableContext SC) {
      ExampleBatchClass b = new ExampleBatchClass(); 
	  //Parameters of ExecuteBatch(context,BatchSize)
	  database.executebatch(b,100); 
   }
}
Here is code to schedule scheduleMerge class, You need to tell when it should be run by cron exp.
scheduledMerge m = new scheduledMerge();
String sch = '0 0 8 10 2 ?';
String jobID = system.schedule('status update', sch, m);
For corn expression : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

If you want run batch directly without scheduling

ExampleBatchClass b = new ExampleBatchClass(); 
	  //Parameters of ExecuteBatch(context,BatchSize)
	  database.executebatch(b,100);





Vatsal KothariVatsal Kothari
Hi,

You can do this with schedule Apex. Create a class which implements Schedulable interface.(Setup --> customize --> Apex Classes --> New)

Refer Below code:

global class TestScheduledApexEmp implements Schedulable {

	global void execute(SchedulableContext ctx) {
      Account a = [SELECT Id, Name,Salary__c,Designation__c,Status__c FROM Emp__c WHERE Designation__c = 'Manager'];
      a.Status__c = 'Batched';
      update a;
	  
	  Account a = [SELECT Id, Name,Salary__c,Designation__c,Status__c FROM Emp__c WHERE Designation__c != 'Manager'];
      a.Status__c = 'Not Batched';
      update a;
	  
   }   
}

after saving the above class goto Schedule Apex (Setup --> customize --> Apex Classes --> Schedule Apex ).
Select the schedule when you want to run above task.

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
Venkata Sumedha singanamalaVenkata Sumedha singanamala
Thanks for the help guys.I have tried the code but still the status field is not getting executed.Please find the below code what I have used please let me know if there any mistake.

global class EMPbat implements Database.Batchable<sObject>{

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

String query='select id,status from Emp__c';

return Database.getQueryLocator(query);

}

global void execute(Database.BatchableContext ct, LIST<Emp__c> la){

for(Emp__c e:la){

e.Status__c='Batched';

}

update la;

}

global void finish(Database.BatchableContext ct){

Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'seshagiri.esp@gmail.com'};
mail.settoAddresses(toAddresses);
mail.setSubject('batching Completed');
mail.setPlainTextBody('The Batch apex job has been processed');
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});

}

}

and to execute the above code I have created the object of the above class implementing the schedulable interface

global class Empbatsch implements schedulable{

global void execute(SchedulableContext sc){

Empbat eb= new Empbat();
Database.executeBatch(eb);

}

}

please help me.. :(

Regards,
Seshagiri