User Friendly Handling of Exceptions in Future Methods
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
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
- What pattern or approach should I use to handle this?
- How can I tell the user who triggered method that it failed and why?
apex callout error-messages future
add a comment |Â
up vote
2
down vote
favorite
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
- What pattern or approach should I use to handle this?
- How can I tell the user who triggered method that it failed and why?
apex callout error-messages future
1
When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
– Oleksandr Berehovskiy
1 hour ago
1
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
1 hour ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
- What pattern or approach should I use to handle this?
- How can I tell the user who triggered method that it failed and why?
apex callout error-messages future
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
- What pattern or approach should I use to handle this?
- How can I tell the user who triggered method that it failed and why?
apex callout error-messages future
apex callout error-messages future
edited 48 mins ago
asked 1 hour ago
Robs
1,161424
1,161424
1
When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
– Oleksandr Berehovskiy
1 hour ago
1
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
1 hour ago
add a comment |Â
1
When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
– Oleksandr Berehovskiy
1 hour ago
1
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
1 hour ago
1
1
When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
– Oleksandr Berehovskiy
1 hour ago
When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
– Oleksandr Berehovskiy
1 hour ago
1
1
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
1 hour ago
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
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
There are two common approaches I typically use for asynchronous error handling:
- insert some sort of
Error_Log__c
record - 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
14 mins ago
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
There are two common approaches I typically use for asynchronous error handling:
- insert some sort of
Error_Log__c
record - 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
14 mins ago
add a comment |Â
up vote
2
down vote
There are two common approaches I typically use for asynchronous error handling:
- insert some sort of
Error_Log__c
record - 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
14 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
There are two common approaches I typically use for asynchronous error handling:
- insert some sort of
Error_Log__c
record - 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
There are two common approaches I typically use for asynchronous error handling:
- insert some sort of
Error_Log__c
record - 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
edited 23 mins ago
answered 29 mins ago


Adrian Larson♦
101k19108228
101k19108228
Good to know, because this is what I've done already :)
– Robs
14 mins ago
add a comment |Â
Good to know, because this is what I've done already :)
– Robs
14 mins ago
Good to know, because this is what I've done already :)
– Robs
14 mins ago
Good to know, because this is what I've done already :)
– Robs
14 mins ago
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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.
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.
answered 24 mins ago
crmprogdev
34.6k73691
34.6k73691
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f235295%2fuser-friendly-handling-of-exceptions-in-future-methods%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
– Oleksandr Berehovskiy
1 hour ago
1
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
1 hour ago