ABAP BOOLC and XSDBOOL use-cases

 In the previous post ABAP Convert Logical Expressions Into Boolean Values, I talked about BOOLC and XSDBOOL functions and their respective features. I thought I would elaborate on their use cases with examples.

BOOLC:

  • Set boolean flag:
  • "Set boolean using logical expression.
    "if the table is empty, set the flag to true.
    lv_istab_empty = boolc( lines( lt_mytab[] ) = 0 ).

  • Set custom flags like Y/N or 0/1 using ABAP built-in processing functions.
  • REPORT syst-repid.
    DATA:
    lt_tab TYPE string_table.
    DATA:
    lv_myflag TYPE char1.
    START-OF-SELECTION.
    " Use BOOLC to set custom flags.
    "sets to Y
    lv_myflag = TRANSLATE( val = boolc( lines( lt_tab[] ) = 0 ) from = `X ` to = `YN` ) .
    write: lv_myflag.
    " add record to itab.
    append `Record` to lt_tab.
    "sets to N
    lv_myflag = TRANSLATE( val = boolc( lines( lt_tab[] ) = 0 ) from = `X ` to = `YN` ) .
    write:/ lv_myflag.
    DATA:
    lv_myintflag TYPE i.
    "sets to 1
    lv_myintflag = strlen( condense( val = boolc( lines( lt_tab[] ) = 0 ) ) ).
    write:/ lv_myintflag.
    CLEAR: lt_tab."clear records.
    "sets to 0
    lv_myintflag = strlen( condense( val = boolc( lines( lt_tab[] ) = 0 ) ) ).
    write:/ lv_myintflag.

  • Pass return to methods expecting ABAP_BOOL parameters,
  • PARAMETERS word TYPE c length 30.
    DATA result_tab TYPE cl_abap_docu=>search_results.
    "Show dialog or return tab value depending on batch/foregorund mode.
    cl_abap_docu=>start( EXPORTING word = word
    no_dialog = boolc( sy-batch IS NOT INITIAL )
    IMPORTING search_results = result_tab
    ).

When not to use BOOLC:

    Comparison against ABAP_BOOL types:

    Despite the name BOOL and C, the function doesn't return ABAP_BOOL type nor a C.
    Instead, it returns a STRING like result.
    Due to ABAP comparison rules when STRING is compared against type ABAP_BOOL( type 
    C length 1 internally), the later gets converted from C to STRING and ignores any blanks.

This leads to unexpected behavior, for example, this code prints "instead I will be printed."
"Run the code to beleive the output.
IF boolc( 1 = 2 ) = ABAP_FALSE.
WRITE: / `I should be preinted logically.`.
ELSE.
WRITE: / `instead I will be printed.`.
ENDIF.
Even though 1 = 2 is false , boolc returns a string with space which is compared to a converted string with ignored spaces( BLANK) hence the comparison fails.

XSDBOOL ( available from ABAP 740 SP08 )

  •    Set boolean flag:

    "Set boolean using logical expression.
    "if the table is empty, set the flag to true.
    lv_istab_empty = xsdbool( lines( lt_mytab[] ) = 0 ).

  •    Pass return to methods expecting ABAP_BOOL parameters.

    PARAMETERS word TYPE c length 30.
    DATA result_tab TYPE cl_abap_docu=>search_results.
    "Show dialog or return tab value depending on batch/foregorund mode.
    cl_abap_docu=>start( EXPORTING word = word
    no_dialog = xsdbool( sy-batch IS NOT INITIAL )
    IMPORTING search_results = result_tab
    ).

  •    Comparison against ABAP_BOOL.

    "This works as expected
    IF xsdbool( 1 = 2 ) = ABAP_FALSE.
    WRITE: / `I should be preinted logically.`.
    ELSE.
    WRITE: / `I should not be printed.`.
    ENDIF.

When not to use XSDBOOL:

    Comparison with STRING types:( Hope you see the pattern here)

    As discussed above,
    Due to ABAP comparison rules when STRING is compared against type ABAP_BOOL(                    type length 1 internally), the latter gets converted from to STRING and ignores any blanks.

Since XSDBOOL returns ABAP_BOOL, the false results get ignored.

"Run the code to beleive the output.
IF boolc( 1 = 2 ) = ABAP_FALSE.
WRITE: / `I should be preinted logically.`.
ELSE.
WRITE: / `instead I will be printed.`.
ENDIF.

Passing its return to ABAP built-in processing functions that take string type argument as input such as TRANSLATE would have an unexpected behaviour,
Picking the same example from BOOLC to set a custom flag,
This code doesn't work as expected due to conversion rules being at play.
report syst-repid.
DATA:
lt_tab TYPE string_table.
DATA:
lv_myflag TYPE char1.
START-OF-SELECTION.
" Use BOOLC to set custom flags.
"sets to Y
lv_myflag = TRANSLATE( val = xsdbool( lines( lt_tab[] ) = 0 ) from = `X ` to = `YN` ) .
write: lv_myflag.
" add record to itab.
append `Record` to lt_tab.
"sets to N
" Doesnt work as trailing space is removed when passing to VAL of translate function.
lv_myflag = TRANSLATE( val = xsdbool( lines( lt_tab[] ) = 0 ) from = `X ` to = `YN` ) .
write:/ lv_myflag.
DATA:
lv_myintflag TYPE i.
"sets to 1
lv_myintflag = strlen( condense( val = xsdbool( lines( lt_tab[] ) = 0 ) ) ).
write:/ lv_myintflag.
CLEAR: lt_tab."clear records.
"sets to 0
lv_myintflag = strlen( condense( val = xsdbool( lines( lt_tab[] ) = 0 ) ) ).
write:/ lv_myintflag.

I hope you liked the BOOLC and XSDBOOL use cases.

Both of the Boolean functions have their use cases and should be used appropriately. The pitfalls around their usage come due to the nature of  ABAP comparison rules and should be kept in mind.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP