SAP Smartforms Tutorial - Table Calculations using Sum Total
Within this SAP Smartforms tutorial, I will share ABAP codes with SAP Smartforms developers showing how to use Table Calculations using Sum - Total function in Smartform document.
Before going details with the Smartform table calculations, I want to note that the samples are created and executed on SAP Netweaver 7.01 ABAP Trial version.
Please check the SAP Netweaver 7.01 ABAP trial version installation guide for details.
As the data source of the sample Smartform document, I will use the ZCARS ABAP table.
The SAP Smartforms output will list the rows in ZCARS ABAP table.
So I create a new SAP Smartforms report using ABAP transaction code smartforms with Smart Form name ZSSF_CARS_LIST.
In the Initialization phase of the Smart Form output, copy and paste following ABAP code which reads all rows from ZCARS ABAP table into internal table GT_ZCARS.
The ABAP internal table data will be displayed in the Smartform table control later in this Smartforms tutorial.
Go to Form > Global Settings > Global Definitions screen. Click on Initialization tab.
SELECT * INTO TABLE GT_ZCARS FROM ZCARS.
Please note that in the output parameters list we should place the GT_ZCARS global variable.
I prefer to use GT_ZCARS also in the input parameters list.
Here is the screenshot how Smartform developers can manage initialization section of this example output.
Of course, we have not yet defined the global variable GT_ZCARS.
If you try to activate Samrtform, it will fail because of the undefined internal table.
Let's define global variables that are required in this example SAP Smartforms tutorial.
Go to Form > Global Settings > Global Definitions screen. Click on Global Data tab.
Create the following global variables in the Global Data table as follows.
|Variable Name||Type Assignment||Associated Type||Default Value|
|GT_ZCARS||TYPE TABLE OF||ZCARS|
ABAP developers will realize that I used a custom type GTY_N10 in the Global Data declarations type.
We have to define this data type in the SAP Smartform document before we can use it.
Go to Form > Global Settings > Global Definitions screen. Click on Types tab.
Type the followin ABAP code for type declaration.
BEGIN OF gty_N10,
N10(10) TYPE N,
END OF gty_N10.
Now we are ready to create SAP Smartforms Table control.
Right click on Main window and choose Create > Table from context menu.
The data of the table is from ABAP internal table we have defined in Global Data tab and populated in Initialization tab.
Let's assing the data source GT_ZCARS to SAP Smartforms table as seen in the below screenshot.
Go to the Data tab of the Table control, mark Internal Table checkbox.
With data loop in internal table GT_ZCARS into GS_ZCARS structure or work area.
You will notice in the above screenshot that I added the CATEGORY field as sort criteria and mark the Event on Sort Begin and Event on Sort End checkboxes.
These checkmarks will cause Smartform to create events and additional event lines just before and after the Main Area of the Smartform table.
Let's now create line types for table rows.
I define two line types; one for displaying item rows and the second is for displaying category sum and overall total line.
And here is the details of table line types when you click Table Painter buton.
Now we can configure Table Calculations tab to display sum of numeric values listed on table using SUM Total operation.
The first line in Table Calculations is for auto numbering rows in the table display.
ABAP developers can find the details about CNT Number Calculation example in SAP Smartforms Table in this target Smartform tutorial.
The second and third lines in Table Calculations manage the category subtotals and report total respectively.
||Target Field Name
||For Field Name
||B Before Loop
||S Sort Criterion
||A After Loop
||S Sort Criterion
||A After Loop
The above Table Calculations configuration will work as follows :
After each row displayed in the table, the global variable GV_N is summed into GV_CategorySubTotal-N10 variable value.
The GV_CategorySubTotal-N10 value will be reset to its original value with Category field sort criterion.
This means GV_CategorySubTotal-N10 will sum GV_N values from each row within same category.
When the category changes, and a new category items begin listing on the table GV_CategorySubTotal-N10 will be set to 0, the initial value.
So to display Category Subtotals, we will display GV_CategorySubTotal-N10 just before it is reset to initial value in the Category Event on Sort End line.
Just before printing the GV_CATEGORYSUBTOTAL-N10 value on the Smartform Text control within the event line cell, I remove preceeding zeros from the global variable.
* Remove preceeding zeros
SHIFT GV_CATEGORYSUBTOTAL-N10 LEFT DELETING LEADING '0'.
For the GV_TOTAL value which will display overall sum of the table row field, the third table calculation line will help Smartform developers.
Note that this SUM calculation is same as previous calculation with exception this does not have a reset criteria.
This means the sum operation will work for all table rows.
The GV_N field variable calculated during table row display will be summed into target field GV_TOTAL-N10 till the end of all table rows.
The GV_TOTAL-N10 total value can be easily displayed in SAP Smartforms table using the Table Footer section as follows.
After you activate SAP Smartform and run Smartform output, a similar report shown below will be displayed.