+ Start a Discussion
Sharad Jain 9Sharad Jain 9 

Apex code to Update custom date field with 60 days

I have to update Custom date field Mydate__c on Custom object X__c by adding 60 days to it. I need to write a apex code to update 20000 records. Can anyone help me how to achieve it.. Please look into this on high priority
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Sharad,

Do we need to date 60days from today to this field 'Mydate__c '?

and For update 20000 records,it's better to use batch Apex .

Thanks
Bhargavi.
Sharad Jain 9Sharad Jain 9
Thanks Bhargavi for your reply but I need to update around 15k records and it is one time process for this I need to create a script 
NitishNitish
Hi Sharad,
Fetch records on basis of Lastupdated Field with LIMIT 10000 and run your Script twice. Its not recommended but you can give a try. May be it will work.  
 
Sharad Jain 9Sharad Jain 9
This is the script but its not working can anyone corret it

Date dt=Date.today();
List<Service_Access__c> Opp=[select id ,Service_End_Date__c from Service_Access__c 
                             where Service_Access_End_Date__c > :dt Limit 1000];
System.debug('List is '+ Opp);
Map<Id,Service_Access__c> ServiceMap=new Map<Id,Service_Access__c>([select id from Service_Access__c]);
for(Service_Access__c acss:Opp){
    
   string accId = (string)acss.get('id');
   if(ServiceMap.containskey(accId)){
       ServiceMap.get(accId).Service_End_Date__c =  acss.Service_End_Date__c.addDays(60);
      
    } 
}
update ServiceMap;
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi
 
Date dt=Date.today();
List<Service_Access__c> Opp=[select id ,Service_End_Date__c from Service_Access__c 
                             where Service_Access_End_Date__c > :dt Limit 1000];

for(Service_Access__c acss:Opp){
    
  acss.Service_End_Date__c =acss.Service_End_Date__c.addDays(60);
      
    } 
}
update ServiceMap;
You can use the above code  for the update.

Thanks
Bhargavi.
 
Raj VakatiRaj Vakati
try this
 
Date dt=Date.today();
List<Service_Access__c> Opp=[select id ,Service_End_Date__c from Service_Access__c 
                             where Service_Access_End_Date__c > :dt Limit 1000];
for(Service_Access__c acss:Opp){
    
      acss.Service_End_Date__c =  System.today().addDays(60);
      
}
update ServiceMap;