Create decision table & it’s entries dynamically in BRF + Workbench through API
Description :
This Blog describes about dynamic creation of a decision table and it’s entries using CL_FDT* classes Collection.
Pre-requisites:
- Basic knowledge on OO ABAP concepts
- Basic knowledge on BRF+ framework
Requirement : Create Decision Table and it’s entries dynamically in BRF plus through simple report.
Interfaces used :
- IF_FDT_FACTORY
- IF_FDT_DECISION_TABLE
- IF_FDT_FUNCTION
Step 1 : Create an application in the workbench level as show in below.
Create decision table dynamically for above application.
Step2: Please paste the following code snippet in your report, required steps are explained in detail through comments.
DATA: lo_factory TYPE REF TO if_fdt_factory,
lt_message TYPE if_fdt_types=>t_message,
lv_message TYPE string,
lv_boolean TYPE abap_bool,
lo_element TYPE REF TO if_fdt_element,
lo_table TYPE REF TO if_fdt_table,
lo_structure TYPE REF TO if_fdt_structure,
lv_element1_id TYPE if_fdt_types=>id,
lv_element2_id TYPE if_fdt_types=>id,
lv_element3_id TYPE if_fdt_types=>id,
lv_structure_id TYPE if_fdt_types=>id,
lo_constant TYPE REF TO if_fdt_constant,
ls_element TYPE if_fdt_structure=>s_element,
lts_element TYPE if_fdt_structure=>ts_element,
lv_string TYPE string,
ls_range TYPE if_fdt_decision_table=>s_range,
ls_table_data TYPE if_fdt_decision_table=>s_table_data,
lo_decision_table TYPE REF TO if_fdt_decision_table,
lo_function TYPE REF TO if_fdt_function,
lo_context TYPE REF TO if_fdt_context,
lts_context_id TYPE if_fdt_types=>ts_object_id,
lts_table_data TYPE if_fdt_decision_table=>ts_table_data,
lts_column TYPE if_fdt_decision_table=>ts_column,
lv_actv_failed TYPE abap_bool,
lx_fdt TYPE REF TO cx_fdt,
lv_dt_id TYPE if_fdt_types=>id,
ls_column LIKE LINE OF lts_column.
FIELD-SYMBOLS: <ls_message> TYPE if_fdt_types=>s_message,
<lv_value> TYPE any.
* Get instance of the FDT factory.
lo_factory = cl_fdt_factory=>if_fdt_factory~get_instance(
‘002590F0DB231ED591961054F1DE0488’ ).“get the object id of the application
*which was created in the workbench
*creation of data objects.
lo_element ?= lo_factory->get_data_object(
iv_data_object_type = if_fdt_constants=>gc_data_object_type_element ).
lo_element->if_fdt_transaction~enqueue( ).
lo_element->if_fdt_admin_data~set_name( ‘IV_VAR1’ ).
lo_element->set_element_type( if_fdt_constants=>gc_element_type_text ).
lo_element->if_fdt_transaction~activate(
IMPORTING
et_message = lt_message
ev_activation_failed = lv_boolean ).
IF lv_boolean EQ abap_true.
* for some reason the activation failed -> individual handling needed
lo_element->if_fdt_transaction~dequeue( ).
ELSE.
lo_element->if_fdt_transaction~save( ).
lo_element->if_fdt_transaction~dequeue( ).
* usually it makes sense to store the id for later access to the application
lv_element1_id = lo_element->mv_id.
ls_element–position = 1.
ls_element–element_id = lv_element1_id.
APPEND ls_element TO lts_element.
ENDIF.
INSERT lv_element1_id INTO TABLE lts_context_id.
* Create another element
lo_element ?= lo_factory->get_data_object(
iv_data_object_type = if_fdt_constants=>gc_data_object_type_element ).
lo_element->if_fdt_transaction~enqueue( ).
lo_element->if_fdt_admin_data~set_name( ‘IV_VAR2’ ).
* set the element-type (see if_fdt_constants=>gc_element_type_* for
* a list of available element types)
lo_element->set_element_type( if_fdt_constants=>gc_element_type_text ).
lo_element->if_fdt_transaction~activate(
IMPORTING
et_message = lt_message
ev_activation_failed = lv_boolean ).
IF lv_boolean EQ abap_true.
* for some reason the activation failed -> individual handling needed
lo_element->if_fdt_transaction~dequeue( ).
ELSE.
lo_element->if_fdt_transaction~save( ).
lo_element->if_fdt_transaction~dequeue( ).
* usually it makes sense to store the id for later access to the application
lv_element2_id = lo_element->mv_id.
ls_element–position = 2.
ls_element–element_id = lv_element2_id.
APPEND ls_element TO lts_element.
ENDIF.
INSERT lv_element2_id INTO TABLE lts_context_id.
* create a result data element
lo_element ?= lo_factory->get_data_object(
iv_data_object_type = if_fdt_constants=>gc_data_object_type_element ).
lo_element->if_fdt_transaction~enqueue( ).
lo_element->if_fdt_admin_data~set_name( ‘EV_RESULT’ ).
lo_element->set_element_type( if_fdt_constants=>gc_element_type_text ).
lo_element->if_fdt_transaction~activate(
IMPORTING
et_message = lt_message
ev_activation_failed = lv_boolean ).
IF lv_boolean EQ abap_true.
* for some reason the activation failed -> individual handling needed
lo_element->if_fdt_transaction~dequeue( ).
ELSE.
lo_element->if_fdt_transaction~save( ).
lo_element->if_fdt_transaction~dequeue( ).
* usually it makes sense to store the id for later access to the application
lv_element3_id = lo_element->mv_id.
ls_element–position = 3.
ls_element–element_id = lv_element3_id.
APPEND ls_element TO lts_element.
ENDIF.
INSERT lv_element3_id INTO TABLE lts_context_id.
* Populate the first column element
ls_column–col_no = 1.
ls_column–object_id = lv_element1_id.
ls_column–is_result = abap_false.
INSERT ls_column INTO TABLE lts_column.
* Populate the second column element
ls_column–col_no = 2.
ls_column–object_id = lv_element2_id.
ls_column–is_result = abap_false.
INSERT ls_column INTO TABLE lts_column.
* Populate the result column element
ls_column–col_no = 3.
ls_column–object_id = lv_element3_id.
ls_column–is_result = abap_true.
INSERT ls_column INTO TABLE lts_column.
* Create and set the Decision Table Expression.
lo_decision_table ?= lo_factory->get_expression( iv_expression_type_id = if_fdt_constants=>gc_exty_decision_table ).
* Enqueue the Decision Table Expression.
lo_decision_table->if_fdt_transaction~enqueue( abap_true ).
* Set the columns of the table
lo_decision_table->set_columns( its_column = lts_column ).
lo_decision_table->if_fdt_admin_data~set_name( ‘DT_TEST’ ). “user defined name. DT_TEST is the decision table Name
* Create a function instance using factory object.
lo_function ?= lo_factory->get_function( ).
* Enqueue the function.
lo_function->if_fdt_transaction~enqueue( ).
* Set the context data object for the function.
lo_function->set_context_data_objects( lts_context_id ).
lo_function->if_fdt_admin_data~set_name( ‘FN_TEST’ ). “user defined name . FN_TEST is the function Name
* Set the root expression of the function.
lo_function->set_expression( lo_decision_table->mv_id ).
* Set the condition for column1 in cell(1,2).
ls_table_data–row_no = 1.
ls_table_data–col_no = 1.
ls_range–position = 1.
ls_range–sign = if_fdt_range=>gc_sign_include.
ls_range–option = if_fdt_range=>gc_option_equal.
CREATE DATA ls_range–r_low_value TYPE if_fdt_types=>element_text.
ASSIGN ls_range–r_low_value->* TO <lv_value>.
<lv_value> = ‘MOURI’.
INSERT ls_range INTO TABLE ls_table_data–ts_range.
INSERT ls_table_data INTO TABLE lts_table_data.
CLEAR ls_table_data.
.
* Set the condition for column2 in cell(1,2).
ls_table_data–row_no = 1.
ls_table_data–col_no = 2.
ls_range–position = 1.
ls_range–sign = if_fdt_range=>gc_sign_include.
ls_range–option = if_fdt_range=>gc_option_equal.
CREATE DATA ls_range–r_low_value TYPE if_fdt_types=>element_text.
ASSIGN ls_range–r_low_value->* TO <lv_value>.
<lv_value> = ‘TECH’.
INSERT ls_range INTO TABLE ls_table_data–ts_range.
INSERT ls_table_data INTO TABLE lts_table_data.
CLEAR ls_table_data.
**Set the result column in cell(1, 3)
ls_table_data–row_no = 1.
ls_table_data–col_no = 3.
CREATE DATA ls_table_data–r_value TYPE if_fdt_types=>element_text.
ASSIGN ls_table_data–r_value->* TO <lv_value>.
<lv_value> = ‘MOURITECH’.
INSERT ls_table_data INTO TABLE lts_table_data.
CLEAR ls_table_data.
* Set the complete table data.
lo_decision_table->set_table_data( its_data = lts_table_data ).
* Perform deep activation & saving.
lo_function->if_fdt_transaction~activate( EXPORTING iv_deep = abap_true
IMPORTING et_message = lt_message
ev_activation_failed = lv_actv_failed ).
* If the activation is successful save all object and release all locks
* else only release the locks.
IF lv_actv_failed EQ abap_true.
lo_function->if_fdt_transaction~dequeue(
EXPORTING iv_deep = abap_true ).
WRITE : / ‘Deep activation failed’.
LOOP AT lt_message ASSIGNING <ls_message>.
MESSAGE ID <ls_message>–msgid TYPE <ls_message>–msgty NUMBER <ls_message>–msgno
WITH <ls_message>–msgv1 <ls_message>–msgv2 <ls_message>–msgv3 <ls_message>–msgv4
INTO lv_message.
WRITE: / ‘Reason : -‘,lv_message.
ENDLOOP.
ELSE.
TRY.
lv_dt_id = lo_decision_table->mv_id.
lo_function->if_fdt_transaction~save(
EXPORTING iv_deep = abap_true ).
WRITE : ‘The ID of the decision table created is:’ ,lv_dt_id. ‘.
CATCH cx_fdt INTO lx_fdt.
WRITE : / ‘Save failed with exception’.
LOOP AT lx_fdt->mt_message ASSIGNING <ls_message>.
WRITE :/ <ls_message>–text.
ENDLOOP.
ENDTRY.
lo_function->if_fdt_transaction~dequeue(
EXPORTING iv_deep = abap_true ).
ENDIF.
Step 3 : Click on execute button.
Step 4: Go to the BRF plus workbench and open the objectID as shown below.
Click on search button.
we successfully created decision table and it’s entries dynamically.
we can also check this decision table under the application which created in step1.
Thank you for reading this blog!!
Hi Swathi,
this is a nice and structured blog about the usage of the BRFplus API in order to create a decision table and entries in that table. Nevertheless I would encourage you to add more "personal" information to the blog like:
This information would add a lot of value for the community when reading your blog and this should be one of the goals when creating a blog.
Cheers
Christian