Sunday, January 29, 2017

Subtracting or Adding months to current date in Salesforce formula:

Subtracting or Adding days or years to today’s date in Salesforce formula is fairly easy. However, Subtracting/Adding months to a date is slightly more complicated as months vary in length and the cycle of months’ restart with each year. This means, a valid day in one month (January 31) might not be valid in another month (February 31).

Thus, the formula for subtracting or adding months to Today() has to take care of the following factors:
  • Should return March 1 if the future month is a February and the day is greater than 28. This is the same for both leap and non-leap years.
  • Should return the first day of the next month if the future month is April, June, September, or November and the day is greater than 30.

The formula to Subtract months from Today’s date:

You can replace any number of months to the variable <Number_of_Months> to use this formula.

DATE(
    YEAR(TODAY()) +
    FLOOR((MONTH(TODAY()) - <Number_of_Months>) / 12) -
    IF (MOD(MONTH(TODAY()) - <Number_of_Months>, 12) = 0,
        1,
        0),

    MOD(( MONTH(TODAY()) - <Number_of_Months> - 1), 12) + 1,

    1
) + DAY(TODAY()) - 2

 The formula to Add months to Today’s date:

You can replace any number of months to the variable <Number_of_Months> to use this formula.
DATE(
  YEAR(TODAY()) + FLOOR( ( MONTH ( TODAY() ) + <Number_of_Months> - 1 ) / 12 ),
  MOD( MONTH ( TODAY() ) + <Number_of_Months> - 1 +
    IF( DAY (TODAY()) > CASE( MOD( MONTH(TODAY()) + <Number_of_Months> - 1, 12 ) + 1,
      2, 28,
      4, 30,
      6, 30,
      9, 30,
      11, 30,
      31 ), 1, 0 ), 12 ) + 1,
    IF( DAY(TODAY()) > CASE( MOD( MONTH(TODAY()) + <Number_of_Months> - 1, 12 ) + 1,
      2, 28,
      4, 30,
      6, 30,
      9, 30,
      11, 30,
      31 ),
    1, DAY(TODAY())
  )
)



No comments:

Lead/Case assignment rules reassigns the assigned leads/cases from the individual owner to the Queue.

Lead/Case assignment rules reassigns the assigned leads/cases from the individual owner to the Queue. Assignment rules in Salesforce a...