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 custom flags like Y/N or 0/1 using ABAP built-in processing functions.
  • Pass return to methods expecting ABAP_BOOL parameters,

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."
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:

  •    Pass return to methods expecting ABAP_BOOL parameters.

  •    Comparison against ABAP_BOOL.

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.

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.

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

Application Logging in ABAP