You need to sign in to do that
Don't have an account?
theitdeptrocks
Reset field to '0' on first day of each quarter
Hi all,
I have a trigger that increments a number field on my Opportunity object for each new Case that is created for that Opp.
The field name is 'Cases this Quarter.'
How would I go about resetting this count to '0' on the first day of each quarter?
Thanks in advance!
Oops.. sorry.. I didn't mean to confuse you with the System Log > Execute Apex tool.. So let me be very explicit....
First create a class (Setup > Develop > Apex Classes > New) and paste in the contents of your resetCasesthisQuarter class.
Then create another class and fill that in with the contents of your resetCasesthisQuarterSched class. Although you should modify it slightly like below:
then you can schedule the resetCasesthisQuarterSched class to run the first of every month.
Alternatively... if you wanted to one off force the code to run you could open the System Log window and in the Execute Apex section you could put in :
and click run, which would force it to run. Doing this would actually update your opportunity records too. (I know earlier I said nothing would happen, but I read the Help and Training on that page wrong.. :-) )
Hope that clears things up.
All Answers
Well you can always write a schedulable apex class and have it run on the first of every month that could go through every opportunity class and reset that number to 0. You probably should also make this code batchable too if the number of opportunity records you need to reset is "large".
FYI..
I'm guessing that the relationship between your case object and opportunity is not Master-Detail. If it is though.... A Rollup Summary field would probably be the way to go.
For 600-800 records you can probably perform all your logic in one run without hitting any governor limits, so you can probably get away with not implementing "batchable apex" right now.
If this number ever gets close to 10k you may way to re-consider as your Apex code will just suddenly stop working due to governor limits. if you have extra time, you should probably at least read up on governor limits and Batchable Apex just to know what they are.
Thanks VinOK,
I think I've got it setup, but will see in about 20 minutes.
I'm concerned about two things:
1) Will it work as is? - We'll find out soon enough
2) Is my update statement the right way to update each record?
you should probably take the "Update" out of the loop as each update call counts against the governor limit, and I believe that scheduled Apex runs with the "Anonymous Block, Visualforce Controller, or WSDL Method" governor limits, so you can only call update 100 times max.
you should probably replace your resetCount method with something that looks more like this.
The idea is to collect all updated records into a list and then call update once with a big list.
Ok great! I didn't know a 'for' could be done like that (I'm more admin that developer, but learning!).
I have updated my code - can a class be manually ran or do I have to reschedule and wait?
Sorry.. I think i read the Help and Training page incorrectly. Using the System Log > Execute Apex box actually executes the apex code and saves any DML inserts / updates / deletes that are made.
Drats... it didn't work.
Here's the class I have scheduled:
Putting that into the "System Log" execute area returns: Compile Error: line 1, column 14: Global type must be contained inside of a global class
Then for confirmation, my resetCasesthisQuarter class looks like this:
Putting that into the "System Log" execute area returns: Last execution at 07/22 16:11:12 in 30 milliseconds.
And then a log entry:
Oops.. sorry.. I didn't mean to confuse you with the System Log > Execute Apex tool.. So let me be very explicit....
First create a class (Setup > Develop > Apex Classes > New) and paste in the contents of your resetCasesthisQuarter class.
Then create another class and fill that in with the contents of your resetCasesthisQuarterSched class. Although you should modify it slightly like below:
then you can schedule the resetCasesthisQuarterSched class to run the first of every month.
Alternatively... if you wanted to one off force the code to run you could open the System Log window and in the Execute Apex section you could put in :
and click run, which would force it to run. Doing this would actually update your opportunity records too. (I know earlier I said nothing would happen, but I read the Help and Training on that page wrong.. :-) )
Hope that clears things up.
Awesome. Glad I could help