We help IT Professionals succeed at work.

# date calculation

on
989 Views
I'm writing a method where I am trying to accomplish the following.  I need to write MONTHLY child/detail records if the record type is 'billable project' and the contract extension field equals true on the master table, which is opportunity.

For example, if a billable project opportunity is marked as won, then the methods sees that the start date is 12/1/2010 and end date is 3/5/2011 and writes FOUR months of Detail records to a new "Revenue Projection" table that is a master/detail relationship to the Opportunity table. It writes FOUR records becase the start date and end date touched four months. The Amount of this opportunity is \$40,000 (in this example), so the logic should simply slice it up four ways like so:

Month__c       Revenue__c
12/1/2010      \$10,000
1/1/2011         \$10,000
2/1/2011         \$10,000
3/1/2011         \$10,000

See below for my method code.

I'm trying to calculate the number of months by subtracting the end date from the start date and divide by 30.5.  Is this correct way to get the number or months or is there a better way?

How can I generate the correct dates to enter into the Month__c field?  Oh, the month field is a date datatype.  In the above example, I'm looping thru 4 times, but I'm stuck on how to correctly generate the dates?

The programming language I'm using is called APEX which is used on the Salesforce platform.  What I am most interestd in is the logic to accomplish the task.  I can take the necessary logic and apply to the APEX language.

Thanks for any help.
``````private static void doPopulateRevenueRecognition(List<Opportunity> inNew)
{
for (Opportunity opp : inNew)
{
if (opp.RecordType.Name == 'Billable Project' && opp.Contract_Execution__c == true)
{
//Integer numMnths = (opp.Revenue_Recognition_End_Date__c - opp.Revenue_Recognition_Start_Date__c)/30.5;
//Decimal revenueSplit = opp.Amount/numMnths;

//Revenue_Projection__c[] rps = new Revenue_Projection__c[]{};

//for (Integer i = 0; i < numMnths; i++)
//{
Revenue_Projection__c rp = new Revenue_Projection__c();
rp.Month__c = '';
rp.Revenue__c = revenueSplit.round();
//}
//insert accs;
}
}
``````
Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
###### Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant