Options

VAR Parameter in Events

vaprogvaprog Member Posts: 1,118
Hi,

I started to implement Workflows in NAV 2017 and came about the following code fragments in Codeunit 1521 Workflow Response Handling:
PROCEDURE ExecuteResponse@2(VAR Variant@1000 : Variant;ResponseWorkflowStepInstance@1001 : Record 1504;xVariant@1004 : Variant);
    ...

          ELSE BEGIN
            OnExecuteWorkflowResponse(ResponseExecuted,Variant,xVariant,ResponseWorkflowStepInstance);
            IF NOT ResponseExecuted THEN
              ERROR(NotSupportedResponseErr,WorkflowResponse."Function Name");
          END;
So here the Event OnExecuteWorkflowResponse is raised and then ResponseExecuted is checked and if not TRUE, an ERROR is raised.
The Event has the following prototype:
[Integration]
    LOCAL PROCEDURE OnExecuteWorkflowResponse@47(VAR ResponseExecuted@1000 : Boolean;Variant@1002 : Variant;xVariant@1003 : Variant;ResponseWorkflowStepInstance@1001 : Record 1504);
    BEGIN
    END;

I found the following Documentation for the event.

So, because the event can have multiple subscribers, how is VAR ResponseExecuted handled by the event, does ExecuteResponse get back the answer of the subscriber that happened to be called last? Are the result of all the subscribers processed in some way (for example ORed together)? If so, where does this happen and / or where is the behavior defined / documented?

Does anybody have any insight?

Best Answer

  • Options
    vaprogvaprog Member Posts: 1,118
    Answer ✓
    So, since the systen bugs me about not having marked some reply as answer I now compile this "Answer".

    Although Slawek_Guzek's Answer was helpfull, it was not complete.

    Also my comment might have been not all that clear and did not really answer all my initial questions.

    So here we go:
    vaprog wrote: »
    So, because the event can have multiple subscribers, how is VAR ResponseExecuted handled by the event,
    One after the other, each subsequent handler getting the returned value from it's predecessor
    vaprog wrote: »
    does ExecuteResponse get back the answer of the subscriber that happened to be called last?
    Yes, it does
    vaprog wrote: »
    Are the result of all the subscribers processed in some way (for example ORed together)?
    No, not by the system. But Yes, by the way each subscriber is supposed to treat the ResponseExecuted parameter (see my comment above)
    vaprog wrote: »
    If so, where does this happen and / or where is the behavior defined / documented?
    So, since the system does not consolidate the answers from multiple handlers, the answer to the first part of that question is: nowhere (or maybe: in the handler's code). The answer to the second half of this question is: As far as I know nowhere apart from by example of the code in the standard NAV application.

Answers

  • Options
    Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    The ResponseExecuted should be set by the code which 'knows' the function name and handles that certain function.

    For example - this is my own handler subscribing to ExecuteResponse:

    8nzs7m8q9p9m.png

    The event is raised with some specific value in Function Name, and if the subscriber handles that specific code it sets the ResponseExecuted. If the value in Function Name is unknown (no line in CASE statement matches it) the ResponseExecuted will not be updated - and if there is no other subscriber who 'knows' the function code then the standard code will throw unhandled response error.

    When OnExecuteWorkflowResponse event is raised with specific function code it expects that some subscribers will handle it, and confirm this by updating ResponseExecuted.

    It works OK even if you have more than one subscriber. You should handle specific function only once, but nothing stops you to have many supscribers to the OnExecuteWorkflowResponse, and some of them handling the same Function Name. All will work as long as you set the ResponseExecuted at some point (in one of the handlers).

    The recommended flow is:
    raise OnExecuteWorkflowResponse with Function Name = "SomeFunctionA"
    Subscriber 1:  "SomeFunctionA" known? No -> ignore/do nothing
    Subscriber 2:  "SomeFunctionA" known? Yes -> do something / update ResponseExecuted
    Subscriber 3:  "SomeFunctionA" known? No -> ignore/do nothing
    
    if you take out Subscriber 2:
    raise OnExecuteWorkflowResponse with Function Name = "SomeFunctionA"
    Subscriber 1:  "SomeFunctionA" known? No -> ignore/do nothing
    Subscriber 3:  "SomeFunctionA" known? No -> ignore/do nothing
    
    here the SomeFunctionA is not handled at all and the C1534 will throw an error after last subscriber has finished its code

    If you have extra Subscriber 4 also knowing "SomeFunctionA" :
    raise OnExecuteWorkflowResponse with Function Name = "SomeFunctionA"
    Subscriber 1:  "SomeFunctionA" known? No -> ignore/do nothing
    Subscriber 2:  "SomeFunctionA" known? Yes -> do something / update ResponseExecuted
    Subscriber 3:  "SomeFunctionA" known? No -> ignore/do nothing
    Subscriber 4:  "SomeFunctionA" known? Yes -> do something / update ResponseExecuted
    
    In the case above you may also decide that since ResponseExecuted passed from previous subscribers is TRUE, indicating that the "SomeFunctionA" has been already handled, you do nothing - like here
    raise OnExecuteWorkflowResponse with Function Name = "SomeFunctionA"
    Subscriber 1:  "SomeFunctionA" known? No -> ignore/do nothing
    Subscriber 2:  "SomeFunctionA" known? Yes -> do something / update ResponseExecuted
    Subscriber 3:  "SomeFunctionA" known? No -> ignore/do nothing
    Subscriber 4:  "SomeFunctionA" known? Yes -> HasBeenHandled? Yes -> DoNothing / No -> do something / update ResponseExecuted
    


    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Options
    vaprogvaprog Member Posts: 1,118
    Hi Slawek

    Thank you for your answer.

    At first, I did not consider a very obvious way of handling what I asked for in my question "Are the result of all the subscribers processed in some way". The obvious answer is:

    The subscribers are called in sequence, and the next subscriber receives the result of the previous one.

    In this way, the result is the logical OR of all subscribers IF (and only if) only the one that processed the event sets the flag to true. Any subscriber that does not handle the event must not set it to false.
  • Options
    vaprogvaprog Member Posts: 1,118
    Answer ✓
    So, since the systen bugs me about not having marked some reply as answer I now compile this "Answer".

    Although Slawek_Guzek's Answer was helpfull, it was not complete.

    Also my comment might have been not all that clear and did not really answer all my initial questions.

    So here we go:
    vaprog wrote: »
    So, because the event can have multiple subscribers, how is VAR ResponseExecuted handled by the event,
    One after the other, each subsequent handler getting the returned value from it's predecessor
    vaprog wrote: »
    does ExecuteResponse get back the answer of the subscriber that happened to be called last?
    Yes, it does
    vaprog wrote: »
    Are the result of all the subscribers processed in some way (for example ORed together)?
    No, not by the system. But Yes, by the way each subscriber is supposed to treat the ResponseExecuted parameter (see my comment above)
    vaprog wrote: »
    If so, where does this happen and / or where is the behavior defined / documented?
    So, since the system does not consolidate the answers from multiple handlers, the answer to the first part of that question is: nowhere (or maybe: in the handler's code). The answer to the second half of this question is: As far as I know nowhere apart from by example of the code in the standard NAV application.

Sign In or Register to comment.