0

So I catch a DMLException which is caused by a custom exception from a managed package, now how can I get that custom exception?

Here is the log:

18:33:24.391 EXECUTION_STARTED 
...
18:33:25.710 SOQL_EXECUTE_BEGIN [35]|Aggregations:0|SELECT Id, Name, DocumentBundleCode__c, Recipient__c FROM DocumentBundle__c WHERE Id = :tmpVar1
18:33:25.710 SOQL_EXECUTE_END [35]|Rows:1
18:33:25.710 USER_DEBUG [165]|DEBUG|### commercialProduct: null
18:33:25.710 EXCEPTION_THROWN [178]|WrtsException: Unable to determine the Template Code/Template Version in order to print item with Id a0k3N0000021LKtQAM
18:33:25.710 EXCEPTION_THROWN [310]|WrtsException: Unable to determine the Template Code/Template Version in order to print item with Id a0k3N0000021LKtQAM
18:33:25.710 EXCEPTION_THROWN [416]|WrtsException: Unable to determine the Template Code/Template Version in order to print item with Id a0k3N0000021LKtQAM
18:33:25.710 SYSTEM_MODE_EXIT false
18:33:25.710 EXCEPTION_THROWN [82]|WrtsException: Unable to determine the Template Code/Template Version in order to print item with Id a0k3N0000021LKtQAM
18:33:25.710 CODE_UNIT_FINISHED MRO_ActivityTrg on Activity trigger event AfterInsert|__sfdc_trigger/MRO_ActivityTrg
18:33:25.710 DML_END [618]
18:33:25.710 EXCEPTION_THROWN [618]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, (): []
18:33:25.710 SAVEPOINT_ROLLBACK [642]|SavepointValue1
18:33:25.710 SYSTEM_MODE_EXIT false
18:33:25.710 CODE_UNIT_FINISHED MRO_DossierTrg on Dossier trigger event AfterUpdate|__sfdc_trigger/MRO_DossierTrg
18:33:25.710 DML_END [297]
18:33:25.710 EXCEPTION_THROWN [297]|System.DmlException: Update failed. First exception on row 0 with id a0o3N000001rGwOQAU; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, (): []
18:33:25.710 SYSTEM_MODE_EXIT false
18:33:25.710 USER_DEBUG [4]|DEBUG|System.DmlException
18:33:25.710 CODE_UNIT_FINISHED execute_anonymous_apex
18:33:25.710 EXECUTION_FINISHED 

I need to check that the DML was caused exactly by this exception:

WrtsException: Unable to determine the Template Code/Template Version in order to print item with Id a0k3N0000021LKtQAM
18:33:25.710 EXCEPTION_THROWN [310]|

How can I do that without parsing this log? I checked .getCause() but it's set to null

1 Answer 1

2

You can't get to the exception inside a trigger transaction. Every trigger transaction is wrapped in a system-level try-catch block that automatically converts any Exception to a DMLException. The original exception is lost at the trigger-transaction barrier. I do believe in some older versions of the API, Exceptions were directly catchable, but this hasn't been true for a number of years now.

3
  • I see, so is there any way in which I could read that inner message? I just figured that I can't even parse it, since getMessage() doesn't return the whole thing Feb 24, 2021 at 13:57
  • @MovsarBekaev The odd part is that I'd expect the message from DMLException to include the original text. Still, it would be a good idea to contact the managed package developer and recommend that they use sObject.addError instead of throwing an exception. It's always a bad idea to let an uncaught exception kill the trigger.
    – sfdcfox
    Feb 24, 2021 at 14:00
  • All right, looks like nothing I can do about it, thank you for your contribution and explanation. Feb 24, 2021 at 14:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .