+ Start a Discussion
Zeeshan Moulvi 2Zeeshan Moulvi 2 

Update Field(Apex Class Schedule Daily)

Hi,

I have a requirement we i need to update the field daily.
Every day when date changes the value from Field 1 should be updated in Field 2  and Field 2 value should be moved to Field 3.
This process should trigger every day.

Object Name: Daily_Check_List__c
Field 1: Todays_Status__c
Field 2: YesterdayStatus__c
Field 3: X2DaysPastStatus__c

Thanks,
Zeeshan.
Keshab AcharyaKeshab Acharya
Hey Zeeshan,

Thanks for posting your query. You can write a batch job to achieve this in Apex. See below code snippet. 
 
global class updateField implements Database.Batchable
{
	global final String Query;
	global updateField(String q)
	{
		Query=q;
	}
	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(query);
	}
	global void execute(Database.BatchableContext BC,List scope)
	{
		List <Daily_Check_List__c> checkList = new list<Daily_Check_List__c>();
		for(Sobject s : scope)
		{
			 Daily_Check_List__c a = (Daily_Check_List__c)s;
			 a.YesterdayStatus__c = a.Todays_Status__c;
			 a.X2DaysPastStatus__c = a.YesterdayStatus__c;
			 checkList.add(a);
		}
		update lstAccount;
	}
	global void finish(Database.BatchableContext BC)
	{
		//You can send an email if you wish to
	}
}
//Now Lets call the Batch Job
Id batchinstanceid = database.executeBatch(new updateField(‘select Id,Daily_Check_List__c,YesterdayStatus__c,X2DaysPastStatus__c from Daily_Check_List__c’));

This code is not tested and excuse for best practices..

Thanks,
Keshab Acharya