Send out email when Apex Queueable fails and test itCode Coverage to Test Custom Object Public ListApex test fails when it should passSignificant elapsed time differences when calling sort() on different Comparable implementationsTest execution order issues with Queueable and @future methodApex class fails testCan Queueable Apex Jobs Run in Parallel?Queueable not restoring state for execute() in unit testMixed DML conflict in unit test involving two queueable jobsApexMock - Test method to invoke a Queueable method failsQueueable Apex consideratoins

Why is this basic language not a regular language?

In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?

I'm planning on buying a laser printer but concerned about the life cycle of toner in the machine

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?

To string or not to string

Roll the carpet

Smoothness of finite-dimensional functional calculus

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

Why does Kotter return in Welcome Back Kotter?

Which models of the Boeing 737 are still in production?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

What does it mean to describe someone as a butt steak?

Do I have a twin with permutated remainders?

What's the output of a record cartridge playing an out-of-speed record

Unknown notation: What do three bars mean?

How to find program name(s) of an installed package?

Have astronauts in space suits ever taken selfies? If so, how?

Assigning pointers to atomic type to pointers to non atomic type

Why is Minecraft giving an OpenGL error?

What is the word for reserving something for yourself before others do?

How can bays and straits be determined in a procedurally generated map?

Theorems that impeded progress

Finding the repeating unit of polymerisation given two constituent molecules

Why was the small council so happy for Tyrion to become the Master of Coin?



Send out email when Apex Queueable fails and test it


Code Coverage to Test Custom Object Public ListApex test fails when it should passSignificant elapsed time differences when calling sort() on different Comparable implementationsTest execution order issues with Queueable and @future methodApex class fails testCan Queueable Apex Jobs Run in Parallel?Queueable not restoring state for execute() in unit testMixed DML conflict in unit test involving two queueable jobsApexMock - Test method to invoke a Queueable method failsQueueable Apex consideratoins






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








4















This question definitely is a duplicate of many questions here, but after reading most of them I am still somewhat clueless.



Question 1: why I can't just catch exceptions in the execute and send an email out? Would a) Batch help, or b) checking or c) writing a custom Error object?



public class MyQueue implements Queueable, Database.AllowsCallouts 

public void execute(QueueableContext context)
try
Integer invalid = 100 / 0;

catch(Exception ex)
sendEmail(ex);





Question 2: Can I test it like this?



@IsTest
private class MyQueue_Test

@IsTest
private static void sendsEmailOnError()

// Exercise
Test.startTest();
System.enqueueJob(new MyQueue());
Test.stopTest();

// Verify
System.assertEquals(1, Limits. getEmailInvocations());











share|improve this question

















  • 2





    Can you elaborate "can not" please? Is that because it does not work (and what is an error), or is that because it is written somewhere not to do it or any other reason?

    – kurunve
    Apr 2 at 14:10











  • Sure, no email is "sent", not even an exception is thrown. In my logs I see a Script-Thrown Exception which is not caught by the catch().

    – Robert Sösemann
    Apr 2 at 14:23

















4















This question definitely is a duplicate of many questions here, but after reading most of them I am still somewhat clueless.



Question 1: why I can't just catch exceptions in the execute and send an email out? Would a) Batch help, or b) checking or c) writing a custom Error object?



public class MyQueue implements Queueable, Database.AllowsCallouts 

public void execute(QueueableContext context)
try
Integer invalid = 100 / 0;

catch(Exception ex)
sendEmail(ex);





Question 2: Can I test it like this?



@IsTest
private class MyQueue_Test

@IsTest
private static void sendsEmailOnError()

// Exercise
Test.startTest();
System.enqueueJob(new MyQueue());
Test.stopTest();

// Verify
System.assertEquals(1, Limits. getEmailInvocations());











share|improve this question

















  • 2





    Can you elaborate "can not" please? Is that because it does not work (and what is an error), or is that because it is written somewhere not to do it or any other reason?

    – kurunve
    Apr 2 at 14:10











  • Sure, no email is "sent", not even an exception is thrown. In my logs I see a Script-Thrown Exception which is not caught by the catch().

    – Robert Sösemann
    Apr 2 at 14:23













4












4








4








This question definitely is a duplicate of many questions here, but after reading most of them I am still somewhat clueless.



