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
bozotheclownbozotheclown 

Counting CreatedDate Occurrences from a DATE Perspectice

Hello. Could anyone give me some guidance with the below.

I maintain a list of sales orders and customers. I have created a VF page that lists/groups customer sales orders for a date range desired by the user. That part was easy.

My issue is that I want to show the average PER DAY sales totals for each customer in this list. Unfortunately, identifying the number of days in the date range is not easy as subtracting startdate-enddate....since I need to omit holidays/weekends.

I have tried to just identify unique occurrences of CreatedDate...but the datetime nature of this field means that multiple records from a single date are treated as multiple occurrences.

Does anyone have suggestions on how to count CreatedDate occurrences from a date perspective? Thanks in advance.


Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

There's probably a few ways to do this.  The way I'd approach it is as follows:

 

Query back all of the records including their created date.

Create a map of integers keyed by date and populate each date in the range with a count of zero, excluding weekend dates.

Iterate the records, and for each record, get the date from the created date and if there is a key present for the date in the map, increment the count.

Finally, iterate the map value list, total them all up and divide by the size of the value list.

 

You could probably shortcut the final step by simply maintaining a total as you go, depending on whether you need the underlying date counts or not.

All Answers

bob_buzzardbob_buzzard

There's probably a few ways to do this.  The way I'd approach it is as follows:

 

Query back all of the records including their created date.

Create a map of integers keyed by date and populate each date in the range with a count of zero, excluding weekend dates.

Iterate the records, and for each record, get the date from the created date and if there is a key present for the date in the map, increment the count.

Finally, iterate the map value list, total them all up and divide by the size of the value list.

 

You could probably shortcut the final step by simply maintaining a total as you go, depending on whether you need the underlying date counts or not.

This was selected as the best answer
bozotheclownbozotheclown

Thanks.  Much appreciated.