Word Wrap Functionality in SAP ABAP ALV - MCQ Village

Word Wrap Functionality in SAP ABAP ALV

Hello friends in this article we are going to see. How to add Word Wrap function in SAP ABAP using simple program using function module FM.

Introduction

At times, there is a requirement to set an ALV Column to “word wrap” so that the text gets moved to the second line if there is too much text for the Column Width. ALV doesn’t provide the functionality to wrap the texts and hence, challenges are faced in case any column has long texts to be displayed in single instance. 

Purpose

The purpose of this document is to provide an option of “word wrap” to ALV functionality. Other options may be possible; however, they only work around and doesn’t provide end user the flexibility to view the texts in single instance. Few of the other alternatives are listed below –  

·         You can make the column text as link and show truncated text so that even if the whole text in a column is not seen, the user can see the entire text on mouse over.

·         You can implement functionality where in on clicking on the required cell, a popup comes with the entire text.  

However, in the above alternatives, incase end user needs to take print out of the report output, he will miss out the truncated texts. 

Dependencies/Pre-requisites/Assumption

Basic understanding of ALV, ABAP Programming is required to understand the flow and concept of ALV reporting.  

Technical Process

Limitations in Standard output of ALV

  • For demonstration purpose, I have used 2 columns – Employee ID (width of Character 10) and Employee Name (width of Character 20). However, Employee Name field contains values which are more than 20 characters.
  • Report output truncates the value after character 21
  • In case of Invoice printing/ Deliver document scenarios, this becomes as a challenge if the long text gets truncated. Truncated texts won’t make any sense to the end user and he won’t be able to take printouts!!!

Steps Involved to trigger Wrap Functionality

  • Identify the column that is required to be wrapped. Multiple columns can also be wrapped as per requirement.
  • Identify the length to which the column needs to be wrapped and define a field of same length. Different columns can be wrapped to different length, and hence logic needs to be modified accordingly.
  • Usually, for ALV output, data records internal table is defined to populate output. In order to implement wrap functionality, we need to declare another internal table with the desired wrapped length. This internal table will contain wrapped texts for the column. Internal table will need to have first column as identifier to match each record of parent internal table.
  • Standard Function Module RKD_WORD_WRAP is used to wrap the desired text.

IMPORT Parameters

  1. TEXTLINE (Mandatory) – Source text line
  2. DELIMITER (Optional) – Indicator, which is used as a separator. SPACE is set in our logic so that words don’t get wrapped in between. In case CSV file needs to be wrapped, delimiter needs to be set as comma
  3. OUTPUTLEN (Optional) – Maximum output line width. Default value is set to 35. TABLES
  4. OUT_LINES (Optional) – All output lines after wrapping get stored as table
  • REUSE_ALV_EVENTS_GET is called to get the list of all active events for particular ALV report output.
  • Populate appropriate FORM name for the event AFTER_LINE_OUTPUT.
  • Call function module REUSE_ALV_LIST_DISPLAY to populate report output. ALV List is used for demonstration; however, ALV Grid can also be used.
  • Implement AFTER_LINE_OUTPUT event among other events for the ALV. Below snapshot shows the logic that is required to trigger multiple lines.

Code :

*&---------------------------------------------------------------------
*& Report ZTEST_WRAP
*&---------------------------------------------------------------------
*& Wrap the column into multiple lines incase Column width is less
*&---------------------------------------------------------------------
REPORT ZTEST_WRAP.
TYPE-POOLS: SLIS.
TYPES: BEGIN OF TY_DATA,
        EMP       TYPE CHAR10,
        EMP_NAME  TYPE CHAR100,
        EMP_NAME1 TYPE CHAR20,
       END OF TY_DATA.
TYPES: BEGIN OF TY_WRD,
         EMP TYPE CHAR20,
       END OF TY_WRD.
CONSTANTS: C_LEN TYPE I VALUE 100.
DATA: REPORT_ID LIKE SY-REPID,
      IT_SENTENCE TYPE TABLE OF TY_WRD,
      WA_WORD TYPE TY_WRD,
      V_REPID TYPE SYST-REPID,
      V_TABIX TYPE SYST-TABIX.
DATA: WS_TITLE TYPE LVC_TITLE VALUE 'An ALV Report'.
DATA: IT_EVT TYPE SLIS_T_EVENT,
      IT_FLD TYPE SLIS_T_FIELDCAT_ALV,
      WA_FLD TYPE SLIS_FIELDCAT_ALV,
      WA_EVT TYPE SLIS_ALV_EVENT,
      WA_LAY TYPE SLIS_LAYOUT_ALV.
DATA: WATAB TYPE TY_DATA,
      I_DATA TYPE STANDARD TABLE OF TY_DATA,
      COUNT TYPE I VALUE 0.
* Number of records
DO 4 TIMES.
  COUNT = COUNT + 1.
  CASE COUNT.
    WHEN 1.
      WATAB-EMP = '10'.
      WATAB-EMP_NAME = 'Purpose of this tutorial is to provide an easy  
                        and quick reference which may be used as a             
                        guide'.
      APPEND WATAB TO I_DATA.
      CLEAR WATAB.
    WHEN 2.
      WATAB-EMP = '20'.
      WATAB-EMP_NAME = 'Coding is done for ALV List Display'.
      APPEND WATAB TO I_DATA.
      CLEAR WATAB.
    WHEN 3.
      WATAB-EMP = '30'.
      WATAB-EMP_NAME = 'Same functionality can be implemented for ALV 
                        Grid Display also'.
      APPEND WATAB TO I_DATA.
      CLEAR WATAB.
  ENDCASE.
