Never give up! Never Surrender! (or how perseverance is important in Apex [or any] coding)

Fabulous New Salesforce Trailhead Badges!I am totally and utterly thrilled to announce that I have written Apex and it worked! It actually did what I meant it to do! Thanks to Salesforce Trailhead, I have achieved a major goal and written my first Apex trigger. Phew! What I high I have.

I have to say, that the biggest lesson from my experience with CFML coding (ColdFusion Markup Language), is to persevere and never give up on finding the answer.  It’s such a huge deal in my mind, and an absolute must for anybody wanting to get into coding (which is so darn picky about every little keystroke), that I wanted to blog about it.

I don’t know about you, but if I’m working on exercises from a book or from an online class, and I can’t get something to work the way it should, I have a tendency to think they got it wrong. I know…pretty lame, but I know a lot of publisher-type people, and I know that there are always little typos here and there, even in the most carefully copy-edited text. So I just assume that this is one of those times. And, how bad is that, that they had to make a mistake when I’m trying to learn here! But generally, once I give up, and look at the solution, I see that, “ooops,” no, it wasn’t them, it was me :(. I would then pound my head and bemoan, “Why, oh why, didn’t I try wait a little longer before giving up?!” (Yes, I am a little dramatic, but that’s part of my charm. Right?)

So fast forward to last night… I was tackling the first challenge in the Apex Basics & Database module. To complete the challenge I needed to:

Create an Apex class that returns an array (or list) of formatted strings (‘Test 0’, ‘Test 1’, …). The length of the array is determined by an integer parameter.

  • The Apex class must be called ‘StringArrayTest’ and be in the public scope.
  • The Apex class must have a public static method called ‘generateStringArray’.
  • The ‘generateStringArray’ method must return an array (or list) of strings. Each string must have a value in the format ‘Test n’ where n is the index of the current string in the array. The number of returned strings is specified by the integer parameter to the ‘generateStringArray’ method.

So that’s what I did, and it worked just fine. I had named both the array and the method correctly, and through System.Debug and then inspecting the debug log, the resulting array had the correct length. But #@%$! Trailhead kept denying me! I would click the damn “check challenge” button over and over (like the nut I am, expecting different results each time) and picky, picky Trailhead would give me the same error message. (Don’t worry TH, you’re still totally my BFF!)

Trailhead was telling me that my method either didn’t exist, wasn’t public, wasn’t static or didn’t return the correct length of array! I was ready to give up…I was once again thinking, “Yep, they made a mistake, and now I don’t get my points and my pretty little badge! Pooh!” (I may have even stamped a foot once…maybe twice.)

But I didn’t give up and it turns out I had missed a very small, but very important piece of wording about the returned value. Actually I did see it, and I obsessed over what they meant by “method must return a value,” and had done a bunch of Googling, but to no avail. But I kept remembering my lessons about giving up (just don’t do it!). I tried yet one more different way to search for “apex class return array,” and like a little ray of sunlight on a cloudy day, I found a post that helped! In fact it helped so much that I felt a little guilty for reading it. But whatever! I had 99.9% of the code right and I choose to believe that Googling isn’t cheating — it’s research!

For those of you who are getting stuck at the same place I was, I’ll give a little (teeny tiny) hint. The operative piece for me was:

‘generateStringArray’ method must return an array (or list) of string

To do that, you use the syntax for the method that begins with “public static List<string>” etc.

I hope I’m not giving too much away, but seriously, it took me ages to find somewhere that referenced how to return an array, and it didn’t help that there was documentation all over that system.debug “returns” things…just not in the way that I needed to pass the challenge! I didn’t want any other potential Woman Code Hero (or man) to give up like I felt like doing. Hence this little (or not so) post.

So here I am, thrilled to announce that, yes, I have two more badges from Trailhead! And they shine the brightest of them all, because I worked bloody hard for them! I actually think that I incorporated more of the learning because of it was so challenging. And it was oh so sweet when I got that lovely little “Congratulations” window! I did a little happy dance right there in my living room. I even called my wife to tell her the good news, “yes, dear, that’s really wonderful. I have no idea what It means, but, ‘Great!'” It’s really too bad that most the people in my work-a-day or personal life have no idea of the meaning of this blessed event.  Oh well…that’s what I have you for!

So thank you all for sharing in my moment of utter glory, and I hope to report more success soon. Next module is on creating Test Coverage, which as we all know at the Holy Grail of Apex because it means you get to deploy your code!!! Ohhh, then I’ll really be dangerous!

My Shiny New Badges!