Posts Tagged apex

Logging Salesforce errors with platform events

The following pattern is fairly common in other technologies:

  try {
    doSomethingRisky();
  } catch (Exception e) {
    logException(e);
    throw e;
  }

This won’t work in salesforce. By rethrowing the exception (assuming it’s not caught further up the call stack), the database transaction will be rolled back - that includes whatever insert or update ws triggered in logException(e). So how do we ensure that the logging occurs in a separate database transaction? The answer is a platform event!

Read More

HTTP Mock Registry - A Simplified Way to Mock Apex Callouts

Apex leaves much to be desired when it comes to mocking callouts to external services. Use of the HttpCalloutMock interface requires that a test author configure mock responses for ALL callouts made through the course of a test’s processing, and this can be quite unmanageable in large or complex projects. There must be a better way… Enter the HTTP Mock Registry.

Frustrated with messy tests and abstraction leaks, we’ve devised a utility that provides a intuitive interface for mocking callouts declaratively as well as a foundation for more modular test suites whenever callouts are involved.

Read More