SAP ABAP | Report On Scenario
Tags: abap, sap, sapabap, erp
Published: 2023-01-29 13:20:41

<u>**SAP ABAP**</u>
_Scenario:_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5i5kjntk1avt0unme0ns.png)

**If clicking on First Radio Button,**
Table name should be pass in parameter
The Fields Should display related to table 
- Table Name
- Field Name
- Data Element 
- Value Table

**If clicking on Second Radio Button,**
Structure name should be pass in parameter
The Fields Should display related to table
- Table Name
- Field Name
- Data Element 
- Value Table

**If clicking on Third Radio Button,**
Two Parameters should be there, the first parameter should given table/structure name, second parameter should be field name related to first parameter field (table/structure), Then  Fields Should display related to table
- Table Name
- Field Name
- Data Element 
- Value Table

**Validations:**
- Passing Of Wrong parameter data, should throw an message
- Leaving Blank & execute, should throw an message

**ABAP Concepts Need To use:**
- RTTS
- ALV
---
<u>**_Building Of Report_**</u>

**Note:**
**_I Used 8 Includes:_** 

**_First INCLUDE: <u>Z_DATA</u>._**
(Defining Parameters, Data & Field Symbol)
```
PARAMETERS: p_rb1 RADIOBUTTON GROUP rg DEFAULT 'X' USER-COMMAND rbg,
            p_rb2 RADIOBUTTON GROUP rg,
            p_rb3 RADIOBUTTON GROUP rg.

PARAMETERS: p_var3 TYPE dd03l-tabname MODIF ID rb1,
            p_var4 TYPE dd02l-tabname MODIF ID rb2.


TYPES: BEGIN OF ty_dd03l,
         tabname   TYPE dd03l-tabname,
         fieldname TYPE dd03l-fieldname,
         rollname  TYPE dd03l-rollname,    "Data element
         entitytab TYPE dd04l-entitytab,   "Value table
       END OF ty_dd03l,

       BEGIN OF ty_dd04l,
         rolname   TYPE dd04l-rollname,
         entitytab TYPE dd04l-entitytab,
       END OF ty_dd04l.


DATA: gt_dd03l TYPE STANDARD TABLE OF ty_dd03l,
      gt_dd04l TYPE STANDARD TABLE OF ty_dd04l.

DATA: lo_temp_table      TYPE REF TO cl_abap_tabledescr,
      lo_ty_struct       TYPE REF TO cl_abap_structdescr,
      lo_ref_final_table TYPE REF TO data.


FIELD-SYMBOLS: <fs_table> TYPE ANY TABLE.
```
**_Second INCLUDE: <u>ZRB_OPE</u>._**
(Radio Button Logic (AT SELECTION-SCREEN OUTPUT))
```
AT SELECTION-SCREEN OUTPUT.

  LOOP AT SCREEN.

    IF p_rb1 IS NOT INITIAL.
      IF screen-group1 EQ 'RB2'.
        screen-active = 0.
        IF screen-group1 EQ 'RB1'.
          screen-active = 1.
        ENDIF.
      ENDIF.

    ELSEIF p_rb2 IS NOT INITIAL.
      IF screen-group1 EQ 'RB2'.
        screen-active = 0.
        IF screen-group1 EQ 'RB1'.
          screen-active = 1.
        ENDIF.
      ENDIF.

    ELSEIF p_rb3 IS NOT INITIAL.
      IF screen-group1 EQ 'RB1'.
        screen-active = 1.
        IF screen-group1 EQ 'RB2'.
          screen-active = 1.
        ENDIF.
      ENDIF.
    ENDIF.

    MODIFY SCREEN.

  ENDLOOP.
```
**_Third INCLUDE: <u>ZDATA_RTTS_OPE</u>._**
(RTTS - Defining Operation of Data (START-OF-SELECTION))
```
START-OF-SELECTION.

  lo_temp_table =
  CAST cl_abap_tabledescr(
       cl_abap_tabledescr=>describe_by_data( p_data = gt_dd03l  )
       ).

  lo_ty_struct ?= lo_temp_table->get_table_line_type( ).

  DATA(p_table_comp) = lo_ty_struct->get_components( ).

  CREATE DATA lo_ref_final_table TYPE HANDLE lo_temp_table.

  ASSIGN lo_ref_final_table->* TO <fs_table>.
```
**_Fourth INCLUDE: <u>ZCL_DEF_RB_TOPE</u>._**
(Assigning Class Definition)
```
CLASS cl_main DEFINITION.

  PUBLIC SECTION.
    METHODS: lm_table,
             lm_structure,
             lm_field.

ENDCLASS.
```
**_Fifth INCLUDE: <u>ZCL_IMP_TB.</u>._**
(Radio Button 01 - Table Operation)
```
CLASS cl_main IMPLEMENTATION.

  METHOD lm_table.

    IF p_rb1 = abap_true.
      IF p_var3 IS INITIAL.
        MESSAGE ID 'zso_msg' TYPE 'I' NUMBER '001'.
      ELSE.

        SELECT * FROM dd02l INTO TABLE @DATA(gt_temp2) WHERE tabname = @p_var3 AND tabclass = 'TRANSP'.

        IF sy-subrc = 0.

          SELECT dd03l~tabname,
                 dd03l~fieldname,
                 dd03l~rollname,
                 dd04l~entitytab
            INTO TABLE @<fs_table>
            FROM dd03l INNER JOIN dd04l
            ON dd03l~rollname = dd04l~rollname
               WHERE tabname = @p_var3.

          LOOP AT gt_dd03l ASSIGNING FIELD-SYMBOL(<fs_abc3>).
            IF <fs_abc3>-entitytab IS INITIAL.
              <fs_abc3>-entitytab = 'N/A'.
            ENDIF.
          ENDLOOP.

          TRY.
              CALL METHOD cl_salv_table=>factory
                IMPORTING
                  r_salv_table = DATA(lo_alv)
                CHANGING
                  t_table      = <fs_table>.

            CATCH cx_salv_msg INTO DATA(lo_exception).

              DATA(lv_msg)  = lo_exception->get_text( ).

          ENDTRY.

          lo_alv->display( ).

        ELSE.

          MESSAGE 'Input is not valid' TYPE 'S' DISPLAY LIKE 'E'.

        ENDIF.
      ENDIF.
    ENDIF.
  ENDMETHOD.
```
**_Sixth INCLUDE: <u>ZCL_IMP_STR</u>._**
(Radio Button 02 - Structure Operation)
```
METHOD lm_structure.

    IF p_rb2 = abap_true.

      IF p_var3 IS INITIAL.
        MESSAGE 'Structure Name Is Required' TYPE 'S' DISPLAY LIKE 'E'.

      ELSE.
        SELECT * FROM dd02l INTO TABLE @DATA(gt_temp2) WHERE tabname = @p_var3 AND tabclass = 'INTTAB'.
        IF sy-subrc = 0.
          SELECT dd03l~tabname,dd03l~fieldname,dd03l~rollname,dd04l~entitytab
            INTO TABLE @gt_dd03l
            FROM dd03l INNER JOIN dd04l
            ON dd03l~rollname = dd04l~rollname
               WHERE tabname = @p_var3.
          LOOP AT gt_dd03l ASSIGNING FIELD-SYMBOL(<fs_abc1>).
            IF <fs_abc1>-entitytab IS INITIAL.
              <fs_abc1>-entitytab = 'N/A'.
            ENDIF.
          ENDLOOP.


          TRY.
              CALL METHOD cl_salv_table=>factory
                IMPORTING
                  r_salv_table = DATA(lo_alv)
                CHANGING
                  t_table      = gt_dd03l.
            CATCH cx_salv_msg INTO DATA(lo_exception).
              DATA(lv_msg)  = lo_exception->get_text( ).
          ENDTRY.

          lo_alv->display( ).

        ELSE.

          MESSAGE 'Input is not valid' TYPE 'S' DISPLAY LIKE 'E'.

        ENDIF.
      ENDIF.
    ENDIF.
  ENDMETHOD.
```
**_Seventh INCLUDE: <u>ZCL_IMP_TB_ST_FD</u>._**
(Radio Button 03 - Table/Structure + Field Operation)
```
METHOD lm_field.

    IF p_rb3 = abap_true.

      IF p_var3 IS INITIAL.
        MESSAGE 'Table/Strucure Name is required' TYPE 'S' DISPLAY LIKE 'E'.
      ELSEIF p_var4 IS INITIAL.
        MESSAGE 'Field is required related to Table/Strucure' TYPE 'S' DISPLAY LIKE 'E'.

      ELSE.
        SELECT * FROM dd03l INTO TABLE @DATA(gt_data) WHERE tabname = @p_var3 AND fieldname = @p_var4.
        IF sy-subrc = 0.
          SELECT dd03l~tabname,dd03l~fieldname,dd03l~rollname,dd04l~entitytab
          INTO  TABLE @gt_dd03l
          FROM dd03l INNER JOIN dd04l
          ON dd03l~rollname = dd04l~rollname
             WHERE tabname = @p_var3 AND fieldname = @p_var4.
          LOOP AT gt_dd03l ASSIGNING FIELD-SYMBOL(<fs_abc2>).
            IF <fs_abc2>-entitytab IS INITIAL.
              <fs_abc2>-entitytab = 'N/A'.
            ENDIF.
          ENDLOOP.

          TRY.
              CALL METHOD cl_salv_table=>factory
                IMPORTING
                  r_salv_table = DATA(lo_alv)
                CHANGING
                  t_table      = gt_dd03l.

            CATCH cx_salv_msg INTO DATA(lo_exception).

              DATA(lv_msg)  = lo_exception->get_text( ).

          ENDTRY.

          lo_alv->display( ).

        ELSE.
          MESSAGE 'Input is not valid' TYPE 'S' DISPLAY LIKE 'E'.
        ENDIF.

      ENDIF.
    ENDIF.
  ENDMETHOD.

ENDCLASS.
```
**_Eigth INCLUDE: <u>ZCL_OP</u>._**
(Class Implementation Result O/P Data (START-OF-SELECTION))
```
START-OF-SELECTION.
  DATA(lo_rttc) = NEW cl_main( ).
  lo_rttc->lm_table( ).
  lo_rttc->lm_structure( ).
  lo_rttc->lm_field( ).
```
---
**<u>_Report Output Execution_</u>**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lity8y1spbhlsgrrddg0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hi5ps6oihvc0mclfftp3.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5xyilroptsjcsd6pmc1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dz65zg1m7atmp5vueeop.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uf8npx1fpdb8hy7p4a5q.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0olj08bwtpt8oxpcday2.png)

---
**_Validation - Execution_**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aw542n80i9qu7sxpo533.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbv68p6mf7yengd07hkg.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcsvdz9434pz10bv3meb.png)

---
Thank You :)