November 17, 2015

Internal tables in ABAP. Part 1

There are two kinds of tables in SAP, these are:
  • Transparent tables
  • Internal tables
Transparent tables are the database tables and they are referenced by the same name. The content of these tables are accessed by OPEN SQL which is a convention defined by SAP to map the data from these tables. These tables are created and modified in transaction 'SE11'.

Internal tables are data repositories that can have a defined structure and reside in the session memory. The structure of these tables can be one defined by our own structure, a transparent table structure or the combination of both. These can be created or defined in 'SE11' and/or as part of the source code of a program. 

Most of the time in ABAP we are going to face the use of the latter when handling large amount of data that could finally be stored in a database or especifically in transparent tables. To access the rows of the internal tables we use special structures known as work areas and field symbols. We will cover more on field symbols in next posts.

There are 3 types of internal tables:
  1. Standard tables
  2. Sorted tables
  3. Hashed tables
  4. Any tables
  5. Index tables

Standard tables

Characteristics:
  • Is the most basic table structure and is used by the majority of (if not all) the function modules and everywhere in ABAP.
  • You can address data using an index and it is also possible to make a binary search which is logarithmically proportional to size of the table.
  • In order to make use a binary search it's necessary to sort the table first with key field.
  • No restrictions on duplicated keys.
  • Use APPEND to add rows to the table.
Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
DATA: it_data TYPE STANDARD TABLE OF ty_data, " It can also be defined as 'it_data TYPE TABLE OF ty_data' without the word 'STANDARD'
   wa_data TYPE ty_data. " Or alternatively: DATA wa_data LIKE LINE OF it_data.

DO 100 TIMES.
 wa_data-field1 = sy-tabix.
 CONCATENATE 'Data' sy-tabix INTO wa_data-field2.
 GET TIME FIELD wa_data-time.
 APPEND wa_data TO it_data.
ENDDO.

READ TABLE it_data INTO WA_DATA WITH KEY field1 = 50.

IF SY-SUBRC EQ 0.
 WRITE: / 'Read table using field: ',  wa_data.
ENDIF.

SORT it_data BY field1.
READ TABLE it_data INTO WA_DATA index 10.

IF SY-SUBRC EQ 0.
 WRITE: / 'Read using index: ', wa_data.
ENDIF.


Sorted table

Characteristics:
  • A key must be defined.
  • Can be sorted or non-sorted by specified key[s].
  • Always in ascending order.
  • It can't be used 'BINARY SEARCH', however internally it uses binary search. The search is logarithmically proportional to size of the table.
  • Can't be used the command 'SORT' on them.
  • Use INSERT <work area> INTO <sorted table> to add rows to the table. The new rows will internally be added according the key defined.
Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
DATA: it_sorted TYPE SORTED TABLE OF ty_data WITH UNIQUE KEY field1,
      it_non_sorted TYPE SORTED TABLE OF ty_data WITH NON-UNIQUE KEY field1.

DO 100 TIMES.
 wa_data-field1 = 100 - sy-tabix. " Number in descendent order
 CONCATENATE 'Data' sy-tabix INTO wa_data-field2.
 GET TIME FIELD wa_data-time.
 INSERT wa_data INTO it_data.
ENDDO.

READ TABLE it_data INTO WA_DATA WITH KEY field1 = 50.

IF SY-SUBRC EQ 0.
 WRITE: / 'Read using key field',  wa_data.
ENDIF.


Hashed table
  • Managed by a hash algorithm.
  • A key must be defined.
  • Main operation is key access.
  • Access time is constant.
  • It is recommended to use with big datasets.
  • Can't be used index to access data.
  • Use INSERT to add rows to the table.
Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
DATA it_hashed TYPE HASHED TABLE OF ty_data WITH UNIQUE KEY field1 field2.

DO 100 TIMES.
 wa_data-field1 = 100 - sy-tabix. " Number in descendant order
 CONCATENATE 'Data' sy-tabix INTO wa_data-field2.
 GET TIME FIELD wa_data-time.
 INSERT wa_data TO it_data.
ENDDO.

READ TABLE it_data INTO WA_DATA WITH KEY field1 = 50.

IF SY-SUBRC EQ 0.
 WRITE: / 'Read using key field',   wa_data.
ENDIF.


Index and Any table

These are generic tables and we are going to show their use in next posts. However they are defined like this.

1
2
3
FIELD-SYMBOLS <it_index> TYPE INDEX TABLE.

FIELD-SYMBOLS <it_any> TYPE ANY TABLE.

See you in the next.

Hope it helps.

November 10, 2015

Clear, refresh and free in ABAP

In contrast to other languages where we need to make use of its basic value to initialize the variables, in SAP we don't have to make such thing. Besides initialize or clear a variable with its basic value like integers to zero and strings to space(s), in SAP we can make use of the command 'CLEAR' that can also be used with any variable and structure. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
DATA: lv_number TYPE n, lv_char type c.
lv_number = 1.
WRITE lv_number.
CLEAR lv_number. " Initialize to 0
WRITE lv_number.

lv_char = 'A'.
WRITE lv_char.
CLEAR lv_char. " Initialize to space[s]
WRITE lv_char.

The same applies to work areas;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
TYPES: BEGIN OF ty_data,
  matnr TYPE matnr,
  value(2) TYPE n,
  erdat TYPE erdat,
END OF ty_data.

DATA wa_data TYPE ty_data.
wa_data-matnr = '1'.
wa_data-value = '01'.
wa_data-erdat = sy-datum.

WRITE: 'Work area with data', wa_data.

CLEAR wa_data. " All members initialized

WRITE: / 'Work area cleared', wa_data.

The output is the following:

The effect on the content of field symbols is the same as work areas.

For internal tables we also have the option to use the CLEAR command and 2 options more: REFRESH and FREE. Let's show how they work with an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
TYPES: BEGIN OF ty_data,
  matnr TYPE matnr,
  value(5) TYPE n,
  erdat TYPE erdat,
END OF ty_data.

DATA: it_data TYPE STANDARD TABLE OF ty_data,
wa_data LIKE LINE OF it_data,
lv_count type i.

DO 10 TIMES.
  wa_data-matnr = sy-index.
  wa_data-value = '01'.
  wa_data-erdat = sy-datum.
  APPEND wa_data TO it_data.
ENDDO.

DESCRIBE TABLE it_data LINES lv_count. " Variable lv_count has the value 10

CLEAR it_data[]. " This will initialize all the content of table including the table header
REFRESH it_data. " This will initialize all the content of table but not the table header
FREE it_data. " This will initialize all the content of table including the table header and will release from memory the table

CLEAR <itab>[]: Will initialize all the content of table including the table header. Check out the brackets.

REFRESH <itab>: Will initialize all the content of table but not the table header. **

FREE <itab>: Will initialize all the content of table including the table header and will release the table from memory.

** What is the table header? Well, when a table is defined like this:

1
DATA it_data TYPE STANDARD TABLE OF ty_data WITH HEADER LINE.

It means that you can use a work area defined within the table so you don't need to create a work area for this. However, it is not advisable, it's better to use work areas and field symbols. So please, don't take this as a regular practice, just have in mind that a lot of legacy code still has this type of declaration.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
DATA: it_data TYPE STANDARD TABLE OF ty_data WITH HEADER LINE,
      lv_count type i.

DO 10 TIMES.
  it_data-matnr = sy-index.
  it_data-value = '01'.
  it_data-erdat = sy-datum.
  APPEND it_data.
ENDDO.

REFRESH it_data. " The work area within IT_DATA still has data.

WRITE it_data. " This will output of the work area'               10 0000120151110'

Well, that's all for this now.

See you in the next.

Hope it helps.

November 6, 2015

Find User-Exits and BAdIs in ABAP. Part 4

In the last post I showed a very simple but practical example about implementing a BAdI. This time I'll show how to implement a classical user-exit. As I wrote before, a BAdI is also a user-exit but in an object oriented version .

Also it's a matter of investigation and you must study all the possibilities when looking for a solution to your business requirement. Some stuff can't be definitely be done however there's at least a standard way to implement it.

For example, once I had this requirement where I needed that a replenishment transaction could divide all the generated purchase requests by the purchasing group. By standard this is not possible so it was necessary to create a program. Being SAP a complete ERP with the best practices in the business it's better to follow its instructions rather than re-inventing the wheel.

As we did in the previous post we must look for the user-exits in the transaction we are trying to enhance. When we have it we must use the transaction 'CMOD'. This transaction works creating projects where we group all the user-exits.

Before we use 'CMOD' we can check transaction 'SMOD', which is used to look for details of the user-exit like parameters and documentation if available.This same information can be watched in transaction 'CMOD', however, you must create the project first.

In this example we are going to implement a simple user-exit for transaction 'VKP1' (Change price calculation). The name of the user-exit is 'LWVK1003', which you can find with a program I posted before.

Let's implement it:

1. Execute transaction 'CMOD'.

2. Name the project 'ZMYENH'.

3. Click on button 'Create'.

4. Type a brief description of the project.

5. Click the button 'Enhancement assignments'. At this stage you'll be requested to create a transport request before saving.

6. Type in the textbox 'Enhancement' the user-exit name 'LWVK1003'.

7. In the same screen click the button 'Components'.

8. A screen with the user-exits is going to be shown. There you will see your enhancement. Double click on it.

9. Now a function module with the name 'EXIT_SAPLWVK1_003' is shown in the screen with the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
function exit_saplwvk1_003 .
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(PI_KALP_UPDKZ) LIKE  KALP-UPDKZ
*"     VALUE(PI_I_KALP) LIKE  KALP STRUCTURE  KALP
*"     VALUE(PI_I_VORGA) LIKE  PISVR-VORGA OPTIONAL
*"     REFERENCE(PI_I_GLOB) TYPE  GLOB OPTIONAL
*"  EXPORTING
*"     VALUE(PE_I_ERRO) LIKE  ERRO_TEMP STRUCTURE  ERRO_TEMP
*"----------------------------------------------------------------------


include zxvkpu04 .


endfunction.

10. Double click on the word 'zxvkpu04'.

11. Copy and paste the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
  case pi_kalp_updkz.

*-- Insert of a pricing position --------------------------------------*
    when 'I'.

*-- Update of a pricing position --------------------------------------*
    when 'U'.
*---- Sales price is less than net-net-purchase price -----------------*
      if pi_i_kalp-vkpne lt pi_i_kalp-ekpnn.
        pe_i_erro-msgid = 'WV'.                      "Message class WV
*       PE_I_ERRO-MSGTY = 'E'.                       "Error
        pe_i_erro-msgty = 'W'.                       "Warning
        pe_i_erro-msgno = '463'.                     "Message no 463
        exit.
      endif.

  endcase.

12. Activate the code.

13. Return to function module with 'F3'.

14. Return to the project.

15. Activate the project and exit of transaction 'CMOD'.

16. Execute transaction 'VKP1' and test it with any material. 


When I execute transaction 'VKP1' and the sales price is less than net purchase price the system responds with a warning.



You can debug placing a break-point in your code and check the execution.




Finally I would like to add that in a project you can add as many user-exits as you may want. Just try to group them according to you business module and areas.

See you in the next.

Hope it helps.