Question 1: why I can't just catch exceptions in the execute and send an email out? Would a) Batch help, or b) checking or c) writing a custom Error object?



public class MyQueue implements Queueable, Database.AllowsCallouts 

public void execute(QueueableContext context)
try
Integer invalid = 100 / 0;

catch(Exception ex)
sendEmail(ex);





Question 2: Can I test it like this?



@IsTest
private class MyQueue_Test

@IsTest
private static void sendsEmailOnError()

// Exercise
Test.startTest();
System.enqueueJob(new MyQueue());
Test.stopTest();

// Verify
System.assertEquals(1, Limits. getEmailInvocations());











share|improve this question














This question definitely is a duplicate of many questions here, but after reading most of them I am still somewhat clueless.



Question 1: why I can't just catch exceptions in the execute and send an email out? Would a) Batch help, or b) checking or c) writing a custom Error object?



public class MyQueue implements Queueable, Database.AllowsCallouts 

public void execute(QueueableContext context)
try
Integer invalid = 100 / 0;

catch(Exception ex)
sendEmail(ex);





Question 2: Can I test it like this?



@IsTest
private class MyQueue_Test

@IsTest
private static void sendsEmailOnError()

// Exercise
Test.startTest();
System.enqueueJob(new MyQueue());
Test.stopTest();

// Verify
System.assertEquals(1, Limits. getEmailInvocations());








apex unit-test asynchronous queueable-interface






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 2 at 14:07









Robert SösemannRobert Sösemann

13.2k1178225




13.2k1178225







  • 2





    Can you elaborate "can not" please? Is that because it does not work (and what is an error), or is that because it is written somewhere not to do it or any other reason?

    – kurunve
    Apr 2 at 14:10











  • Sure, no email is "sent", not even an exception is thrown. In my logs I see a Script-Thrown Exception which is not caught by the catch().

    – Robert Sösemann
    Apr 2 at 14:23












  • 2





    Can you elaborate "can not" please? Is that because it does not work (and what is an error), or is that because it is written somewhere not to do it or any other reason?

    – kurunve
    Apr 2 at 14:10











  • Sure, no email is "sent", not even an exception is thrown. In my logs I see a Script-Thrown Exception which is not caught by the catch().

    – Robert Sösemann
    Apr 2 at 14:23







2




2





Can you elaborate "can not" please? Is that because it does not work (and what is an error), or is that because it is written somewhere not to do it or any other reason?

– kurunve
Apr 2 at 14:10





Can you elaborate "can not" please? Is that because it does not work (and what is an error), or is that because it is written somewhere not to do it or any other reason?

– kurunve
Apr 2 at 14:10













Sure, no email is "sent", not even an exception is thrown. In my logs I see a Script-Thrown Exception which is not caught by the catch().

– Robert Sösemann
Apr 2 at 14:23





Sure, no email is "sent", not even an exception is thrown. In my logs I see a Script-Thrown Exception which is not caught by the catch().

– Robert Sösemann
Apr 2 at 14:23










2 Answers
2






active

oldest

votes


















5














The problem is that Test.stopTest executes the asynchronous code, then resets the governor limits back to the state it was in immediately before Test.startTest is called. If you really wanted to verify the output, you'd need a static method to check:



public class MyQueue implements Queueable, Database.AllowsCallouts 
@TestVisible static Boolean emailSent = false;
public void execute(QueueableContext context)
try
Integer invalid = 100 / 0;

catch(Exception ex)
sendEmail(ex);
emailSent = true;





And from there, test it in your unit test:



@IsTest
private class MyQueue_Test

@IsTest
private static void sendsEmailOnError()

// Exercise
Test.startTest();
System.enqueueJob(new MyQueue());
Test.stopTest();

// Verify
System.assert(MyQueue.emailSent);




This is one of those relatively rare conditions where the easiest way to perform a task is to inject test code to the actual code. You should do this minimally, of course, but as often as necessary.



Alternatively, a more idiomatic way to do this would be to use the Stub API. The idea here is that you'd mock out sendEmail by overriding it in the unit test. This is a bit complicated to use in practice, especially since you only need to add two lines of code.






