0

I need calculate only workdays for DUE DATE field which uses this existing formula -

IF(
    ISNULL(DATEVALUE(DATETIMEVALUE(NewCaseDate__c))), 
    DATEVALUE(CreatedDate), 
    DATEVALUE(DATETIMEVALUE(NewCaseDate__c))
) + Days_To_Respond__c"

I have found a formula like this but it throws error

Error: Incorrect parameter type for function 'MOD()'. Expected Number, received Date/Time

Also, formula return type will be DATE and we can't change it.

DATEVALUE(CreatedDate) + 
FLOOR(
    (Days_To_Respond__c - 
    IF(
        MOD(CreatedDate, 7) = 5, 
        2, 
        IF(
            MOD(CreatedDate, 7) = 0, 
            1, 
            0
        )
    )
    ) / 5
) * 7 + 
MIN(
    Days_To_Respond__c - 
    IF(
        MOD(CreatedDate, 7) = 5, 
        2, 
        IF(
            MOD(CreatedDate, 7) = 0, 
            1, 
            0
        )
    ), 
    5
)

3 Answers 3

0

MOD(CreatedDate, 7) doesn't work, because you can't divide a date by a number. However, just choose a date that is a Sunday, and subtract that from the CreatedDate.

MOD(CreatedDate - DATE(1970, 1, 4), 7)
2
  • Thank you! Would you please elaborate the formula, should i use this 'MOD(CreatedDate - DATE(1970, 1, 4), 7)' everywhere where MOD is written?
    – Sonu
    Commented Jul 16 at 16:04
  • @Sonu Yes, you'd use that in each instance.
    – sfdcfox
    Commented Jul 16 at 18:38
0

The issue is MOD() function expects a number, but you're providing a date/time value.

You can try this formula:

DATE(
        YEAR(CreatedDate),
        MONTH(CreatedDate),
        DAY(CreatedDate) +
        FLOOR((Days_To_Respond__c + 
                CASE(MOD(DATEVALUE(CreatedDate) - DATE(1900, 1, 7), 7),
                        0, 1,
                        6, 2,
                        0
                )
        ) / 5) * 7 +
        MIN(
                MOD(Days_To_Respond__c + 
                        CASE(MOD(DATEVALUE(CreatedDate) - DATE(1900, 1, 7), 7),
                                0, 1,
                                6, 2,
                                0
                        ), 5),
                5
        )
)

This DATE(YEAR(CreatedDate), MONTH(CreatedDate), DAY(CreatedDate)) part of the formula calculates the base date.

This FLOOR((Days_To_Respond__c + CASE(...)) / 5) * 7 part of the formula adds the number of workdays.

This MIN(MOD(Days_To_Respond__c + CASE(...), 5), 5) part of the formula adjusts for weekends.


Updated:

I have updated the formula as per your comment to consider the NewCaseDate__c field when it is populated.

DATE(
        YEAR(IF(ISBLANK(NewCaseDate__c), CreatedDate, NewCaseDate__c)),
        MONTH(IF(ISBLANK(NewCaseDate__c), CreatedDate, NewCaseDate__c)),
        DAY(IF(ISBLANK(NewCaseDate__c), CreatedDate, NewCaseDate__c)) +
        FLOOR((Days_To_Respond__c +  
                CASE(MOD(DATEVALUE(IF(ISBLANK(NewCaseDate__c), CreatedDate, NewCaseDate__c)) - DATE(1900, 1, 7), 7),
                        0, 1,
                        6, 2,
                        0
                )
        ) / 5) * 7 + 
        MIN(
                MOD(Days_To_Respond__c + 
                        CASE(MOD(DATEVALUE(IF(ISBLANK(NewCaseDate__c), CreatedDate, NewCaseDate__c)) - DATE(1900, 1, 7), 7),
                                0, 1,
                                6, 2,
                                0
                        ), 5),
                5
        )
)

This formula checks if NewCaseDate__c is populated. If it is, it uses NewCaseDate__c as the base date. otherwise, it uses CreatedDate. The rest of the formula remains the same, only workdays are counted when adding Days_To_Respond__c.

4
  • Hi Tushar, this worked, but i forgot to add one more field, i.e., "new case date" This formula should also work when I populate 'new case date' then the formula should consider new case date + days to respond
    – Sonu
    Commented Jul 16 at 16:10
  • @Sonu, What is the data type of the NewCaseDate__c field and What is the datatype of the Days_To_Respond__c field? Commented Jul 17 at 4:28
  • data type of NewCaseDate__c is 'Date' & Days_To_Respond__c is 'Number' where 8 is the default value
    – Sonu
    Commented Jul 18 at 16:32
  • @Sonu, I have updated my answer as per your comment. You can check and let me know if it works for you. Commented Jul 19 at 4:43
-1

try something like this

public static Date calculateDueDate(Date startDate, Integer daysToRespond) {
Integer daysToAdd = 0;
while(daysToAdd < daysToRespond) {
    startDate = startDate.addDays(1);
    if(!(startDate.format('E').equalsIgnoreCase('Sat') || startDate.format('E').equalsIgnoreCase('Sun'))) {
        daysToAdd++;
    }
}
return startDate;

}

1
  • 1
    Frame challenges can be good, but the responsibility lies with you to explain why another method should be considered. Without that, your answer (apex code) isn't very useful to someone trying to fix an issue in a formula field.
    – Derek F
    Commented Jul 15 at 15:19

You must log in to answer this question.

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