+ Start a Discussion

writing trigger to calculate current fiscal year revenue on account object

I have been suffering with lack of logic from last couple of days.
i want to write a trigger on account object custom field which will calculate revenue for current fiscal yearYTD based on oppotunity amount.
once the current fiscal yearYTD gets changed again calculations start from next fiscal yearYTD.(that mean whatever value stored for last current fiscal yearYTD need to be override with urrent fiscal yearYTD)
Can somebody help me in figure out the best approach to get current fiscalYTD.
I hope i'm clear with my question.

Thanks in Advance

nitin sharmanitin sharma
Can u see if you can accomplish it with the workflow rules such as updating a field ?
So,you need to query the Opportunity in current Fiscal Year in your Trigger and make the changes something like below :-

// Query the current fiscal yr opps
List<Opportunity> oppList = [SELECT Id FROM Opportunity WHERE CloseDate = THIS_FISCAL_YEAR};

Then,you need to do some operation so that the Trigger would re-fire again in Next fiscal year as you need to update the next fiscal year values too something like a scedulable class.

I would rather suggest you to use Schedulable class to do your operation in place of Trigger if you requirement is to update once in Fiscal year not real time.

Hope this helps!!

If this helps,please mark it as best answer to help others :)
You're on the right path.  This is something best handled by Custom Apex.  But, keep in mind that Triggers only operate when data on the Account is changed.  Namely, either an Integration or a User must edit something on your Account to execute the Trigger.

Vinit is also on the right path, but I'd take it one step further.  Vinit is correct that a Scheduled Job would help when the calendar year rolls over, but I would put most of my logic within Batch Apex, not the Scheduled Apex.  Why you ask?  Batch Apex has much higher governor limits than Scheduled Apex.  If you have too many Accounts, the Scheduled apex might hit limits.  With Batch, you most likely won't hit any limits (or not anytime soon anyway).
Vishant ShahVishant Shah
You can add a lookup summary field to do the same thing as Account and Opportunity have a Master-Detail, and you can apply lookup filter to the summary field. 

Let us know if it helps , please mark it as best answer to help others.
Thanks every one for your valuable suggestions.
@Shankwi: yes , u r correct calculations only happen when we any thing changed in records but aggregare functions lets you calculate without editing also.correct me if im wrong.using aggregate functions in soql will consider whole records for calculations.
@Vini_kumar: if we use THIS_FISCAL_YEAR in where clause its keep calculates the value.calculations wont happen when year changed.

here is my explanation for the my problem.
Think that 2014 is current fiscal YTD.thare is field called sum.when fiscal year started ,values should added to that sum field.think that sum is having value as 500.when 2014 is changed and fall into next fiscal year 2015,  calculations should happen from first value.

Hope im clear with my question