User Friendly Handling of Exceptions in Future Methods

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
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



  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?









share|improve this question



















  • 1




    When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
    – Oleksandr Berehovskiy
    11 hours ago






  • 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
    11 hours ago

















up vote
3
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



  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?









share|improve this question



















  • 1




    When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
    – Oleksandr Berehovskiy
    11 hours ago






  • 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
    11 hours ago













up vote
3
down vote

favorite









up vote
3
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



  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?









share|improve this question















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?






apex callout error-messages future






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 11 hours ago

























asked 11 hours ago









Robs

1,166424




1,166424







  • 1




    When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
    – Oleksandr Berehovskiy
    11 hours ago






  • 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
    11 hours ago













  • 1




    When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
    – Oleksandr Berehovskiy
    11 hours ago






  • 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
    11 hours 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
11 hours ago




When an invoice gets to a certain status, Salesforce creates it in Xero. - is status updated by User, or automated process ?
– Oleksandr Berehovskiy
11 hours ago




2




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
11 hours 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
11 hours ago











2 Answers
2






active

oldest

votes

















up vote
3
down vote













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





share|improve this answer






















  • Good to know, because this is what I've done already :)
    – Robs
    10 hours ago

















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.





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "459"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    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





    share|improve this answer






















    • Good to know, because this is what I've done already :)
      – Robs
      10 hours ago














    up vote
    3
    down vote













    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





    share|improve this answer






















    • Good to know, because this is what I've done already :)
      – Robs
      10 hours ago












    up vote
    3
    down vote










    up vote
    3
    down vote









    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





    share|improve this answer














    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 10 hours ago

























    answered 10 hours ago









    Adrian Larson♦

    101k19108228




    101k19108228











    • Good to know, because this is what I've done already :)
      – Robs
      10 hours ago
















    • Good to know, because this is what I've done already :)
      – Robs
      10 hours ago















    Good to know, because this is what I've done already :)
    – Robs
    10 hours ago




    Good to know, because this is what I've done already :)
    – Robs
    10 hours ago












    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.





    share|improve this answer
























      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.





      share|improve this answer






















        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.





        share|improve this answer












        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.






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 10 hours ago









        crmprogdev

        34.6k73691




        34.6k73691



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)