Posts

Showing posts from October, 2021

Use of Safe Navigation Operator

Safe Navigation Operator Instead of using explicit, sequential checks for null references, use the safe navigation operator (?.). This new operator short-circuits expressions that attempt to operate on a null value and returns null instead of throwing a NullPointerException. NOTE: All Apex types are implicitly nullable and can hold a null value returned from the operator. Example - Account acc = [SELECT Name FROM Account WHERE Id = :accId]; if (acc != null) {     return acc.Name; } Using Safe Navigation Operator String accName = acc?.acc.Name; String accName =  [SELECT Name FROM Account WHERE Id = :accId]?.Name; The Safe Navigation Operator (?.) in Apex is a feature that allows developers to safely navigate objects and data structures, avoiding null pointer exceptions. When you use the Safe Navigation Operator to access a chain of references, Apex will evaluate the expression from left to right and return null immediately if it encounters a null reference rather tha...

Determine whether the Salesforce organization is a Sandbox or a Production

To determine if a Salesforce organization is a sandbox or a production environment in Apex. Salesforce's sObject Organization has a field called IsSandbox, which is true for sandboxes and false for Production. Boolean isSandbox = UserInfo.getIsSandbox(); Organization org = [Select Id, Name, IsSandbox from Organization]; if (org.isSandbox) {     System.debug('The organization is a sandbox.'); } else {     System.debug('The organization is a production environment.'); } You can use this check to conditionally execute logic based on whether the Apex code is running in a sandbox or production environment. This can be useful for implementing different behaviors or configurations specific to each environment. In Salesforce, the Organization object represents metadata and settings for a Salesforce organization. It provides information about the organization's configuration, such as its name, address, edition, features, limits, and more. The Organization object is rep...