Hey guys! Did you know that there is a hack how to skip next in an extended X++ method, wrapped using CoC (Chain of Command), which is NOT decorated with the Replaceable attribute?
This hack works also inside a transaction (ttsLevel > 0) 😀
WARNING: Please note, we DO NOT RECOMMEND using this hack! Microsoft can change behaviour of next, exception handling, etc. in the future and this "technique" might stop working.
Anyway, sometimes you simply can't find another way around 😊
Let's take a look at the following extensions of the built-in class SrsPrintDestinationTokensNone and its method expandEmailToken().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/// <summary> /// The class SrsPrintDestinationTokensNone_DC_Extension is an extension of the class SrsPrintDestinationTokensNone. /// </summary> [ExtensionOf(classStr(SrsPrintDestinationTokensNone))] final class SrsPrintDestinationTokensNone_DC_Extension { /// <summary> /// Handles the case when a token is unrecognized. /// </summary> /// <param name = "_token">A token</param> /// <param name = "_settings">Print destination settings</param> /// <returns>An unrecognized token</returns> public str expandEmailToken(SrsPrintDestinationToken _token, SRSPrintDestinationSettings _settings) { boolean mustSkipNext = true; str tokenNotFoundMsg; // Skip the next by using the following trick with the try-catch block. The base expandEmailToken() method generates a warning message that causes // problems if infolog contains some old warnings or error messages. try { if (mustSkipNext) { // When an exception is thrown inside a ttsBegin-ttsCommit transaction block, no catch statement inside that transaction block can process the exception, // unless it is a UpdateConflict or a DuplicateKeyException. We always want to be sure that the catch block is executed, so we use the UpdateConflict exception. throw Exception::UpdateConflict; } tokenNotFoundMsg = next expandEmailToken(_token, _settings); } catch (Exception::UpdateConflict) { // Don't expand the token, and don't show the error. Let Docentric report DSP class to deal with this. tokenNotFoundMsg = DocConstantPlaceholder::PlaceholderStartSymbol + _token + DocConstantPlaceholder::PlaceholderEndSymbol; } return tokenNotFoundMsg; } } |
Initially, we used throw Exception::Error but this solution didn't work inside a transaction (ttsLevel > 0).
Learn more on CoC in MS doc >>
Learn more on exception handling inside a transaction >>