New Methods in Apex– Winter 21

New Apex Methods released in Winter 21

Improve Apex Testing with New SObject Error Methods

Have you ever felt why error methods are not available in Testing?

Options are available now with below methods:

  1. SObject.hasErrors() and SObject.getErrors():
    • Track errors with the new methods without performing a DML operation to check the result for errors
    • Use this method to retrieve the list of errors for a specific SObject instance
    • If the SObject.getErrors() method returns a list of Database.Error objects that contain the errors encountered
  2. SObject.hasErrors():
    • Use this method to know if an SObject instance contains errors
  3. SObject.addError(): 
    • Dynamically add errors to specific fields with new overload methods
    • If the SObject.addError() method has been called on an SObject instance, the SObject.hasErrors() method returns true

Example Code:

Case a = new Case();
   String msg = 'New error method in SObject';
   a.addError('Name', msg); 
   List<Database.Error> errors = a.getErrors();
   System.assertEquals(1, errors.size());
   Database.Error error = errors.get(0);
   System.assertEquals(msg, error.getMessage());
   System.assertEquals(StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION,        error.getStatusCode());
   String[] fields = error.getFields();
   System.assertNotEquals(null, fields);
   System.assertEquals(1, fields.size());

Delete up to 50,000 Big Object Records in a Batch

The Database.deleteImmediate() method now supports batch deletes of up to 50,000 big object records at once. This new limit matches the total number of records you can retrieve with a SOQL query.

Enhanced Support for @namespaceAccessible Annotation

The @namespaceAccessible annotation provides enhanced support for accessing interfaces, properties, and abstract classes in second-generation packages.

Features: The @namespaceAccessible annotation improvements include:

  • The @namespaceAccessible annotation now supports interfaces, properties, and abstract classes
  • A public or protected variable or method with the @namespaceAccessible annotation must be in a class that is declared as public and annotated with @namespaceAccessible
  • A public or protected inner class with @namespaceAccessible annotation must be enclosed in an outer class that is declared as public and annotated with @namespaceAccessible

Send Custom Notifications from Apex

Use the Messaging.CustomNotification class to create, configure, and send custom notifications directly from Apex code, such as a trigger.

Why not API callouts?

Rather than making API callouts from Apex, use CustomNotification to send notifications. It requires fewer lines of code and avoids API callout and authentication complexity. It is also more efficient, which is especially helpful when you are sending notifications from triggers or sending many notifications.

How it will be?

Using the Messaging.CustomNotification class to create and send custom notifications from Apex is easy. Create a new instance of CustomNotification, configure a few notification details, and call the send() method.

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.