Skip to Content

Description :

This Blog describes about dynamic creation of a decision table and it’s entries using  CL_FDT* classes Collection.

Pre-requisites:

  1. Basic knowledge on OO ABAP concepts
  2. Basic knowledge on BRF+ framework

Requirement : Create Decision Table and it’s entries dynamically in BRF plus  through simple report.

Interfaces used :


  1. IF_FDT_FACTORY
  2. IF_FDT_DECISION_TABLE
  3. IF_FDT_FUNCTION

Step 1 :  Create an application in the workbench level as show in below.

2015-08-17 16_08_06-BRFplus Workbench.png

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_elementposition = 1.

   ls_elementelement_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_elementposition = 2.

   ls_elementelement_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_elementposition = 3.

   ls_elementelement_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_columncol_no     = 1.

   ls_columnobject_id =   lv_element1_id.

   ls_columnis_result  = abap_false.

   INSERT ls_column INTO TABLE lts_column.

* Populate the second column element

   ls_columncol_no     = 2.

   ls_columnobject_id  = lv_element2_id.

   ls_columnis_result  = abap_false.

   INSERT ls_column INTO TABLE lts_column.

* Populate the result column element

   ls_columncol_no     = 3.

   ls_columnobject_id  = lv_element3_id.

   ls_columnis_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_datarow_no = 1.

   ls_table_datacol_no = 1.

   ls_rangeposition = 1.

     ls_rangesign     = if_fdt_range=>gc_sign_include.

     ls_rangeoption   = if_fdt_range=>gc_option_equal.

     CREATE DATA ls_ranger_low_value TYPE if_fdt_types=>element_text.

     ASSIGN ls_ranger_low_value->* TO <lv_value>.

     <lv_value> = ‘MOURI’.

     INSERT ls_range INTO TABLE ls_table_datats_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_datarow_no = 1.

   ls_table_datacol_no = 2.

     ls_rangeposition = 1.

     ls_rangesign     = if_fdt_range=>gc_sign_include.

     ls_rangeoption   = if_fdt_range=>gc_option_equal.

     CREATE DATA ls_ranger_low_value TYPE if_fdt_types=>element_text.

     ASSIGN ls_ranger_low_value->* TO <lv_value>.

     <lv_value> = ‘TECH’.

     INSERT ls_range INTO TABLE ls_table_datats_range.

     INSERT ls_table_data INTO TABLE lts_table_data.

     CLEAR ls_table_data.

**Set the result column in cell(1, 3)

   ls_table_datarow_no = 1.

   ls_table_datacol_no = 3.

   CREATE DATA ls_table_datar_value TYPE if_fdt_types=>element_text.

   ASSIGN ls_table_datar_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.


Capture.PNG


Step 4: Go to the BRF plus workbench and open the objectID as shown below.

Capture1.PNG

Click on search button.

2015-08-17 16_19_15-BRFplus Workbench.png

we successfully created decision table and it’s entries  dynamically.

we can also check this decision table under the application which created  in step1.

2015-08-17 16_26_22-BRFplus Workbench.png

Thank you for reading this blog!!

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Christian Lechner

    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:

    • Why did you need to use the API and in what business context does your example take place?
    • What are the advantages/disadvantages of the API from your point of view?
    • What might be the next steps after handling the creation (like an outlook)?
    • Did you overcome any problems when using the API


    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

    (0) 

Leave a Reply