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

ABAP LOOP Statement with At New and At End

ABAP LOOP statement enables developers to iterate in an ABAP internal table. ABAP Loop command enhancements At New and At End provide additional functions for SAP developers like identifying the start and end of a group or partition according to a field in a sorted internal table. While using ABAP LOOP statement in your ABAP programs, you can think of using AT NEW field and AT END field statements to find fast solutions for your requirements.

For example, ABAP developers can create a program listing sales orders of all sales representative in a month. The order data can be stored in an internal table sorted by sales represantative. While looping over sales order data, if Loop statement is used with At New and At End extensions, it is possible to count orders of each representative during the Loop statement.

ABAP Loop statement with At New and At End

In this ABAP tutorial, I will share source codes of a similar requirement case but not this time count item details, but print item posnr values on screen.

ABAP Loop At New and At End sample code

I also copied down the source codes of the ABAP report where Loop At flow control statement is used with extensions At New and At End.

data LS_VBAP type VBAP.
data LT_VBAP type table of VBAP.
data LV_COMMA type C.

select * from VBAP into table LT_VBAP up to 1000 rows.

loop at LT_VBAP into LS_VBAP.
 at new VBELN.
  write :/ 'Sales document ', LS_VBAP-VBELN, ' with item(s) ('.
 endat.

 write: LV_COMMA, LS_VBAP-POSNR.
 LV_COMMA = ','.

 at end of VBELN.
  write ')'.
  clear LV_COMMA.
 endat.
endloop.
Code

As a summary, above sample ABAP program SELECT a list of sales items from VBAP table and lists all of them using ABAP Loop command.

Within the Loop command, AT NEW statement detects when the VBAP structure has a different VBELN (document number) then the previous one.
When a new VBELN Sales Order number is detected in the loop, the order number is printed. And a bracket is opened for the item positions of that sales order.

LOOP AT END statement also detects the different vbeln number and enables ABAP developers to do work with the previous structure in the loop.
In this sample ABAP program, I used the Loop At End for closing the bracket containing the list of sales order item positions.

Here is the output of the sample ABAP program featuring ABAP Loop At New and At End statements.

ABAP Loop listing sales orders with items

Although we loop through the internal table for sales order items sequentially using the Loop command, At New field and At End field enabled developers to print sales order header data with item details



SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


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