{"id":1350,"date":"2015-03-18T15:13:17","date_gmt":"2015-03-18T14:13:17","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=1350"},"modified":"2015-03-18T15:16:37","modified_gmt":"2015-03-18T14:16:37","slug":"abap-grouping-fields-in-field-catalog","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=1350","title":{"rendered":"ABAP &#8211; Grouping fields in field catalog"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\" size-full wp-image-358 alignleft\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" alt=\"SAP\" width=\"44\" height=\"50\" \/>In some cases you find a report with ALV output where there are maybe hundreds of fields available to be displayed. This can be VERY confusing for the user when he tries to select just few fields he wants to display but all fields are listed in one HUGE list together.<\/p>\n<p><!--more--><\/p>\n<p>This example shows how to group fields that can be separated with some logic, e.g. fields from MARA, fields from MARC, fields from MARD, &#8230;<\/p>\n<p>When you create such groups, user can make a pre-selection by selecting the <em>GROUP <\/em>and then only fields belonging to that specific group are being offered for selection in ALV <em>FILTER<\/em> or <em>LAYOUT<\/em> change.<\/p>\n<p>Let&#8217;s take a look at how such code might look like<\/p>\n<p>First we define our global data and their types<\/p>\n<pre lang=\"abap\">TYPES:\r\n* Type definition for an ALV line\r\n  BEGIN OF ts_data,\r\n    mara TYPE mara,\r\n    marc TYPE marc,\r\n    mard TYPE mard,\r\n  END OF ts_data,\r\n\r\n* ALV lines table type definition\r\n  tt_data TYPE TABLE OF ts_data.\r\n\r\nDATA:\r\n  gt_data       TYPE tt_data,     \"ALV data\r\n  gt_fcat       TYPE lvc_t_fcat,  \"ALV Field Calatog\r\n  gt_alv_groups TYPE lvc_t_sgrp,  \"ALV Field Groups\r\n  go_grid       TYPE REF TO cl_gui_alv_grid,\r\n  go_container  TYPE REF TO cl_gui_custom_container.<\/pre>\n<p>Now we add the core functionality &#8211; we assume there is a screen 0100 created with custom control called GO_CONTAINER<\/p>\n<pre lang=\"abap\">START-OF-SELECTION.\r\n  PERFORM load_data CHANGING gt_data.\r\n\r\n  PERFORM build_fcat CHANGING gt_fcat\r\n                              gt_alv_groups.\r\n  CALL SCREEN 100.\r\n\r\n* This is the only PBO module of screen 0100\r\nMODULE 0100_pbo OUTPUT.\r\n  PERFORM display_alv.\r\nENDMODULE.<\/pre>\n<p>We load data from MARA (up to 10 entries) and all their corresponding sub-entries from tables MARC and MARD.<\/p>\n<pre lang=\"abap\">* Load data from MARA-MARC-MARD for up to 10 materials\r\nFORM load_data CHANGING ct_data TYPE tt_data.\r\n  DATA:\r\n    lt_mara TYPE TABLE OF mara,\r\n    lt_marc TYPE TABLE OF marc,\r\n    lt_mard TYPE TABLE OF mard,\r\n    ls_data TYPE ts_data.\r\n\r\n  SELECT *\r\n    FROM mara\r\n    INTO TABLE lt_mara\r\n    UP TO 10 ROWS.\r\n\r\n  SELECT *\r\n    FROM marc\r\n    INTO TABLE lt_marc\r\n    FOR ALL ENTRIES IN lt_mara\r\n    WHERE matnr = lt_mara-matnr.\r\n\r\n  SELECT *\r\n    FROM mard\r\n    INTO TABLE lt_mard\r\n    FOR ALL ENTRIES IN lt_marc\r\n    WHERE matnr = lt_marc-matnr\r\n      AND werks = lt_marc-werks.\r\n\r\n  LOOP AT lt_mara ASSIGNING FIELD-SYMBOL(&lt;ls_mara&gt;).\r\n    CLEAR ls_data.\r\n    ls_data-mara = &lt;ls_mara&gt;.\r\n\r\n    READ TABLE lt_marc \r\n      TRANSPORTING NO FIELDS \r\n      WITH KEY matnr = &lt;ls_mara&gt;-matnr.\r\n    IF sy-subrc &lt;&gt; 0.\r\n      APPEND ls_data TO ct_data.\r\n    ELSE.\r\n\r\n      LOOP AT lt_marc ASSIGNING FIELD-SYMBOL(&lt;ls_marc&gt;)\r\n        WHERE matnr = &lt;ls_mara&gt;-matnr.\r\n\r\n        ls_data-marc = &lt;ls_marc&gt;.\r\n\r\n        READ TABLE lt_mard \r\n          TRANSPORTING NO FIELDS \r\n          WITH KEY matnr = &lt;ls_mara&gt;-matnr \r\n                   werks = &lt;ls_marc&gt;-werks.\r\n        IF sy-subrc &lt;&gt; 0.\r\n          APPEND ls_data TO ct_data.\r\n        ELSE.\r\n\r\n          LOOP AT lt_mard ASSIGNING FIELD-SYMBOL(&lt;ls_mard&gt;)\r\n            WHERE matnr = &lt;ls_mara&gt;-matnr \r\n              AND werks = &lt;ls_marc&gt;-werks.\r\n\r\n            ls_data-mard = &lt;ls_mard&gt;.\r\n            APPEND ls_data TO ct_data.\r\n\r\n          ENDLOOP.\r\n        ENDIF.\r\n      ENDLOOP.\r\n    ENDIF.\r\n  ENDLOOP.\r\nENDFORM.<\/pre>\n<p>Here comes the interesting part &#8211; building field catalog where we specify the <em>Field-Groups<\/em><\/p>\n<pre lang=\"abap\">* Generate field catalog automatically for the 3 given tables\r\n* Also group their fields\r\nFORM build_fcat CHANGING ct_fcat       TYPE lvc_t_fcat\r\n                         ct_alv_groups TYPE lvc_t_sgrp.\r\n  PERFORM append_table_2_fcat USING 'MARA' 'A' CHANGING ct_fcat ct_alv_groups.\r\n  PERFORM append_table_2_fcat USING 'MARC' 'C' CHANGING ct_fcat ct_alv_groups.\r\n  PERFORM append_table_2_fcat USING 'MARD' 'D' CHANGING ct_fcat ct_alv_groups.\r\nENDFORM.\r\n\r\nFORM append_table_2_fcat USING iv_tabname\r\n                               iv_groupname\r\n                         CHANGING ct_fcat   TYPE lvc_t_fcat\r\n                                  ct_groups TYPE lvc_t_sgrp.\r\n  DATA:\r\n    lv_structure_name TYPE dd02l-tabname,\r\n    lt_fcat           TYPE lvc_t_fcat.\r\n\r\n  FIELD-SYMBOLS:\r\n    &lt;ls_fcat&gt;  LIKE LINE OF ct_fcat,\r\n    &lt;ls_group&gt; LIKE LINE OF ct_groups.\r\n\r\n* Create new group of fields if it does not exist yet\r\n  READ TABLE ct_groups \r\n    TRANSPORTING NO FIELDS \r\n    WITH KEY sp_group = iv_groupname.\r\n  IF sy-subrc &lt;&gt; 0.\r\n    APPEND INITIAL LINE TO ct_groups ASSIGNING .\r\n    &lt;ls_group&gt;-sp_group = iv_groupname.\r\n    &lt;ls_group&gt;-text     = iv_tabname.\r\n  ENDIF.\r\n\r\n* Generate field catalog entries for given table name\r\n  lv_structure_name = iv_tabname.\r\n  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'\r\n    EXPORTING\r\n      i_structure_name       = lv_structure_name\r\n      i_client_never_display = 'X'\r\n      i_bypassing_buffer     = 'X'\r\n    CHANGING\r\n      ct_fieldcat            = lt_fcat\r\n    EXCEPTIONS\r\n      inconsistent_interface = 1\r\n      program_error          = 2\r\n      OTHERS                 = 3.\r\n  CHECK sy-subrc = 0.\r\n\r\n* Modify the field catalog to match our data structure\r\n  LOOP AT lt_fcat ASSIGNING &lt;ls_fcat&gt;.\r\n    &lt;ls_fcat&gt;-ref_field = &lt;ls_fcat&gt;-fieldname.\r\n    &lt;ls_fcat&gt;-ref_table = iv_tabname.\r\n\r\n    &lt;ls_fcat&gt;-tabname   = 'GT_DATA'.\r\n    &lt;ls_fcat&gt;-fieldname = |{ iv_tabname }-{ &lt;ls_fcat&gt;-fieldname }|.\r\n\r\n**********************************************************************\r\n* THIS IS THE IMPORTANT PART\r\n    &lt;ls_fcat&gt;-sp_group  = iv_groupname.\r\n**********************************************************************\r\n\r\n*   Show only first 50 columns\r\n    IF lines( ct_fcat ) &gt;= 50.\r\n      &lt;ls_fcat&gt;-no_out = abap_true.\r\n    ENDIF.\r\n\r\n    APPEND &lt;ls_fcat&gt; TO ct_fcat.\r\n  ENDLOOP.\r\nENDFORM.<\/pre>\n<p>Now we just have to display data<\/p>\n<pre lang=\"abap\">FORM display_alv.\r\n  IF go_container IS INITIAL.\r\n    CREATE OBJECT go_container\r\n      EXPORTING\r\n        container_name = 'GO_CONTAINER'.\r\n\r\n    CREATE OBJECT go_grid\r\n      EXPORTING\r\n        i_parent = go_container.\r\n\r\n    go_grid-&gt;set_table_for_first_display(\r\n      EXPORTING\r\n        it_special_groups             =    gt_alv_groups\r\n      CHANGING\r\n        it_outtab                     =    gt_data\r\n        it_fieldcatalog               =    gt_fcat \" Field Catalog\r\n    ).\r\n  ENDIF.\r\nENDFORM.<\/pre>\n<p>This is the output and if you go to the FILTER or LAYOUT modification, you should be able to filter by the groups you defined:<\/p>\n<p><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_LAYOUT.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1351\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=1351\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_LAYOUT.png\" data-orig-size=\"746,527\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Filtering fields in LAYOUT change\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_LAYOUT.png\" class=\"alignnone size-medium wp-image-1351\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_LAYOUT-300x212.png\" alt=\"Filtering fields in LAYOUT change\" width=\"300\" height=\"212\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_LAYOUT-300x212.png 300w, https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_LAYOUT-425x300.png 425w, https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_LAYOUT.png 746w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_FILTER.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1352\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=1352\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_FILTER.png\" data-orig-size=\"588,449\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Filtering fields in FILTER change\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_FILTER.png\" class=\"alignnone size-medium wp-image-1352\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_FILTER-300x229.png\" alt=\"Filtering fields in FILTER change\" width=\"300\" height=\"229\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_FILTER-300x229.png 300w, https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_FILTER-393x300.png 393w, https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_GROUPS_FILTER.png 588w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In some cases you find a report with ALV output where there are maybe hundreds of fields available to be displayed. This can be VERY confusing for the user when he tries to select just few fields he wants to &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=1350\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"ABAP - Grouping fields in field catalog","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,86,136,9,222],"tags":[447,415,416],"class_list":["post-1350","post","type-post","status-publish","format-standard","hentry","category-abap","category-alv","category-alv-tutorial","category-development","category-dynpro","tag-alv","tag-lvc_t_fcat","tag-lvc_t_sgrp"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-lM","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1350"}],"version-history":[{"count":2,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1350\/revisions"}],"predecessor-version":[{"id":1354,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1350\/revisions\/1354"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}