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
venkateshyadav1243venkateshyadav1243 

scheduler class

Hi

 

I need small help

i want wirte one shcduler class  to rename the accounts name after one year,

what i need to do is i created some records in salesforce from december 2012 to november 30

i want schedule a class on every year dec 1

to update the all accounts name to old accounts

 

actualy i written the class

am not aware how can i fetch the reocrds from accounts records that are  created on date  dec 2012 to nover 30 fro every year

 

 

can any one help

 

regards

venkatesh

Best Answer chosen by Admin (Salesforce Developers) 
prakash_sfdcprakash_sfdc

Hi,

 

Try the following code:

 

global class updateAccountNames Implements Schedulable
{
public Date startDate{get;set;}
public Date endDate{get;set;}
           
                       global void execute(SchedulableContext sc)
                        {
                            startDate = date.newinstance(Date.today().year()-1, 12, 01);
       endDate = date.newinstance(Date.today().year(), 11, 30);
updateAccountNames();
                                   
                        }
                        
                    
                        public void updateAccountNames()
                        {
                                    List<Account> accountsToUpdate = new List<Account>();
for(Account acc: [SELECT Name FROM Account WHERE CreatedDate>=startDate AND CreatedDate<=EndDate])
{
acc.Name = newName;
accountsToUpdate .add(acc);
}
update accountsToUpdate;
                      }
}

Please give kudos if it helps.

Cheers!

All Answers

prakash_sfdcprakash_sfdc

Hi,

 

Try the following code:

 

global class updateAccountNames Implements Schedulable
{
public Date startDate{get;set;}
public Date endDate{get;set;}
           
                       global void execute(SchedulableContext sc)
                        {
                            startDate = date.newinstance(Date.today().year()-1, 12, 01);
       endDate = date.newinstance(Date.today().year(), 11, 30);
updateAccountNames();
                                   
                        }
                        
                    
                        public void updateAccountNames()
                        {
                                    List<Account> accountsToUpdate = new List<Account>();
for(Account acc: [SELECT Name FROM Account WHERE CreatedDate>=startDate AND CreatedDate<=EndDate])
{
acc.Name = newName;
accountsToUpdate .add(acc);
}
update accountsToUpdate;
                      }
}

Please give kudos if it helps.

Cheers!
This was selected as the best answer
prakash_sfdcprakash_sfdc

Hi,

 

Can you please give kudos :)

venkateshyadav1243venkateshyadav1243



Hi for the startdate and enddate

 

am getting the dates

startDate2012-12-01

 

endDate2013-11-30


if i want perform in the nex year means 2013 to 2014,2014 to 2015.....................

 

how can i get the date ?

prakash_sfdcprakash_sfdc
It will take the start year as the previous year and end year as the current year.