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
hemant ranahemant rana 

at the end of a month a field value gets updated to 0

hi i have a requirement where at the end of every month a field is set to 0 by itself.
I think about workflow but in worflow i have to create or edit a record then only workflow will fire.
So, please help.
Best Answer chosen by hemant rana
CheyneCheyne
To do this, you'll need to write a scheduled apex class that runs at the end of the month, and sets that field to 0 for all relevant records. You can find the documentation for scheduling Apex code at http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm. Basically, your class will look like this:

<pre>
global class ScheduledUpdateToZero implements Schedulable {
   global void execute(SchedulableContext SC) {
      //Update your records here.
   }
}
</pre>

Then, to schedule it, you can go into the Developer Console and execute the following anonymous Apex:

<pre>
ScheduledUpdateToZero updater = new ScheduledUpdateToZero();
String sch = '59 59 23 L * ?';
system.schedule('Update field to 0', updater, sch); 
</pre>

That should schedule the class to run at 11:59:59 PM on the last day of every month. You can verify that by going to Setup -> Administration Setup -> Monitoring -> Scheduled Jobs. You should see the job "Update field to 0" that you just created on the list.

All Answers

CheyneCheyne
To do this, you'll need to write a scheduled apex class that runs at the end of the month, and sets that field to 0 for all relevant records. You can find the documentation for scheduling Apex code at http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm. Basically, your class will look like this:

<pre>
global class ScheduledUpdateToZero implements Schedulable {
   global void execute(SchedulableContext SC) {
      //Update your records here.
   }
}
</pre>

Then, to schedule it, you can go into the Developer Console and execute the following anonymous Apex:

<pre>
ScheduledUpdateToZero updater = new ScheduledUpdateToZero();
String sch = '59 59 23 L * ?';
system.schedule('Update field to 0', updater, sch); 
</pre>

That should schedule the class to run at 11:59:59 PM on the last day of every month. You can verify that by going to Setup -> Administration Setup -> Monitoring -> Scheduled Jobs. You should see the job "Update field to 0" that you just created on the list.
This was selected as the best answer
hemant ranahemant rana
Thanks Cheyne.... it worked