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
Asma ErumAsma Erum 

Split Opportuntiy Revenue by month based on start an end date

I am trying to write a custom report using Apex and visual force that would list all the opportunities and their monthly revenue based on the Opportunity's Start Date, End Date and Total Revenue, and Business_Days (all 3 are custom fields). 

Esentially I would need to calcualte the Buisness Days (excluding weekends) between the start and end date and then divide total revenue by this to get the per day revenue. 

Can anyone guide me as to how this can be done via Apex class. I'm fairly new to using Apex. An extrmely small sample code would help. 

Here is what I have so far, but I don't know if I'm on the write direction: 

public class revenueAggregationByMonth {
    
    public static void GettOpps(Date StartDate, Date EndDate){
  
    List<Opportunity> allopps = [SELECT Account.Name, Name, Business_Days__c, Start_Date__c, End_Date__c, Estimated_Production_Revenue_Won__c
                FROM Opportunity
                WHERE Start_Date__c >= StartDate AND End_Date__c <= EndDate];
     
        for(Opportunity o: allopps)
        {
            decimal Revenue_Per_Day = 0.00;
            if (o.Estimated_Production_Revenue_Won__c > 0 && o.Business_Days__c > 0)
            {
                Revenue_Per_Day = o.Estimated_Production_Revenue_Won__c/o.Business_Days__c;
                System.debug('Account Name: '+ o.Account.Name + ' Opporunity Name: '+ o.Name + 'Revenue Per Day: '+ Revenue_per_Day);
            }
        }
    }
}

Andy BoettcherAndy Boettcher
You'll want to work with the built-in "Business Hours" object in Salesforce and the "BusinessHours" class in Apex.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm

It won't be EXACT, but that gives you the business hours root number.