-3

While implementing below approach to calculate working days excluding weekends, I am also trying to exclude Holidays that I've added to salesforce org as well. Is it possible to add few resources and elements in the same flow to implement it?

2 Answers 2

1

I would do it with a batch apex and a custom field of type number number_working_days__c.

That way you can schedule it to run only on worked days (and avoid week-ends) by using the correct cron expression to schedule it with the System.Schedule method

The batch would query the database for records matching your conditions : status not equal to your list and not completed as well.

Then you just add 1 to your custom field and the batch then update all records with the newly incremented counter.

Finally, as long as the status is completed or one of the value in your list, the number is not incremented and you have your information available on each record.

documentation for batch apex : Batch Apex

documentation for scheduling and cron expression examples : Apex Scheduler

Flow approach :

use a Schedule-Triggered flow

Scheduled triggered flow

define a schedule Scheduling

define filtering criterias : exclude all the status values in your list + the completed status filtering criterias

Then increase the custom field number_of_working_days__c by 1 and Update the record. enter image description here

To test if it's a week day (Monday to Friday) or the weekend (Saturday or Sunday) create a flow variable of type Number as below enter image description here

Then add a decision at the beginning of the flow to test if it's weekend or not, if it's weekend just stop the flow, else let the flow logic run. enter image description here

8
  • Hey! Thanks for your effort. If possible help me with Flow because I am pretty new to Batch.
    – SBS
    Commented Jan 4, 2023 at 16:59
  • ok updating my answer
    – Chris
    Commented Jan 4, 2023 at 17:21
  • if you want to not take into account the week-ends then you need to add additional logic in the flow to test if current day is Saturday or Sunday, but it's not difficult.
    – Chris
    Commented Jan 4, 2023 at 17:31
  • Great effort Chris!! But the thing is Business user wants to see the status every now and then. Actually the purpose here is to Calculate SLA like whats the status of the service created and whether its nearing SLA timeline days. Can we achieve that in Record triggered flow? Thanks in Advance.
    – SBS
    Commented Jan 5, 2023 at 5:22
  • Well it depends the level of precision you require, with my proposition you can display the field number of working days to business users, they can see it all the time in the record page layout. Now this field gets recalculated once per day, maybe that's enough for your requirement ?
    – Chris
    Commented Jan 5, 2023 at 7:42
0

The "stop calculating when X, and resume when Y" bit means that you cannot do this with a formula field.

Formulas are evaluated every time the record is accessed, and they do not hold the result of evaluating the formula.

Workflow rules are also being phased out. It doesn't matter what your preference is here, you need to use a Flow or Apex to achieve this.

1
  • Thanks for your Response! I did some research and came to know the same. Can you help me to understand better how we can achieve this using Flow? Thanks in Advance.
    – SBS
    Commented Jan 4, 2023 at 12:46

Not the answer you're looking for? Browse other questions tagged .