+ Start a Discussion
Cody DrennonCody Drennon 

Is there a way to validate date based on month and year picklists

Hello, 
So i have a user interface that has a month picklist and a year picklist.  Each one is hardcoded with months and years and each one has an Id.  There are only 4 years in the years picklist and all 12 months in the month picklist.  My questions is, can these be validated for example, make sure that the end date and no greater than one year after the start date.  I have been trying to figure out a way to do this for over a day now and just cant seem to figure out any logic for this.  If anyone could please let me know if this is even possible i would greatly appreciate it.  Thank you.  This is using a custom apex page, and angular2 font end.
Best Answer chosen by Cody Drennon
Gururaj BGururaj B
Create 2 formula(date forumula field) fiels for start date and end date(here keeping the date as 01 for both the dates i assume you have only month and year for you dates) 
The formula field should be like this:
DATE( value(TEXT(Year__c )) ,IF( ISPICKVAL(Month__c  , "Jan"), 01, IF( ISPICKVAL(Month__c  , "Feb"), 02, IF( ISPICKVAL(Month__c  , "Mar"), 03, IF( ISPICKVAL(Month__c  , "Apr"), 04, IF( ISPICKVAL(Month__c  , "May"), 05, IF( ISPICKVAL(Month__c  , "Jun"), 06, IF( ISPICKVAL(Month__c  , "Jul"), 07, IF( ISPICKVAL(Month__c  , "Aug"), 08, IF( ISPICKVAL(Month__c  , "Sep"), 09, IF( ISPICKVAL(Month__c  , "Oct"), 10, IF( ISPICKVAL(Month__c  , "Nov"), 11, 12 ) ) ) ) ) ) ) ) ) ) ),01)

Then create a validation to compare the start and end date and throw appropriate error message.

If this works please mark this as best answers..

All Answers

Gururaj BGururaj B
Create 2 formula(date forumula field) fiels for start date and end date(here keeping the date as 01 for both the dates i assume you have only month and year for you dates) 
The formula field should be like this:
DATE( value(TEXT(Year__c )) ,IF( ISPICKVAL(Month__c  , "Jan"), 01, IF( ISPICKVAL(Month__c  , "Feb"), 02, IF( ISPICKVAL(Month__c  , "Mar"), 03, IF( ISPICKVAL(Month__c  , "Apr"), 04, IF( ISPICKVAL(Month__c  , "May"), 05, IF( ISPICKVAL(Month__c  , "Jun"), 06, IF( ISPICKVAL(Month__c  , "Jul"), 07, IF( ISPICKVAL(Month__c  , "Aug"), 08, IF( ISPICKVAL(Month__c  , "Sep"), 09, IF( ISPICKVAL(Month__c  , "Oct"), 10, IF( ISPICKVAL(Month__c  , "Nov"), 11, 12 ) ) ) ) ) ) ) ) ) ) ),01)

Then create a validation to compare the start and end date and throw appropriate error message.

If this works please mark this as best answers..
This was selected as the best answer
daofu hudaofu hu
I got it