1

I'm working on the #APEX Unit Testing unit on #TrailHead, and I saw what appears to be a very useful Date method, DateWithin30Days(date1, date2) This would be very useful for some of my projects, but I can't find it any where in the APEX documentation.

Here is the code sample:

public static Date CheckDates(Date date1, Date date2) {
    //if date2 is within the next 30 days of date1, use date2.  Otherwise use the end of the month
    if(DateWithin30Days(date1,date2)) {
        return date2;
    } else {
        return SetEndOfMonthDate(date1);
    }
}
  • That's probably just a private method inside that particular class. If it was a Salesforce method, it would have a namespace, like System, Date, etc. Where is this code? – CommonCoreTawan May 24 at 15:27
  • Short answer, no. The lack of prefix to that class tells me that either it's a method on the same class you're on or on a class that your class inherits from. Maybe you can find it within the trailhead you're on? – Sebastian Kessel May 24 at 15:34
4

The method DateWithin30Days(Date 1, Date 2) that you have referred here is not a standard Date method.

It is though still an Apex method but declared right within the class where you see this code. You are most likely looking at this code from trailhead, which you can always utilize for your purpose.

//method to check if date2 is within the next 30 days of date1
private static Boolean DateWithin30Days(Date date1, Date date2) {
    //check for date2 being in the past
        if( date2 < date1) { return false; }

        //check that date2 is within (>=) 30 days of date1
        Date date30Days = date1.addDays(30); //create a date 30 days away from date1
    if( date2 >= date30Days ) { return false; }
    else { return true; }
}
  • I literally just saw this in the class. I was expecting it to be declared at the top, not the bottom. Looks like the same is for SetMonthEndOfDate. – Daniel Roberts May 24 at 15:57
  • It's easy and okay to miss things at times :) – Jayant Das May 24 at 16:14
3

Short answer, no.

The lack of prefix to that class tells me that either it's a method on the same class you're on or on a class that your class inherits from. Maybe you can find it within the trailhead you're on?

However, here is a way to do what you need, with a little more flexibility

public static Boolean DateWithinXDays(Date startDate, Date endDate, Integer days) {
   return (startDate.daysBetween(endDate) <= days)
}
  • 1
    Thanks for the heads up. That method helps, I figured I could write my own, but didn't want to waste the time if it was a standard method. It might be in the trailhead, but my hunch is that they did what you have here. Thanks. – Daniel Roberts May 24 at 15:42
  • Jayant below pasted the code that exists in the Trailhead. Frankly, I kinda prefer this one... – Sebastian Kessel May 24 at 15:44
2

It's part of the sample code for Get Started With Apex Unit Tests. The link to the sample code is located in the Challenge section of the module.

You might want to complete the more recent and more thorough Unit Testing on the Lightning Platform module in lieu of, or in addition to, this module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.