share|improve this answer























  • Should I use Database Savepoints and rollbacks in the catch or does Apex care about this? I mean if I do some DML in the try...

    – Robert Sösemann
    Apr 2 at 21:46






  • 1





    @RobertSösemann for testing, it doesn't matter. For production code, if you perform multiple DML and do not roll back, you may leave your data in an inconsistent state. If you're at all concerned about this possibility, definitely roll back. This is on a case by case situation, though. You'll need to analyze each case separately, and determine if partial success should be allowed.

    – sfdcfox
    Apr 2 at 22:25


















3














I usually take the route of storing results in a log object as opposed to email.



If you want, you can add a trigger to the log object to send out an email. You can enable activity tracking for the emails as well which would allow you to query for the existence of an activity as opposed to checking the governor limits.



The benefits being:



  • logs are more permanent

  • logs are less likely to get lost in email

  • logs are not user specific meaning if there is an issue and the email recipient is out of office, the log can be viewed and the issue can be resolved

  • you can report on logs/activities but not email





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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    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%2f256225%2fsend-out-email-when-apex-queueable-fails-and-test-it%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    The problem is that Test.stopTest executes the asynchronous code, then resets the governor limits back to the state it was in immediately before Test.startTest is called. If you really wanted to verify the output, you'd need a static method to check:



    public class MyQueue implements Queueable, Database.AllowsCallouts 
    @TestVisible static Boolean emailSent = false;
    public void execute(QueueableContext context)
    try
    Integer invalid = 100 / 0;

    catch(Exception ex)
    sendEmail(ex);
    emailSent = true;





    And from there, test it in your unit test:



    @IsTest
    private class MyQueue_Test

    @IsTest
    private static void sendsEmailOnError()

    // Exercise
    Test.startTest();
    System.enqueueJob(new MyQueue());
    Test.stopTest();

    // Verify
    System.assert(MyQueue.emailSent);




    This is one of those relatively rare conditions where the easiest way to perform a task is to inject test code to the actual code. You should do this minimally, of course, but as often as necessary.



    Alternatively, a more idiomatic way to do this would be to use the Stub API. The idea here is that you'd mock out sendEmail by overriding it in the unit test. This is a bit complicated to use in practice, especially since you only need to add two lines of code.






    share|improve this answer























    • Should I use Database Savepoints and rollbacks in the catch or does Apex care about this? I mean if I do some DML in the try...

      – Robert Sösemann
      Apr 2 at 21:46






    • 1





      @RobertSösemann for testing, it doesn't matter. For production code, if you perform multiple DML and do not roll back, you may leave your data in an inconsistent state. If you're at all concerned about this possibility, definitely roll back. This is on a case by case situation, though. You'll need to analyze each case separately, and determine if partial success should be allowed.

      – sfdcfox
      Apr 2 at 22:25















    5














    The problem is that Test.stopTest executes the asynchronous code, then resets the governor limits back to the state it was in immediately before Test.startTest is called. If you really wanted to verify the output, you'd need a static method to check:



    public class MyQueue implements Queueable, Database.AllowsCallouts 
    @TestVisible static Boolean emailSent = false;
    public void execute(QueueableContext context)
    try
    Integer invalid = 100 / 0;

    catch(Exception ex)
    sendEmail(ex);
    emailSent = true;





    And from there, test it in your unit test:



    @IsTest
    private class MyQueue_Test

    @IsTest
    private static void sendsEmailOnError()

    // Exercise
    Test.startTest();
    System.enqueueJob(new MyQueue());
    Test.stopTest();

    // Verify
    System.assert(MyQueue.emailSent);




    This is one of those relatively rare conditions where the easiest way to perform a task is to inject test code to the actual code. You should do this minimally, of course, but as often as necessary.



    Alternatively, a more idiomatic way to do this would be to use the Stub API. The idea here is that you'd mock out sendEmail by overriding it in the unit test. This is a bit complicated to use in practice, especially since you only need to add two lines of code.






    share|improve this answer























    • Should I use Database Savepoints and rollbacks in the catch or does Apex care about this? I mean if I do some DML in the try...

      – Robert Sösemann
      Apr 2 at 21:46






    • 1





      @RobertSösemann for testing, it doesn't matter. For production code, if you perform multiple DML and do not roll back, you may leave your data in an inconsistent state. If you're at all concerned about this possibility, definitely roll back. This is on a case by case situation, though. You'll need to analyze each case separately, and determine if partial success should be allowed.

      – sfdcfox
      Apr 2 at 22:25













    5












    5








    5







    The problem is that Test.stopTest executes the asynchronous code, then resets the governor limits back to the state it was in immediately before Test.startTest is called. If you really wanted to verify the output, you'd need a static method to check:



    public class MyQueue implements Queueable, Database.AllowsCallouts 
    @TestVisible static Boolean emailSent = false;
    public void execute(QueueableContext context)
    try
    Integer invalid = 100 / 0;

    catch(Exception ex)
    sendEmail(ex);
    emailSent = true;





    And from there, test it in your unit test:



    @IsTest
    private class MyQueue_Test

    @IsTest
    private static void sendsEmailOnError()

    // Exercise
    Test.startTest();
    System.enqueueJob(new MyQueue());
    Test.stopTest();

    // Verify
    System.assert(MyQueue.emailSent);




    This is one of those relatively rare conditions where the easiest way to perform a task is to inject test code to the actual code. You should do this minimally, of course, but as often as necessary.



    Alternatively, a more idiomatic way to do this would be to use the Stub API. The idea here is that you'd mock out sendEmail by overriding it in the unit test. This is a bit complicated to use in practice, especially since you only need to add two lines of code.






    share|improve this answer













    The problem is that Test.stopTest executes the asynchronous code, then resets the governor limits back to the state it was in immediately before Test.startTest is called. If you really wanted to verify the output, you'd need a static method to check:



    public class MyQueue implements Queueable, Database.AllowsCallouts 
    @TestVisible static Boolean emailSent = false;
    public void execute(QueueableContext context)
    try
    Integer invalid = 100 / 0;

    catch(Exception ex)
    sendEmail(ex);
    emailSent = true;





    And from there, test it in your unit test:



    @IsTest
    private class MyQueue_Test

    @IsTest
    private static void sendsEmailOnError()

    // Exercise
    Test.startTest();
    System.enqueueJob(new MyQueue());
    Test.stopTest();

    // Verify
    System.assert(MyQueue.emailSent);




    This is one of those relatively rare conditions where the easiest way to perform a task is to inject test code to the actual code. You should do this minimally, of course, but as often as necessary.



    Alternatively, a more idiomatic way to do this would be to use the Stub API. The idea here is that you'd mock out sendEmail by overriding it in the unit test. This is a bit complicated to use in practice, especially since you only need to add two lines of code.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 2 at 17:21









    sfdcfoxsfdcfox

    263k12209456




    263k12209456












    • Should I use Database Savepoints and rollbacks in the catch or does Apex care about this? I mean if I do some DML in the try...

      – Robert Sösemann
      Apr 2 at 21:46






    • 1





      @RobertSösemann for testing, it doesn't matter. For production code, if you perform multiple DML and do not roll back, you may leave your data in an inconsistent state. If you're at all concerned about this possibility, definitely roll back. This is on a case by case situation, though. You'll need to analyze each case separately, and determine if partial success should be allowed.

      – sfdcfox
      Apr 2 at 22:25

















    • Should I use Database Savepoints and rollbacks in the catch or does Apex care about this? I mean if I do some DML in the try...

      – Robert Sösemann
      Apr 2 at 21:46






    • 1





      @RobertSösemann for testing, it doesn't matter. For production code, if you perform multiple DML and do not roll back, you may leave your data in an inconsistent state. If you're at all concerned about this possibility, definitely roll back. This is on a case by case situation, though. You'll need to analyze each case separately, and determine if partial success should be allowed.

      – sfdcfox
      Apr 2 at 22:25
















    Should I use Database Savepoints and rollbacks in the catch or does Apex care about this? I mean if I do some DML in the try...

    – Robert Sösemann
    Apr 2 at 21:46





    Should I use Database Savepoints and rollbacks in the catch or does Apex care about this? I mean if I do some DML in the try...

    – Robert Sösemann
    Apr 2 at 21:46




    1




    1





    @RobertSösemann for testing, it doesn't matter. For production code, if you perform multiple DML and do not roll back, you may leave your data in an inconsistent state. If you're at all concerned about this possibility, definitely roll back. This is on a case by case situation, though. You'll need to analyze each case separately, and determine if partial success should be allowed.

    – sfdcfox
    Apr 2 at 22:25





    @RobertSösemann for testing, it doesn't matter. For production code, if you perform multiple DML and do not roll back, you may leave your data in an inconsistent state. If you're at all concerned about this possibility, definitely roll back. This is on a case by case situation, though. You'll need to analyze each case separately, and determine if partial success should be allowed.

    – sfdcfox
    Apr 2 at 22:25













    3














    I usually take the route of storing results in a log object as opposed to email.



    If you want, you can add a trigger to the log object to send out an email. You can enable activity tracking for the emails as well which would allow you to query for the existence of an activity as opposed to checking the governor limits.



    The benefits being:



    • logs are more permanent

    • logs are less likely to get lost in email

    • logs are not user specific meaning if there is an issue and the email recipient is out of office, the log can be viewed and the issue can be resolved

    • you can report on logs/activities but not email





    share|improve this answer





























      3














      I usually take the route of storing results in a log object as opposed to email.



      If you want, you can add a trigger to the log object to send out an email. You can enable activity tracking for the emails as well which would allow you to query for the existence of an activity as opposed to checking the governor limits.



      The benefits being:



      • logs are more permanent

      • logs are less likely to get lost in email

      • logs are not user specific meaning if there is an issue and the email recipient is out of office, the log can be viewed and the issue can be resolved

      • you can report on logs/activities but not email





      share|improve this answer



























        3












        3








        3







        I usually take the route of storing results in a log object as opposed to email.



        If you want, you can add a trigger to the log object to send out an email. You can enable activity tracking for the emails as well which would allow you to query for the existence of an activity as opposed to checking the governor limits.



        The benefits being:



        • logs are more permanent

        • logs are less likely to get lost in email

        • logs are not user specific meaning if there is an issue and the email recipient is out of office, the log can be viewed and the issue can be resolved

        • you can report on logs/activities but not email





        share|improve this answer















        I usually take the route of storing results in a log object as opposed to email.



        If you want, you can add a trigger to the log object to send out an email. You can enable activity tracking for the emails as well which would allow you to query for the existence of an activity as opposed to checking the governor limits.



        The benefits being:



        • logs are more permanent

        • logs are less likely to get lost in email

        • logs are not user specific meaning if there is an issue and the email recipient is out of office, the log can be viewed and the issue can be resolved

        • you can report on logs/activities but not email






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 2 at 17:50

























        answered Apr 2 at 17:44









        gNerbgNerb

        6,081834




        6,081834



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Salesforce Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid …


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f256225%2fsend-out-email-when-apex-queueable-fails-and-test-it%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Magento 1.9 observer on only new product added ( no updated product )Admin Create New Order ObserverObserver checkout_cart_product_add_after get product quantity in cartHow can I differentiate between product creation and saving existing products?How to get the original price Product SaveBeforeMagento Observer for Admin Sales order Address updateHow do i get quantity in a product_save_after event observer magento2?Using observer event to get updated customer infoSave product observer - detect availabilityEvent not triggered when new product added in admin : Magento 2magento 1.9 observer on quantity product change

            Does an object always see its latest internal state irrespective of thread? The 2019 Stack Overflow Developer Survey Results Are InDoes a finally block always get executed in Java?Java: serial thread confinement questionJava difference between fixed threadpool and scheduled threadpoolJackrabbit and concurrent modificationHow to stop a Runnable scheduled for repeated execution after a certain number of executionsMultiple threads accessing inner classHow can a loop be completed by two thread? say loop from count=1 to count=4 by ist thread and count =5 to 8 by 2nd thread?If I keep a reference to a Runnable, when does the Thread it ran on get released?How to stop a schedule ScheduledExecutorServiceHow to implement a singleThreadedScheduledExcecutor in wildfly

            Adding contour labels to SpatialLinesDataFrame or sf object? The Next CEO of Stack OverflowTearing of SpatialLinesDataFrame and problem with gClip functionAutomatically adjusting map labelsExtracting data.frame from simple features object in RR convert sf object back to SpatialPolygonsDataFrameSimplify polygons of sf objectlines of a SpatialLinesDataFrame corresponding to pointsCropping sf object in R?Pattern matching with grepl on SpatialPolygonsDataFrame object in RHow to create and empty SpatialLinesDataFrame in RWarning message in raster::rotate(): “this does not look like an appropriate object for this function”