SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow


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.

REFRESH GT_ZCARS.
SELECT * INTO TABLE GT_ZCARS FROM ZCARS.
Code

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.

sap-smartforms-initialization-code

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 NameType AssignmentAssociated TypeDefault Value
GT_ZCARSTYPE TABLE OFZCARS
GS_ZCARSTYPEZCARS
GV_ORDERNOTYPECHAR3
GV_COUNTERTYPEI0
GV_CATEGORYSUBTOTALTYPEGTY_N10
GV_TOTALTYPEGTY_N10
GV_NTYPEGTY_N10

sap-smartform-table-calculations-sum-global-data

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.

TYPES :
 BEGIN OF gty_N10,
  N10(10) TYPE N,
 END OF gty_N10.
Code

sap-smartforms-global-definitions-types-tab

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.

smartform-table-for-netweaver-trial-cars-example

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.

smartforms-event-on-sort-begin-and-end

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.

sap-smartforms-table-line-types

And here is the details of table line types when you click Table Painter buton.

sap-smartform-table-row-type-definitions-in-table-painter

Now we can configure Table Calculations tab to display sum of numeric values listed on table using SUM Total operation.

sap-smart-forms-table-calculations-with-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.

Operation Field Name Target Field Name Time Reset For Field Name
CNT Number GV_COUNTER B Before Loop S Sort Criterion CATEGORY
SUM Total GV_N GV_CATEGORYSUBTOTAL-N10 A After Loop S Sort Criterion CATEGORY
SUM Total GV_N GV_TOTAL-N10 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.

smartforms-table-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'.
Code

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.

smartform-table-sum-total-calculation-display-in-footer

After you activate SAP Smartform and run Smartform output, a similar report shown below will be displayed.

sap-smartforms-table-calculations-sum-total-example



SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.