0

I'm running into this strange issue regarding the Queueable Apex and Exception handling.

I have something like this: I'm querying for an Email Template(Only one)

List<EmailTemplate> emailTemplate = [Select Id, Subject, HtmlValue, Body from EmailTemplate where DeveloperName =: eTemplate];

Then

if(emailTemplate != null && !emailTemplate.isEmpty()){
    //My logic here
}
else

{
    //Kicking a queuable here to set the field(to TRUE) that fires this functionality
    throw new MyException('something bad happened!');
}

Now the logic in the else block doesn't fire the queuable jobs.I think the "THROW" statement doesn't let the queuable to fire.

Is there any logic that might help here

1
  • emailTemplate will never be null.
    – sfdcfox
    Jun 6 '17 at 21:43
1

Yes, an uncaught exception will cause the entire transaction to roll back, including any executed jobs, DML Operations, etc. You would have to remove your throw if you want the job to fire.

If you want to surface an error in Visualforce, you can just use ApexPages.addMessage or ApexPages.addMessages, depending on what you're most comfortable with. As mentioned in the comments, your code structure may need to change to properly handle the case where your query fails to find a record.

While it's not the only paradigm that works, I find it better to avoid exceptions in your execution path where possible. The common approach is to query to a list and check if it is empty.

List<EmailTemplate> candidates = [/*query*/];
if (candidates.isEmpty())
{
    ApexPages.addMessage(new ApexPage.Message(
        ApexPages.Severity.FATAL, 'No template found'
    ));
}
else
{
    // execute job
}
1
  • Yeah ..it worked fine without the throw exception....the reason I'm using the throw statement is to show the exception on our UI(VF with ExtJs) which is a combination of almost 10 objects.I'm trying to figure out if there is any alternate way
    – sai kumar
    Jun 6 '17 at 21:49

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.