Apex Datetime Method: Using newInstance(Date, Time)

I had a very challenging few minutes this morning around the Apex Datetime Method that takes two parameters; a Date and a Time.  I was trying to fix a Visualforce “report” I had built that takes start and end date parameters from date fields on the Visualforce page.  Well, certain records were being excluded from the reports if they happened on the last day of the date range, so I wanted to get the first second of the first day and the last second of the last day into the incoming parameters for my report date range.

I went to the Salesforce Apex Datetime Methods page, and saw that there was a newInstance method on the Datetime class that does exactly what I wanted; accepts a date and time, and yields a Datetime.  So, I started with something like this:

Datetime reportStartDateTime = datetime.newInstance(reportStartDate, ’00:00:00′);

Datetime reportEndDateTime = datetime.newInstance(reportEndDate, ’23:59:59′);

I tried several different versions of the time value, but nothing was working.  And, worst of all, I was getting a weird error message from Eclipse (Kepler 4.3) and/or the Force.com IDE plugin (I’m in version 31 at this point):

Compilation error: Variable does not exist: datetime

 So, I started doubting if I was using a valid datetime method, and looking for missing semicolons and such.  But, I finally realized that I just wasn’t supplying the time correctly.  Here’s what worked:

Datetime reportStartDateTime = datetime.newInstance(reportStartDate, Time.newInstance(0,0,0,0));

Datetime reportEndDateTime = datetime.newInstance(reportEndDate, Time.newInstance(23,59,59,59));

So, the trick seems to be using the newInstance method of the Time class to get a valid time in there, rather than a string.  I’m guessing there might be a string literal one CAN use to make this work, but I’m leaving that for another day, as it didn’t jump out at me in the documentation, and I’ve got code to commit on deadline…

Salesforce: Automatically Creating an Apex Map from a List with ID of SObject as the Key

I recently learned something that is already saving me a ton of time.  I didn’t know it was possible to take a list of SObjects, and create a Map<Id, SObject> without writing an explicit loop to traverse the list.  But, Apex developers, we can do just that with the following map constructor:

Map<Id, SObject>(List<SObject>)

So, let’s say I’d queried up a list of Account records, as so:

List<Account> myAccountList = [ SELECT Name, AccountNumber FROM Account LIMIT 50 ];

What I had been doing previously to create a Map<Id, SObject> was this:

Map<Id, Account> myAccountMap = new Map<Id, Account>();

for(Account a : myAccountList) {
     myAccountMap.put(a.Id, a);
}

Granted, that works.  But, how much EASIER is this?

Map<Id, Account> myAccountMap = new Map<Id, Account>(MyAccountList);

You can read more about this in the Salesforce documentation on the Apex map class, specifically here.

I also think I read something about the idea in Dan Appleman’s book, “Advanced Apex Programming for Salesforce.com and Force.com..”  If you want to be a better Apex programmer, pick up a copy of that book.  There on links on the book’s website for purchasing a copy.

Salesforce Advanced Developer Quest: Multiple Choice Passed!

Well, I passed the Salesforce Advanced Developer Certification multiple choice exam on my second attempt!  I’m really excited about it.  That actually happened back on May 28, but I haven’t exactly found my stride in blogging regularly.

Some of my colleagues have asked me what made the difference between my first attempt at the exam and my second.  I almost want to apologize to anyone who might read this for what I’m about to say, but I think it’s the truth:  The thing that made the difference was that I started reading the Force.com Apex Code Developer’s Guide straight through.  I didn’t get quite to the end before I took the test again, but I got most of the way through.  I really think that’s what made the difference.  It helped me answer some questions about Apex web services, email templates, etc., that I just hadn’t had an opportunity at work to do much with.  So, for those of you out there in the world working toward that DEV501 certification, my advice is, “Read the Apex Code Developer’s Guide.”

I was also very excited when I got into a programming assignment window faster than expected!  It sounds like I’m going to get my programming assignment on September 29, 2014.  I’ll then have about 3 weeks before I’m schedule for the essay exam.  That was the longest I could give myself with the scheduling options available at the proctoring location I use (University of Louisville, Brandeis Hall, Testing Center).  For some reason, I’ve never really wanted to figure out the requirements for the home proctoring…call me paranoid if you like, but it feels just a little too much like Big Brother would be watching me in my home office (leave me a comment if you think I’m crazy for that, or if you can identify…I’m curious about how other folks feel on the subject).  I also hope that the shorter deadline will keep me from procrastinating until the very last minute, and I’ll have several days after the essay exam / defense of my solution to finish things up before submitting the assignment.

I was lucky enough to get to attend Dreamforce in November of 2013 in San Francisco.   One thing I heard during the sessions about working toward the Salesforce Advanced Developer certification was that you needed to write good test classes, and pay attention to the testing best practices in the Apex documentation.  So, I plan to study up on test class/method best practices between now and when my programming assignment window opens.

I’ll post about my experience with the programming assignment as I go along.  I won’t be able to write anything specific about the assignment due to the non-disclosure requirements of the Salesforce certification program.  But, I’ll write up some general comments about how much time I end up investing, challenges I face, etc.  My hope is that by sharing some of my experience, I’ll be able to encourage some of my colleagues and network to pursue this credential as well.  I’ve really learned a lot already just preparing for the multiple choice, and I think I’m a better Apex / Visualforce  developer as a result.

Wish me luck!