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,
"Set boolean using logical expression. | |
"if the table is empty, set the flag to true. | |
lv_istab_empty = boolc( lines( lt_mytab[] ) = 0 ). |
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. |
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.
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. |
XSDBOOL ( available from ABAP 740 SP08 )
- Set boolean flag:This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
"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.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
"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,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. |
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.