Supercharge your RTP Validations with Groovy — Date using Scenario
Validate Dates Like a Pro: Supercharge Your RTP Validations with Groovy
Validating dates entered by users can be extremely useful especially when it comes to CapEx, Workforce, or Project planning.
One common scenario is to ensure that the user-entered date falls within the start and end of the plan duration/window.
Validating the date
Let’s jump straight into the script
/*RTPS: {rtpScenario}, {rtpHireDate}*/
// Create the message bundle
def mbUs = messageBundle (
["DATE.DateEarlier":"The entered date \"{0}\" is earlier than the start date \"{1}\" of the {2} scenario. Please enter a different date and try again.",
"DATE.DateAfter":"The entered date \"{0}\" is after the end date \"{1}\" of the {2} scenario. Please enter a different date and try again."
] )
def mbl = messageBundleLoader(["en" : mbUs])
Member mbrScenario = rtps.rtpScenario.getMember()
String startYear = "20" + ((String)mbrScenario.toMap()["Start Year"]).drop(2)
String startPeriod = (String)mbrScenario.toMap()["Start Period"]
String endYear = "20" + ((String)mbrScenario.toMap()["End Year"]).drop(2)
String endPeriod = (String)mbrScenario.toMap()["End Period"]
Date startDate = new Date().parse("yyyy-MMM-dd", "${startYear}-${startPeriod}-01")
Date endDate = Excel.EOMONTH(new Date().parse("yyyy-MMM-dd", "${endYear}-${endPeriod}-01"), 0)
// validate rtpMember
validateRtp(rtps.rtpHireDate, {!(rtps.rtpHireDate.Date < startDate)}, mbl, "DATE.DateEarlier", rtps.rtpHireDate.Date.format("dd/MMM/yyyy"), startDate.format("dd/MMM/yyyy"), mbrScenario.Name)
validateRtp(rtps.rtpHireDate, {!(rtps.rtpHireDate.Date > endDate)}, mbl, "DATE.DateAfter", rtps.rtpHireDate.Date.format("dd/MMM/yyyy"), endDate.format("dd/MMM/yyyy"), mbrScenario.Name)So we create our message bundle with two messages
one for the date being earlier than the plan start date
the other for the date being later than the plan end date
To validate this, we would need to know what the plan start and plan end dates are.
We’re using the Scenario member (conveniently provided as a RTP, but you could just as well fetch it by name from a Dimension variable).
This can be done in fewer lines (maybe using a closure to get the map properties), but basically, we’re getting the Start Year, Start Period, End Year, and End Period properties that you define for the scenario members.
We are then converting that to a start date and end date, assuming first of the month for the start period, and last day of the month for the end period. After that, the validation is pretty straightforward!
Let’s see what it looks like in action
Wrap up
Effectively, we’ve used an elegant way to block incorrect entries and also learned how we can fetch the member properties and use them. Explore more and see that you can fetch storage, alias, and many other properties.









