Skip to content

abaK functionalities

Nuno Godinho edited this page May 20, 2019 · 6 revisions

Learn from the Unit tests

The simplest way to learn how to use abaK is by looking at the unit tests of class ZCL_ABAK.

Or learn from these examples

The ZIF_ABAK interface has the following methods:

  • GET_VALUE()
  • GET_VALUE_IF_EXISTS()
  • GET_RANGE()
  • GET_RANGE_IF_EXISTS()
  • CHECK_VALUE()
  • CHECK_VALUE_IF_EXITS()
  • INVALIDATE()

Their names are self explanatory except for INVALIDATE() which forces the data source to clear its cache and reload the data.

Methods _IF_EXISTS()don't throw a exception if no constants are defined, allowing for much simpler code in the cases where the definition of the constant is optional. For example, by using method GET_RANGE_IF_EXISTS(), when you use that range in a SELECT or from an internal table, the results will only be restricted by this range in case a constant is defined. Otherwise it will have no effect.

Sample data

Let's assume there is a register in table ZBAK with id GLOBAL pointing to a custom table with the following content.

MANDT SCOPE FIELDNAME CONTEXT IDX UE_SIGN UE_OPTION UE_LOW UE_HIGH
100 COMMON BUKRS FROM 0 I EQ 1000
100 COMMON BUKRS TO 0 I EQ 2000
100 COMMON WAERS 1 I EQ EUR
100 COMMON WAERS 2 I EQ USD
100 COMMON WRBTR LEVEL1 1 I BT 0 999
100 COMMON WRBTR LEVEL2 1 I GE 1000
100 COMMON WERKS 0 I EQ 1234

Get a single value

This code will return a single value or yield an exception in case that constant is not defined.

DATA: o_abak TYPE REF TO zif_abak_source,
      bukrs TYPE bukrs.

TRY.
    o_abak = zcl_abak_factory=>get_zabak_instance( 'GLOBAL' ).

    bukrs = o_abak->get_value( i_scope     = 'COMMON'
                               i_fieldname = 'BUKRS'
                               i_context   = 'FROM' ).
  CATCH zcx_abak.
*   Handle exception
ENDTRY.  

An even simpler one-line approach

DATA: bukrs TYPE bukrs.
bukrs = zcl_abak_factory=>get_zabak_instance( 'GLOBAL' )->get_value_if_exists( 'BUKRS' ).

Get a single value if it exists

The same as the previous example but without raising an exception if no constant is defined.

DATA: o_abak TYPE REF TO zif_abak_source,
      bukrs TYPE bukrs.

TRY.
    o_abak = zcl_abak_factory=>get_zabak_instance( 'GLOBAL' ).

  CATCH zcx_abak.
*   Handle exception
ENDTRY.  

* We can call this method without TRY CATCH
bukrs = o_abak->get_value_if_exists( i_scope     = 'COMMON'
                                     i_fieldname = 'BUKRS'
                                     i_context   = 'TO' ).

Check a single value

Check if a given value is similar to a defined constant and yield an exception in case the constant is not defined.

DATA: o_abak TYPE REF TO zif_abak_source,
      bukrs TYPE bukrs.

TRY.
    o_abak = zcl_abak_factory=>get_zabak_instance( 'GLOBAL' ).

    IF o_abak->check_value( i_scope     = 'COMMON'
                            i_fieldname = 'WERKS'
                            i_value     = '1234' ) = abap_true.
      WRITE 'ok!'.
    ENDIF.

  CATCH zcx_abak.
*   Handle exception
ENDTRY.  

Check a single value if constant exists

The same as the previous example but without raising an exception if no constant is defined.

DATA: o_abak TYPE REF TO zif_abak_source,
      bukrs TYPE bukrs.

TRY.
    o_abak = zcl_abak_factory=>get_zabak_instance( 'GLOBAL' ).

  CATCH zcx_abak.
*   Handle exception
ENDTRY.  

* We can call this method without TRY CATCH
IF o_abak->check_value_if_exists( i_scope     = 'COMMON'
                                  i_fieldname = 'WERKS'
                                  i_value     = '1234' ) = abap_true.
  WRITE 'ok!'.
ENDIF.

If the constant is not defined the method returns ABAP_FALSE.

Get a range

This code will return a range or yield an exception in case the constant is not defined.

DATA: o_abak  TYPE REF TO zif_abak_source,
      r_waers TYPE RANGE OF waers.

TRY.
    o_abak = zcl_abak_factory=>get_zabak_instance( 'GLOBAL' ).

    r_waers = o_abak->get_range( i_scope     = 'COMMON'
                                 i_fieldname = 'WAERS' ).

    SELECT * FROM zsomething WHERE waers IN r_waers.
*     Do something
    ENDSELECT.

  CATCH zcx_abak.
*   Handle exception
ENDTRY.  

Get a range if exists

The same as the previous example but without raising an exception if no constant is defined.

DATA: o_abak  TYPE REF TO zif_abak_source,
      r_waers TYPE RANGE OF waers.

TRY.
    o_abak = zcl_abak_factory=>get_zabak_instance( 'GLOBAL' ).

  CATCH zcx_abak.
*   Handle exception
ENDTRY.  

r_waers = o_abak->get_range_if_exists( i_scope     = 'COMMON'
                                       i_fieldname = 'WAERS' ).

SELECT * FROM zsomething WHERE waers IN r_waers.
* Do something
ENDSELECT.