ENDDO.
REPORT_ID = SY-REPID.
 CLEAR WA_FLD.
  WA_FLD-FIELDNAME = 'EMP'.
  WA_FLD-REF_TABNAME = 'I_DATA'.
  WA_FLD-SELTEXT_L = 'EMP. ID'.
  WA_FLD-REF_FIELDNAME = 'EMP'.
  APPEND WA_FLD TO IT_FLD.
  CLEAR WA_FLD.
  WA_FLD-FIELDNAME = 'EMP_NAME1'.
  WA_FLD-INTTYPE = 'CHAR'.
  WA_FLD-OUTPUTLEN = 20.
  WA_FLD-INTLEN = 20.
  WA_FLD-SELTEXT_L = 'EMP. NAME'.
  WA_FLD-DDICTXT = 'L'.
  APPEND WA_FLD TO IT_FLD.
* Word Wrap the text in multiple lines
LOOP AT I_DATA INTO WATAB.
  V_TABIX = SY-TABIX.
  CLEAR: IT_SENTENCE [].
  CALL FUNCTION 'RKD_WORD_WRAP'
   EXPORTING
     TEXTLINE                  = WATAB-EMP_NAME
     OUTPUTLEN                 = C_LEN
   TABLES
     OUT_LINES                 = IT_SENTENCE
   EXCEPTIONS
     OUTPUTLEN_TOO_LARGE       = 1
     OTHERS                    = 2
            .
  IF SY-SUBRC eq 0.
    IF NOT IT_SENTENCE IS INITIAL.
      READ TABLE IT_SENTENCE INTO WA_WORD INDEX 1.
        WATAB-EMP_NAME1 = WA_WORD-EMP.
        MODIFY I_DATA FROM WATAB INDEX V_TABIX.
    ENDIF.
  ENDIF.
ENDLOOP.
* Get event. We will handle BEFORE and AFTER line output
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
  IMPORTING
    ET_EVENTS = IT_EVT.
READ TABLE IT_EVT INTO WA_EVT WITH KEY NAME = SLIS_EV_AFTER_LINE_OUTPUT.
WA_EVT-FORM = SLIS_EV_AFTER_LINE_OUTPUT.
MODIFY IT_EVT FROM WA_EVT INDEX SY-TABIX.
WA_LAY-EDIT = 'X'.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
  EXPORTING
    I_CALLBACK_PROGRAM = REPORT_ID
    IS_LAYOUT          = WA_LAY
    IT_FIELDCAT        = IT_FLD
    IT_EVENTS          = IT_EVT
  TABLES
    T_OUTTAB           = I_DATA
  EXCEPTIONS
    PROGRAM_ERROR      = 1
    OTHERS             = 2.
IF SY-SUBRC <> 0.
* Exceptions will be handled as per requirement
ENDIF.
*---------------------------------------------------------------------*
* FORM AFTER_LINE_OUTPUT                                              *
*---------------------------------------------------------------------*
FORM AFTER_LINE_OUTPUT USING RS_LINEINFO TYPE SLIS_LINEINFO.
  CLEAR: IT_SENTENCE, WATAB.
  READ TABLE I_DATA INTO WATAB INDEX RS_LINEINFO-TABINDEX.
  CHECK SY-SUBRC = 0.
  CALL FUNCTION 'RKD_WORD_WRAP'
    EXPORTING
      TEXTLINE = WATAB-EMP_NAME
      OUTPUTLEN = C_LEN
    TABLES
      OUT_LINES = IT_SENTENCE.
  DESCRIBE TABLE IT_SENTENCE LINES V_TABIX.
  CHECK V_TABIX > 1.
  LOOP AT IT_SENTENCE INTO WA_WORD FROM 2.
    WRITE: / SY-VLINE,
    12 SY-VLINE,
    13 WA_WORD-EMP,
    33 SY-VLINE.
  ENDLOOP.
ENDFORM. "after_line_output


Modified Output of ALV after Wrap functionality

  • For demonstration purpose, I have used 2 columns – Employee ID (width of Character 10) and Employee Name (width of Character 20)
  • String of length 95 is passed to Employee name field which is of CHAR20 and this column is wrapped so that whole string is shown in multiple lines

Benefits

  • Desired Columns can be wrapped and shown in the same report output.
  • Multiple columns can be wrapped as per requirement. Also, different columns can be wrapped to different lengths, and hence logic needs to be modified accordingly.
  • Wrap length can be set dynamically and changed during run time as per desired functionality.
  • Even though string gets wrapped into multiple lines, however it’s treated as one line item. This provides an advantage incase HOTSPOT functionality is required over line items.
  • No hassles or logic is required to ensure that line gets split at words instead of in between the words as standard SAP function modules are used to achieve the same. Also, we can set custom delimiters for wrap functionality. Hence, CSV file can be wrapped at comma.
  • Print output will also show wrapped output and hence, word wrap functionality is quite helpful in scenarios like Invoice printing, Delivery Document.

Conclusion

  • Though SAP doesn’t provide Wrap functionality along with ALV standard features, however, we can achieve this though custom logic and coding.
  • This document can be referred by technical consultants to understand how wrap functionality can be implemented for ALV. This document doesn’t explain about ALV Events functionality.
  • The code can be reused with minimal changes as per requirement.

Leave a comment

%d bloggers like this:
CLOSE ADS
CLOSE ADS