3

Background

Salesforce is integrated with an external system, like Xero Accounting.

When the User updates an invoice status 'Sent' Salesforce creates it in Xero.

Process Builder calls an @InvocableMethod, which has a @future(callout=true) method which makes the API call.

public class InvoiceToXeroAction {

    @InvocableMethod(
        label = 'Invoice to Xero'
        description = 'Creates the invoice and contact in Xero via API'
    )
    public static List<Response> execute( List<Request> requests ) {

        List<Response> responses = new List<Response>();

        for ( Request req : requests ) {

            Response res = new Response();

            XeroService.createInvoice(req.invoiceId); // this method is @future

            res.isSuccess = true;
            responses.add( res );
        }
        return responses;
    }

    public class Request {

        @InvocableVariable(
            label = 'Invoice ID'
            description = 'The Salesforce ID for the Invoice__c custom object'
            required = true
        )
        public Id invoiceId;

    }

    public class Response {

        @InvocableVariable(
            label = 'Error Message'
            description = 'The message of the error'
        )
        public String errorMesssage;

        @InvocableVariable(
            label = 'Is Success'
            description = 'Successfully added Invoice to Xero'
        )
        public Boolean isSuccess;
    }
}

Sometimes the external API returns an exception, validation error or times out.

Questions

  1. What pattern or approach should I use to handle this?
  2. How can I tell the user who triggered method that it failed and why?
  • 1
    When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ? – Oleksandr Berehovskyi Oct 9 '18 at 12:31
  • 2
    If you weren't using Process Builder, the answer to this would be much simpler. Process Builder doesn't have nearly the same kind of robust error handling capabilities that pure Apex does. Can you post your code for your future method? – crmprogdev Oct 9 '18 at 12:46
3

There are two common approaches I typically use for asynchronous error handling:

  1. insert some sort of Error_Log__c record
  2. send an email notification

I recommend the former even if sending an email is your ultimate goal. Doing so allows you to:

  • set up an admin configurable Email Template
  • have a workflow send an email alert whenever such records are created
    • WFR will only work if you have static recipients such as developers/admins
  • set up reporting, dashboards, etc.
  • schedule those reports to be sent out to when there are any errors
  • Good to know, because this is what I've done already :) – Robs Oct 9 '18 at 13:33
1

In the scenario you've provided, the best I think you are going to do is to add an email error handler or use something like Loggly to record success or failure for these requests. You could add a method to the end of your Response Class that either creates an email to the admin (could possibly be to the user if you passed their Id in with the request and saved it as a public map). The simplest solution would be to do something like this at the end of your response:

if(!isEmpty(errorMessage)) new EmailHandler(errorMessage);

// pass the error message to an EmailHandler with it as a parameter where 
// it gets sent to the admin for handling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.