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.
- Click on the root node and add an Actions node.
- Add an Action node with the following property:
- ActionName: Buy
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:
- Create an Invoice variable based on the Invoice BC
- 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.
|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
|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.
&Window.Url = EntityManagerInvoice.Link(K2BTrnMode.Display, &Invoice.InvoiceCode, "")
For &Message in &Invoice.GetMessages()
|Subroutine code - Part 4
Try it at runtime and see how the invoice is generated.