Apex File Naming Standards

Consistent file naming helps keep Visualforce and Apex classes easy to recognize and find. I’ve seen many orgs with both the good and the bad. Below is a table with naming suggestions. Some of them are mine, but most came from a presentation that I found referenced in a Trailhead module. The presentation is by Vivek Chawla. I recommend looking it over because it covers a lot of good stuff. Thank you Vivek.

Apex and Visualforce Naming

  • UpperCamelCase
  • Avoid using underscores.
  • Use full names where possible. Be consistent.
  • Test class should be named for the class they cover, and the word “Test” should be appended to the end.
Functional Type Name Suffix Examples
Trigger Trigger AccountTrigger
Trigger Handler TriggerHandler AccountTriggerHandler
VF Controller Controller AccountCloneController
VF Controller Extension ControllerExt AccountCloneControllerExt
Service Class Services AccountServices
Utility Class Util AccountDupeCatcherUtil
Selector Class Selector AccountSelector
Model / Wrapper Class Varies… Accounts / AccountWrapper
Web Service (SOAP) Ws AccountToolsWs
Web Service (REST) Rest AccountToolsRest
Email Service EmlSvc AccountCreateEmlSvc
Asynchronous (Future) Async AccountIntegrationsAsync
Asynchronous (Batch) Batch AccountMigrationBatch
Scheduled Apex Job AccountCleanupJob
Queueable Apex Que AccountSyncingQue
Visualforce Page -none- AccountClone
Visualforce Component Cmp AccountCloneCmp
Lightning Components -TBD-
Lightning Pages -TBD-

Workflow Rule, Field Update, and Email Alert Naming

Workflow Rules and their actions can also get unruly without consistent naming. Force365 published some good suggestions. Below are the ones that I refer to most.

Workflow Rule
[Object]: [Criteria Description i.e. Condition]

Workflow Actions
Field Update –
Set [Field] to [Value]
Email Alert –
Send [Template short description]
Task –
[Task Subject]

References

SlideShare: Salesforce Development Best Practices by Vivek Chawla
Blog: Force365 – Salesforce Naming Conventions – Declarative

 

Remove Last Comma or Other Characters from the End of a String

Apex

Removing the last comma is often needed when dynamically building a SOQL Select string. Salesforce quietly released a bunch of new string methods in Winter ’13 that I have started to really appreciate. One of the new methods is removeEnd() and removeEndIngoreCase(). These are great for removing that last comma.

If your data structure is a simple list of strings, you don’t even need the removeEnd(). The String.join() will form the string without an ending comma.

Javascript

Here is how to remove the last comma of a string variable in Javascript.

Salesforce Describe Methods – Record Types, Fields, and Global Describe

Record Type Describe

Getting an object’s record type Id is simple with the statement below, and it avoids using a SOQL statment on the RecordType object. However, the method name is misleading because getRecordTypeInfosByName actually retrieves the record type with the Label and not the Name. Also, be careful because the name is case sensitive. A Null Pointer Exception will be thrown if the label does not match exact spelling and case, so add appropriate error handling.

Continue reading

In-line Editing Visualforce Component for Any Object

This is a Visualforce component for in-line editing of an object’s child or related records. It will work for most Salesforce standard and custom objects. Pass in parameters to change what is displayed and if the user can add, edit, or delete records.

In-line Editing Visualforce Component for any object

Required Parameters

  • aParentRecId: Parent Record Id. This is used to select the related object’s records.
  • aRelationField: Field that refers back to the parent. This field is used in the query where condition with the Parent Record Id.
  • asObjectType: Type of child Object.
  • aFieldList: List of fields to display in the pageBlockTable.

Optional Parameters

  • aAllowAdd: Ability to add new records. The Add button will display.
  • aAllowEdit: Ability to edit records. The Edit link will display.
  • aAllowDelete: Ability to delete records. The Del link will display.
  • aLabelOverrideFieldList: List of fields with overridden labels.
  • aLabelOverrideTextList: List of text that overrides the field labels.
  • aDefaultValueFieldList: List of fields used to set default values on added records.
  • aDefaultValueTextList: List of text used to set default values on added records.
  • aBlockTitle: Page block title text.

Continue reading

Visualforce Table with Alphabet Filter and Sortable Columns

This Visualforce page contains apex:pageBlockTable to demonstrate a few different concepts.

  • Sortable Columns in an apex:pageBlockTable
  • Alphabet Filter that filters on the current sorted column
  • Use of the ApexPage StandardSetController
  • Selecting an individual record in the table using a link
  • Selecting multiple records with a column of checkboxes using a Sub Class(Wrapper Class) to hold the boolean value.

Both single and multiple record selection were included for demonstration. Most actual projects would only require one type, and I thought it would be easy to cut whatever was not needed. The search for the Billing Address fields was consolidated into one field to make the interface simpler. Below is an example of what the Visualforce table looks like with the alphabet filter, sorted columns, address search, and checkbox record selection.

Sortable apex:pageBlockTable, alphabet filter, checkbox selection and link selection
Continue reading