Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

date calculation

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();
					rps.add(rp);	
				//}
				//insert accs;
			}
		}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can add a reference to Microsoft.VisualBasic and access the DateDiff function like this:
 
 long nMonths = Microsoft.VisualBasic.DateAndTime.DateDiff(Microsoft.VisualBasic.DateInterval.Month, dt1, dt2);