Apex Tips & Tricks

Here are Tips & Tricks that I’ve found useful in Apex.

Get Salesforce Record Ids

There are times when you need to get the set of record ids from a list of SObject records. An easy way to get them is to loop over them and collect them like so:

An easier way using a map:

Request / Response Pattern

The request / response pattern refers to using a “Request” Parameter Object for an apex method’s input and a “Response” Parameter object for what is returned.


  • In managed packages, a global method can’t have its signature changed once the package is released. Additional input or output in newer versions is fairly common so adding new request or response fields is how one can get around having to create additional global methods.
  • No method overloads are needed.

Apex Can Invoke Salesforce APIs

While counter-intuitive, apex can be used to invoke the Salesforce APIs such as the Metadata, Bulk, Partner or Enterprise APIs. This is helpful when you want to stay on the Salesforce platform without having to manage servers and infrastructure off of it.

See Calling Salesforce Web Services Using Apex as an example.

Interfaces Can Extend Interfaces

While Apex started as a scripting language, it evolved into a feature rich, object-oriented language. While used infrequently, one can have an interface extend another interface. This has been helpful in managed packages where new methods are needed on an existing interface. A new interface is created that extends the old one and then used where needed.


It seems that apex developers often forget that Apex has Enums  enumerated values. As a result, it’s fairly common to see code where a string variable is assigned various values and then checked against different values.

Whenever possible, I prefer enums.

What Apex tips and tricks do you have?

Happy Coding,


2 thoughts on “Apex Tips & Tricks”

  1. For the two solutions for getting SObject record IDs, it would be interesting to see which one is the most efficient given a list of 1,000 or even 10,000, 50,000 records. Using the map is definitely easier, and I would assume is also the most efficient (but I’ve never done a test on this myself).

Leave a Reply

Your email address will not be published. Required fields are marked *