Skip to content

Examples

Nuno Godinho edited this page May 1, 2023 · 6 revisions

Global constants for a whole system

In this scenario we define a group of system-wide constants which will be used in several different places. So we'll call the scope GLOBAL and will use a database to store the actual values.

1. Create the custom constants table

Create a table called ZCONSTS_GLOBAL (please follow your system's naming conventions) according to the instructions in Source: Database. The table is defined as Customizing so that it requires a Transport Request and can only be edited (using SM30) in the DEV environment.

2. Register the new table in ZABAK

Use transaction SM30 (see ZABAK Configuration for more details):

ID NAME FORMAT_TYPE CONTENT_TYPE CONTENT
GLOBAL System-wide global constants DB INLINE ZABAK_GLOBAL

3. Fill table ZCONSTS_GLOBAL with the desired constants

Use transaction SM30:

MANDT SCOPE FIELDNAME CONTEXT IDX UE_SIGN UE_OPTION UE_LOW UE_HIGH
BUKRS NAFTA 1 I EQ 1111
BUKRS NAFTA 2 I EQ 2222

4. Use it

Get a reference to a ZIF_ABAK instance using the factory's method specific for ZABAK customized constants GET_ABAK_INSTANCE(). Check Instantiating abaK to learn more about this:

DATA: o_abak TYPE zif_abak.
o_abak = zcl_abak_factory=>get_zabak_instance( 'GLOBAL' ).

Use the instance to get the desired constant. In this particular case, since we defined two values for field BUKRS with context NAFTA, we can use it as a range:

IF i_bukrs IN o_abak->get_range( i_fieldname = 'BUKRS'
                                 i_context   = 'NAFTA' ).
  WRITE 'NAFTA!!!'.
ENDIF.

Note that we opted to leave SCOPE blank which is the default value so we don't need to explicitly specify it. That's because in this case, table ZCONSTS_GLOBAL is already a scope in itself.

Constants for project "Moon"

This is a big project so we want to have a dedicated constants table to store all its constants. It will be shared by all the developed objects (reports, interfaces, enhancements, forms, etc.) related to this project.

1. Create the custom constants table

Create a table called ZMOON_CONSTS (please follow your system's naming conventions) according to the instructions in Source: Database. This will be an Application table so that it can be maintained directly in PRD without requiring any Transport Request (because the project team decided so).

2. Fill table ZMOON_CONSTS with the desired constants

Use transaction SM30:

MANDT SCOPE FIELDNAME CONTEXT IDX UE_SIGN UE_OPTION UE_LOW UE_HIGH
ZMOON_REPORT1 SGTXT EARTH 0 I EQ Planet Earth
ZMOON_REPORT1 SGTXT MOON 0 I EQ Cheese satelite
ZMOON_REPORT2 BUKRS 0 I EQ 1111

The first two lines define a text which should be used by program ZMOON_REPORT1 depending on the provided context. The third line defines the company that should be used by program ZMOON_REPORT2.

3. Use it

Unlike the previous example, it was decided to not register this constants table centrally in ZABAK so we will use the generic factory method GET_STANDARD_INSTANCE(). Check Instantiating abaK to learn more about this.

3.1. Get SGTXT in program ZMOON_REPORT1

The text depends on parameter I_PLANET:

DATA: o_abak TYPE zif_abak.
o_abak = zcl_abak_factory=>get_standard_instance( i_format_type = zif_abak_consts=>format_type-internal
                                                  i_source_type = zif_abak_consts=>source_type-database
                                                  i_content     = 'ZMOON_CONSTS' ).

s_bseg-sgtxt = o_abak->get_value( i_scope     = SY-CPROG
                                  i_fieldname = 'SGTXT'
                                  i_context   = i_planet ).

3.2. Get company for program ZMOON_REPORT2

DATA: o_abak TYPE zif_abak.
o_abak = zcl_abak_factory=>get_standard_instance( i_format_type = zif_abak_consts=>format_type-internal
                                                  i_source_type = zif_abak_consts=>source_type-database
                                                  i_content     = 'ZMOON_CONSTS' ).

s_bkpf-bukrs = o_abak->get_value( i_scope     = SY-CPROG
                                  i_fieldname = 'BUKRS' ).

Constants for report legacy report ZXYZ_VERY_OLD_REPORT

Please see Format: Custom for a thorough example on how to implement this.