K2BTools Tutorial - Exercise 8.4: Purchase action

Now we need to create an action that allows us to generate the invoice with the selected products. To this end, we will add an action and implement it.

  1. Click on the root node and add an Actions node.
  2. Add an Action node with the following property:
    1. ActionName: Buy
  3. Save.

Once this is done, we need to implement the code associated with the action. To add the invoice we will use the transaction as a Business Component. When K2BWebPanelDesigner uses variables that will not be displayed on screen, they have to be defined in the object variable section.

Go to the variables section:

  1. Create an Invoice variable based on the Invoice BC
  2. Create an InvoiceLine variable based on the InvoiceLine BC (Invoice.InvoiceLine)

Once you have done this, return to the Designer section. Select the node associated with the Buy action, open the GoToEvent option and access the user code to implement the action.

In this subroutine we will have to create the invoice. To do so, we will have to run through the selected products SDT and generate a line for each one of them.

First, we have to obtain the SDT with the selected items. It is located in a hidden XML that we need to load in the SDT.

To do so, type the line below.

&MultipleSelectionItems.FromXml(&XMLItems_MultipleSelection)

Subroutine code - Part 1

Next, in the SDT variable we will add the header invoice with a code automatically generated by a proc, the selected customer and today’s date.

&Invoice = new()

&Invoice.InvoiceCode = GenerateInvoiceCode.Udp()

&Invoice.CustomerId = &CustomerId

&Invoice.InvoiceDate = Today()

Subroutine code - Part 2

Then we will run through the SDT of the selected elements and add a line for each one of them.

For &MultipleSelectionItem in &MultipleSelectionItems

&InvoiceLine = new()

&InvoiceLine.ProductId = &MultipleSelectionItem.ProductId

&InvoiceLine.InvoiceProductQuantity = 1

&Invoice.InvoiceLine.Add(&InvoiceLine)

Endfor

Subroutine code - Part 3

Lastly, we save the BC. If an error occurs, we will show it in the web panel; otherwise, we will display a popup with the generated invoice.

&Invoice.Save()

Commit

If &Invoice.Success()

&Window.Url = EntityManagerInvoice.Link(K2BTrnMode.Display, &Invoice.InvoiceCode, "")

&Window.Open()

Else

For &Message in &Invoice.GetMessages()

msg(&Message.Description)

EndFor

EndIf

Subroutine code - Part 4

Try it at runtime and see how the invoice is generated.