Share the Love......
salesforce apex idioms

Common Salesforce Apex Idioms and Patterns : Part 1

In this series of blog posts I will try to list out the common Apex Idioms and Design Patterns that we all as Salesforce Developers need to use from time to time. We tend to google these idioms out or try to refer to Salesforce developer forums or stackexchange to find the right code idiom that we can use to solve our particular technical issue.

I will try to list out as many as possible in subsequent posts.

[table width =”100%” style =”” responsive =”false”] [table_head] [th_column]Index[/th_column] [/table_head] [table_body] [table_row] [row_column]Create a Map of Id to List of Ids[/row_column] [/table_row] [table_row] [row_column]Create Test User[/row_column] [/table_row] [table_row] [row_column]Get RecordtypeId without SOQL query[/row_column] [/table_row] [table_row] [row_column]Create Community Users[/row_column] [/table_row] [table_row] [row_column]Get fields from a FieldSet and use them in a SOQL query[/row_column] [/table_row] [table_row] [row_column]Describe Information of a sObject or Getting the attributes of a sObject[/row_column] [/table_row] [table_row] [row_column]Describe Information of a field or Getting the attributes of a field[/row_column] [/table_row]
[/table_body] [/table]
  1. Create a Map of Id to List of Ids

This is a common idiom used whenever you have multiple items which has further items associated with it. An example is shown here. Another good example would be Accounts and their Contacts.

2. Create Test User

This is another idiom which is mostly copied and pasted in test classes where you need to create a Test User to run the Test code in a particular user’s context. You should always do this as testing with different user contexts in test classes accounts for robust code and good coding practices.

3. Get RecordtypeId without SOQL query

In Summer 14 Release, the limits on Describe Calls were removed. Now it’s better to get RecordtypeId for an Object via describe call rather than wasting SOQL queries.

4. Create Community Users

This is something that used to get me running to stackexchange every time and again, until I finally came up with this code and saved it. There are a couple of nuances to creating multiple Community Users on the basis of Contacts in Apex like Alias, NickName, User fields. This particular Idiom will help you immensely whenever you need to create Community users either in a Trigger or Batch Class.

If you are a little confused with why the user creation code is future annotated, then check this out


5. Get fields from a FieldSet and use them in a SOQL query

Sometimes you need to refer a FieldSet and its fields in a SOQL query. Here’s a short and simple code to do it.

6. Describe Information of a sObject or Getting the attributes of a sObject

The example here is for Account object but you can use any object

7. Describe Information of a field or Getting the attributes of a field

The example here is for Account Name field but you can use any field


Stay tuned. More Tips to come. Please subscribe to this blog so you don’t miss on any updates.

[well type=””]

About the Author Atul Gupta

Leave a Comment: