Step 5: Defining the Event Handler

In this step, you will define an event handler for the ARInvoiceEntry.OnCloseDocument event. Do the following:

  1. In the RSSVWorkOrderEntry graph, define an event handler, as the following code shows.
            #region Workflow Event Handlers 
            public PXWorkflowEventHandler<RSSVWorkOrder, ARInvoice> OnCloseDocument;
            #endregion
    In the first type parameter of the handler, you have specified the primary DAC of the form where the handler is used. As the second type parameter, you have specified the primary DAC of the form where the event is fired.
    Tip: Add the using directive for the PX.Data.WorkflowAPI namespace if it has not been added earlier.
  2. In the RSSVWorkOrderWorkflow class, bind the OnCloseDocument event handler to the CloseDocument event in the screen configuration: After the AddDefaultFlow method, call the WithHandlers method. In the method, add the OnCloseDocument event handler, as the following code shows.
                    .WithHandlers(handlers =>
                    {
                        handlers.Add(handler => handler
                            .WithTargetOf<ARInvoice>()
                            .OfEntityEvent<ARInvoice.Events>(e => e.CloseDocument)
                            .Is(g => g.OnCloseDocument)
                            .UsesPrimaryEntityGetter<
                            SelectFrom<RSSVWorkOrder>.
                            Where<RSSVWorkOrder.invoiceNbr
                              .IsEqual<ARRegister.refNbr.FromCurrent>>>());
                    })
    In the code above, you have added a handler for an event that changes the state of the invoice. In the UsesPrimaryEntityGetter method, you have selected the repair work order whose state should be updated by the number of the invoice that has been closed.
    Tip: Make sure that you have added the using directives, which are shown in the following code.
    using PX.Objects.AR;
    using PX.Data.BQL.Fluent;
  3. In the definition of the Completed state, add the event handler using the WithEventHandlers method, as the following code shows.
                            fss.Add<States.completed>(flowState =>
                            {
                                return flowState
                                    .WithFieldStates(states =>
                                    {
                                        states.AddField<RSSVWorkOrder.customerID>(state =>
                                            state.IsDisabled());
                                        states.AddField<RSSVWorkOrder.serviceID>(state =>
                                            state.IsDisabled());
                                        states.AddField<RSSVWorkOrder.deviceID>(state =>
                                            state.IsDisabled());
                                    })
                                    .WithActions(actions =>
                                    {
                                        actions.Add(g => g.CreateInvoiceAction, a => a
                                        .IsDuplicatedInToolbar()
                                        .WithConnotation(ActionConnotation.Success));
                                    })
                                    // Add the OnCloseDocument event handler
                                    .WithEventHandlers(handlers =>
                                    {
                                        handlers.Add(g => g.OnCloseDocument);
                                    });
                            });
  4. In the lambda expression for the WithTransitions method, add the transition from the Completed state to the Paid state, as the following code shows.
                            transitions.AddGroupFrom<States.completed>(ts =>
                            {
                                ts.Add(t => t.To<States.paid>()
                                  .IsTriggeredOn(g => g.OnCloseDocument));
                            });
  5. Save your changes.