Apex : Throwing Custom Exceptions

There will be scenarios when you want to throw an exception from your catch block, may be to propagate to the invoking class so it is handled properly.

Below is a Custom Exception class which can be used to throw different types of exceptions as needed.

public with sharing class CustomExceptions {

public class InputDataException extends Exception {}
public class RequiredException extends Exception {}
public class GovernorLimitException extends Exception {}
public class CommunityException extends Exception{}
public class RecordLockedException extends Exception{}
public class AuthorizationException extends Exception {}

}

From the Apex class: you can use the code below to throw custom exceptions

if( String.isEmpty(case.Analysis__c) ) {
throw new CustomExceptions.RequiredException(‘Analysis is missing from the Case’);
}
else {
throw new CustomExceptions.RequiredException(‘Cases can be assigned by Tier 1 or Admin Users’);
}

Permanent link to this article: https://salesforcebuddy.com/2019/01/apex-throwing-custom-exceptions/

Salesforce Collections : Map from SOQL

There must have been times when creating a map from a SOQL is required. I am showing 2 ways of doing the same.

  1. Map from List ( obtained from SOQL)
    Map<Id, CustomObj__c> myMap = new Map<Id, CustomObj__c>();
    for(CustomObj__c objCS : [Select Id Name,Status__c From CustomObj__c ]){
    myMap.put(objCS.Id, objCS);                                                                                                      }
  2. Map directly from SOQL

Map<Id, CustomObj__c> myMap = new Map<String, CustomObj__c>([Select   Id,Name,Status__c From CustomObj__c ]);

However, if you want to put any other field ( other than Id as key, you will have to iterate over the list and put field in the map like approach #1

Map<String, CustomObj__c> myMap = new Map<String, CustomObj__c>();
for(CustomObj__c objCS : [Select Id Name,Status__c From CustomObj__c ]){
myMap.put(objCS.Status__c, objCS);

Permanent link to this article: https://salesforcebuddy.com/2019/01/salesforce-collections-map-from-soql/

Salesforce Collections – Maps

Maps are pretty useful and can make your code more readable and efficient. In particular, maps can be instantiated in a number of ways.

Here is the standard way of instantiating a map:

Map<Id, Account> accountsById = new Map<Id, Account>();

However, if you happen to have a list of sObjects you can just pass that list in the constructor like this:

Map<Id, Account> accountsById = new Map<Id, Account>([SELECT Id, Name FROM Account LIMIT 10]);

It is easy to work with Maps in Triggers

As you can see, no loops were needed to get a set of account ids. We simply passed the list of accounts to the new map instance and then used the maps keySet() method to get a list of ids for us.

Map<Id, Account> accountsById = new Map<Id, Account>(trigger.new);
Set accountIds = accountsById.keySet();
List contacts = [SELECT Id, FirstName, LastName FROM Contact WHERE AccountId IN :accountIds];

If you have a list of sObjects you don’t have to loop through and get a set of ids. You could just pass that list of sObjects to your query like this (assuming you have a list of account records in a variable called List accounts.

[SELECT Id FROM Contact WHERE AccountId IN :accounts]

Apex is smart enough to know how to get the account ids from the list and to apply them as part of the WHERE clause.

Permanent link to this article: https://salesforcebuddy.com/2019/01/salesforce-collections-maps/