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
pratyusha raavipatipratyusha raavipati 

i want apex code for mass update button for task

hi,
actually my requirement was i have so many tasks to complete but i did not completed on this day so have to postphone then to someother day means i want change the date at a time for suppose of 50 tasks

please help me

Thanks & Regards
Pratyusha K
NagendraNagendra (Salesforce Developers) 
Hi Pratyusha,

This is a very, very basic example you can work off of. Here's what's going on:

You are creating a method to be called from the Visualforce page called "updateMyAccounts". First in the method, you are creating a list of Object__c records by running a query. The query I wrote simply grabs only the info you need for this example: the id of the record and the Custom_Date_Field__c for each record. I added a filter to only select records "WHERE CreatedDate = TODAY" just as an example of a filter you can add to narrow which records you want to mass update. Keep in mind pulling back 10,000+ records will error out when updating. Next, you enter a for loop to iterate through the list and assign today's date to the Custom_Date_Field__c for each record. Then you will update the entire list to save the changes to the date field.

In your Visualforce page you will create a button that can be pressed and when it is, the method above will run and all the records will be updated.

Controller Page:
public void updateMyAccounts() {
   Object__c[] objs = [SELECT id, Custom_Date_Field__c FROM Object__c WHERE CreatedDate = TODAY];
   for (Object__c obj : objs) {
   obj.Custom_Date_Field__c = date.today();
}
update objs;
}
Visual Force Page:
<apex:commandButton value="Update Dates" action="{!updateMyAccounts}"/>
Please mark this as solved if the information helps so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Best Regards,
Nagendra.