• About
  • Contact me!

Salesforce Everywhere

~ Blog on Salesforce Dev and admin stuff

Salesforce Everywhere

Category Archives: Adding months to a date using Salesforce formula

Adding months to a Date field

19 Wednesday Aug 2015

Posted by Mani in Adding months to a date using Salesforce formula, Salesforce Configuration

≈ Leave a comment

Tags

Adding months

Requirement:

In my current project, based on Commencement Date of Resource, I need to send out email notification to several departments(HR, Payroll etc) and I need to do it if Resource completes 6, 12, 24 and 36 months. Basically, I had to add 6, 12,24 and 36 months to Commencement Date field and based on that date, I need to trigger Workflow notification(Time Trigger).

Analysis:

Salesforce help site https://help.salesforce.com/HTViewHelpDoc?id=formula_examples_dates.htm has a formula and it is as follows, but unfortunately it throws compilation error(Compiled formula is too big to execute (5,503 characters). Maximum size is 5,000 characters) when tried:

IF(
MOD( MONTH( Commencement_Date__c ) + 6, 12 ) = 2,
IF(
DAY( Commencement_Date__c ) > 28,
DATE( YEAR( Commencement_Date__c ) + FLOOR( ( MONTH( Commencement_Date__c ) + 6 ) / 12 ), 3, 1 ),
DATE( YEAR( Commencement_Date__c ) + FLOOR( ( MONTH( Commencement_Date__c ) + 6 ) / 12 ), 2, DAY( Commencement_Date__c ) )
),
IF(
OR(
MOD( MONTH( Commencement_Date__c ) + 6, 12 ) = 4,
MOD( MONTH( Commencement_Date__c ) + 6, 12 ) = 6,
MOD( MONTH( Commencement_Date__c ) + 6, 12 ) = 9,
MOD( MONTH( Commencement_Date__c ) + 6, 12 ) = 11
),
IF(
DAY( Commencement_Date__c ) > 30,
DATE( YEAR( Commencement_Date__c ) + FLOOR( ( MONTH( Commencement_Date__c ) + 6 ) / 12 ),
MOD( MONTH( Commencement_Date__c ) + 6, 12 ) + 1, 1 ),
DATE( YEAR( Commencement_Date__c ) + FLOOR( ( MONTH( Commencement_Date__c ) + 6 ) / 12 ),
MOD( MONTH( Commencement_Date__c ) + 6, 12), DAY( Commencement_Date__c ) )
),
IF(
MOD( MONTH( Commencement_Date__c ) + 6, 12 ) = 0,
DATE( YEAR( Commencement_Date__c ) + FLOOR( ( MONTH( date ) + 6 ) / 12 ) – 1, 12, DAY( Commencement_Date__c ) ),
DATE( YEAR( Commencement_Date__c ) + FLOOR( ( MONTH( Commencement_Date__c ) + 6 ) / 12 ),
MOD( MONTH( Commencement_Date__c ) + 6, 12), DAY( Commencement_Date__c ) )
)
)
)

Solution:

Thanks to Chris (http://www.salesforceweek.ly/author/chris) and the link http://www.salesforceweek.ly/2015/01/how-to-formula-add-months-to-date.html gives a good post about adding months to a Date.

As per the above link, I tried the following formula which adds n months(6,12, 24 and 36 month) works perfectly fine(including leap years) where n means :

DATE (  /*YEAR*/  

YEAR(Commencement_Date__c) + FLOOR((MONTH(Commencement_Date__c) + n - 1)/12), 

 /*MONTH*/  
 
CASE(MOD(MONTH(Commencement_Date__c) + n, 12 ), 0, 12, MOD(MONTH(Commencement_Date__c)+ n, 12 )),  

/*DAY*/  

MIN(DAY(Commencement_Date__c),  CASE(MOD(MONTH(Commencement_Date__c) + n,12), 9, 30, 4, 30, 6, 30, 11, 30, 2,  

/* return max days for February dependent on if end date is leap year */  

IF(MOD(YEAR(Commencement_Date__c) + FLOOR((MONTH(Commencement_Date__c) + n)/12), 400) = 0 || (MOD(YEAR(Commencement_Date__c) + FLOOR((MONTH(Commencement_Date__c) + n)/12), 4) = 0 && MOD(YEAR(Commencement_Date__c) + FLOOR((MONTH(Commencement_Date__c) + n)/12), 100) <> 0  ), 29,28), 31))  )

Please let me know your comments as always.

Advertisement

Blog Stats

  • 114,773 hits

Archives

Recent Posts: Salesforce Everywhere

How to get field values on onLoad in lightning-record-edit-form in LWC?

Using Dynamic Actions

How to check if the logged in user is a Guest user or not in APEX class?

How to Close Quick action in LWC

Lightning component to filter Products and display results in lightning data table

Connect with me

  • View @manibalan_s’s profile on Twitter
  • LinkedIn

Developer links

  • Salesforce Developer website
  • Salesforce Success Community

Enter your email address and click on Follow us (below) to follow this blog and receive notifications of new posts by email.

Website Powered by WordPress.com.

  • Follow Following
    • Salesforce Everywhere
    • Already have a WordPress.com account? Log in now.
    • Salesforce Everywhere
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar