Here is a collection of little tidbits I’m noticing while learning Apex. I’ve just started this post, so there’s not much here yet, but I’ll add to it as I can.
Declaring and Instantiating List Variables and Bracket Notation
There are two ways to declare a List variable:
- List<Account> acctList;
- Account[] acctList;
I happen to love the bracket notation. Just two little keystrokes and you’re done (as opposed to an extra four with “List<>” notation).
But as I was hobbling along it seemed that brackets were supremely frowned upon used to the right of the variable name when instantiating a new list: acctList = new Account[]; gives the compiler fits!
So up until now I’ve been forced to use all those extra key strokes: acctList = new List<Account>();
But I have happily just learned from the profoundly Salesforce-savvy, Doug Ayers, that there is some hope for the brackets to the right of the variable after all!
Here are some of the ways listed in this article, Apex Code: The Basics, that had previously escaped my searches on how brackets can be used:
Account[] accs2 = new Account[]{};
Account[] accs3 = new Account[]{new Account()};
Account[] accs4 = new Account[4];
Integer[] ints2 = new int[]{1, 2, 3};
Integer[] ints3 = new int[6];
See all those lovely brackets to the right of the variable? I’m doing my happy dance right now!
The trick to finding this cool info, it seems, is to know that Lists and Arrays are the same thing with only one slight difference note: an Array is a one-dimensional List only, whereas a List can be one- or multi-dimensional:
String[] stringArr == List<String> stringArr <– these are 1-dim arrays
List<List<String>> <– this is a 2-dim list for which there isn’t an equivalent bracket (array) notation.
I think I’ll write a little more about this later.
Compound Address Fields – Can They Be Passed Into a Method?
It’s pretty nifty that all the address fields on standard objects can be accessed in SOQL with a single word: ShippingAddress, MailingAddress, etc. I ran across a situation, however, that has completely stumped me, and after six hours of monkeying around trying various solutions I finally threw in the towel.
Here is what happened:
I wrote a little utility to see if two addresses were the same (this is the bare-bones…it needs to check for null variables to be more robust):
The idea was that it would examine the latitude and longitude of the address fields and let the calling routine know if they matched or not.
It worked in my test routine just fine:
But I couldn’t get it to work in a Trigger no matter how hard I tried and what I tweaked. Over and over again the dreaded FATAL_ERROR – Attempt to de-reference a null object.
I even assigned the field to a System.Address variable and tested it with the same result.
System.debug(addressVar == null) and System.debug(acct.ShippingAddress == null) always yielded true, even when the address was fully populated.
That leaves me wondering, do compound address fields in a Trigger record always equate to null?
There must be a trick here, it just hasn’t posed itself to me just yet.
So to work around the problem I had to skip using my handy-dandy utility method and test for same addresses in the Trigger helper class itself:
This is a head scratcher and I’d really like to know what is going on, so if you know, enlighten me please!!
More soon…