Wednesday 2 April 2014

Exceptions in Salesforce

DML Exceptions

Any problem with a DML statement, such as an insert statement missing a required field on a record.

ListException

Any problem with a list, such as attempting to access an index that is out of bounds.

  1. try
  2. {
  3.     List<Integer> li = new List<Integer>();
  4.     li.add(15);
  5.     // This list contains only one element, but we're attempting to access the second element from this zero-based list.
  6.     Integer i1 = li[0];
  7.     Integer i2 = li[1]; // Causes a ListException
  8. }
  9. catch(ListException le)
  10. {
  11.     System.debug('The following exception has occurred: ' + le.getMessage());
  12. }

Debug log will be: The following exception has occurred: List index out of bounds: 1 .

NullPointerException

Any problem with dereferencing a null variable.

Try out some code that does some things on purpose to cause this exception to be thrown. Execute the following:

  1. try
  2. {
  3.     String s;
  4.     Boolean b = s.contains('abc'); // Causes a NullPointerException
  5. }
  6. catch(NullPointerException npe)
  7. {
  8.     System.debug('The following exception has occurred: ' + npe.getMessage());
  9. }

Debug will be: The following exception has occurred: Attempt to de-reference a null object

QueryException

Any problem with SOQL queries, such as assigning a query that returns no records or more than one record to a singleton sObject variable.

  1. try
  2. {
  3.     // This statement doesn't cause an exception, even though we don't have a merchandise with name='XYZ'.
  4.     // The list will just be empty.
  5.     List<Merchandise__c> lm = [SELECT Name FROM Merchandise__c WHERE Name='XYZ'];
  6.     // lm.size() is 0
  7.     System.debug(lm.size());
  8.     // However, this statement causes a QueryException because we're assiging the return value to a Merchandise__c object but no Merchandise is returned.
  9.     Merchandise__c m = [SELECT Name FROM Merchandise__c WHERE Name='XYZ' LIMIT 1];
  10. }
  11. catch(QueryException qe)
  12. {
  13.     System.debug('The following exception has occurred: ' + qe.getMessage());
  14. }

Debug log will be The following exception has occurred: List has no rows for assignment to SObject .

SObjectException

Any problem with sObject records, such as attempting to change a field in an update statement that can only be changed during insert .
Try out some code that does some things on purpose to cause this exception to be thrown. Execute the following:
  1. try
  2. {
  3.     Merchandise__c m = [SELECT Name FROM Merchandise__c LIMIT 1]; // Here No use of Limit.
  4.     // Causes an SObjectException because we didn't retrieve the Total_Inventory__c field.
  5.     Double inventory = m.Total_Inventory__c;
  6. }
  7. catch(SObjectException se)
  8. {
  9.     System.debug('The following exception has occurred: ' + se.getMessage());
  10. }

Debug log will be: The following exception has occurred: SObject row was retrieved via SOQL without querying the requested field: Merchandise__c.Total_Inventory__c

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...