Thursday, December 19, 2013

Structs: Using Superstructures and MOVE-CORRESPONDING instead of dynamic assigns

I recently had the problem to use polymorphy in two structures which had to be bound depending on the user type. They both needed to be testet on a common component. At first i did a dynamic way assigning the structure to TYPE ANY TABLE and then using components assigns.

This is always kind of depressing because dynamic allocations in large projects may have severe failure impacts, if they are changed a year later due to some requests. A less finesse but sturdier way is to use a superstructure, move everything in it, do your tests in a loop, and then use the sy-index for the target itabs.

An Example:

User Paul shall only see a structure with components A and B
User Andy shall only see B and C

You do a superstructure A B C for all your Methods and internal behaviour.

Just before you are ready for presentation you declare the corresponding itab for your user type and then MOVE-CORRESPONDING all from source to target.

Reading Values from Authority Objects

For everybody who is as lost as me in the ABAP djungle, here is an Example to read Values from authority Objects.

***read cost center of a user
 call function 'GET_AUTH_VALUES'
    exporting
      object1           'Z_AUTH_OBJ'
      user              sy-uname
    tables
      values            lt_auth_values
    exceptions
      user_doesnt_exist 1
      others            2.
    if sy-subrc 0.
      read table lt_auth_values into ls_auth_value with key field 'ZZ_COST_CENTER'.
      "Do something here
    endif.

It is a pretty example for binding Values to user Roles instead of User Parameters. This way they can be shared and redistributed.