Quantcast
Channel: Tweaks
Viewing all articles
Browse latest Browse all 66

Utility Program to Auto Format the Texts into Meaningful Sentence

$
0
0

PO Texts in the Material Master – Long Text Wrap up Issue and Resolution

Issue: When loading PO texts in the Material master from a flat file, the long text do not wrap up automatically into a meaningful sentence.

Business Need: Our Client’s master data team came up with the above issue and we were trying to find a solution as to how to make the text truncate at the right place so that the meaning of the sentence remains intact.

Out of curiosity, I started looking at various standard programs and debugged the standard programs, to figure out how the SAP does it so cleanly.

The solution was right under our nose. T-Code SO10 (standard text) does it very simple and straight.

The text upload from a flat file is perfectly wrapped and truncated at appropriate places to keep the words and the sentence as a whole meaningful.

Analysis: Started debugging SO10 t-code from this point, to figure out how the file is wrapped by SAP. Go to t-code SO10 and hit the create button.

Note – The input file is not formatted at all.

longtext truncate in sap

Hit the Upload from Text menu to import a text file.
Text Wrap in SAP

Choose ASCII.ASCII format upload by SAPThe default Code Page used by SAP is ‘4110’.

codepage in sap

All the Code Pages in SAP can be found in table TCP00 (SAP code page catalog).

Codepage table in sap

Look, SAP auto formats the imported texts into meaningful sentences without breaking the work in the middle.

Long text Formatting in SAP

Also ReadUnwanted ‘#’ in the file

The inference from the above Analysis:

1. Read the file using the below FM.

SAP uses FM ‘WS_FILENAME_GET’ to read the file.

CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_filename     = pa_file
mask             = ',*.txt.'
mode             = 'O'
title            = 'Upload File'(078)
IMPORTING
filename         = pa_file
EXCEPTIONS
inv_winsys       = 1
no_batch         = 2
selection_cancel = 3
selection_error  = 4
OTHERS           = 5.

Step 2: FM to read the file.

SAP uses FM ‘IMPORT_TEXT’ in ASCII format with code page 4110. The table itf_lines will automatically have the begin of the paragraph (indicator ‘/’). This is the key FM which formats the long text into a meaningful sentence.

* -- Import the file contents to the internal table !!! 
CALL FUNCTION 'IMPORT_TEXT'
EXPORTING
codepage        = c_codepage
file            = pa_file
format_type     = c_ascii
word_langu      = sy-langu
TABLES
itf_lines       = gt_lines[]
EXCEPTIONS
file_open_error = 1
file_read_error = 2
upload_error    = 3
no_contents     = 4
OTHERS          = 5.

Remember, our goal is to send the text to FM SAVE_TEXT. But LINES table of SAVE_TEXT has a length of 132 characters. If your longtext is greater than 132 character, then it would be difficult to break the sentence correctly. Therefore, using this FM IMPORT_TEXT is very helpful. It automatically breaks the input longtext from file to good sentence and it is ready to be passed to SAVE_TEXT to upload to our desired destination.

Note: Make sure the code page is  4110 and is ASCII format.

If you use GUI_UPLOAD instead of the above FM, then you need to write your own intelligent logic to break the word at the right place and keep it meaningful. Check this example screenshot below where a long text is in li_tab as a very long string and we do not know how to pass correct lines to SAVE_TEXT.

The problem with GUI_UPLOAD is that it is not easy to break the sentence correctly using some custom logic. We cannot truncate the word in between. So the above FM IMPORT_TEXT is very useful in breaking one long string into multiple rows of meaningful sentences.

GUI_UPLOAD issue

Q: How to Identify PO text for different material in the file.
Ans: SAP automatically puts ‘/’ for every new paragraph. So every material PO text is separated by ‘/’, i.e. tdformat = ‘/’ (in itf_lines of IMPORT_TEXT).

Also Read – Playing Sherlock Holmes to detect CONVT_CODEPAGE runtime error mystery

Sample Program Developed to simulate the SAP behavior

Selection screen:  

CONSTANTS : c_codepage TYPE tcp02-cpcodepage  VALUE '4110',
             c_ascii(5) TYPE c  VALUE 'ASCII'.
 * -- Types declaration
 TYPES : BEGIN OF gts_err,
           matnr TYPE matnr,
           error TYPE bapi_msg,
         END OF gts_err.
 
 * -- Data declarations !!!
 DATA : gt_lines TYPE TABLE OF tline,
        gs_line  LIKE LINE  OF gt_lines,
        gs_lines LIKE LINE  OF gt_lines.
 
 DATA : gs_header LIKE thead,
        gv_matnr  TYPE matnr_d,
        gv_name   TYPE tdobname.
 
 DATA:  gt_lines_save TYPE STANDARD TABLE OF tline,
        gs_lines_save LIKE LINE OF  gt_lines_save.
 
 DATA: gt_err     TYPE TABLE OF gts_err,
       gs_err     LIKE LINE  OF gt_err,
       gr_message TYPE REF   TO cx_salv_msg.
 
 *-- REFERENCE definitions                                                *
 DATA gx_alv TYPE REF TO cl_salv_table.
 
 * -- Selection screen.. !!!
 SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
 PARAMETERS: pa_file  LIKE rlgrap-filename OBLIGATORY .
 SELECTION-SCREEN END OF BLOCK b1.
 
 * -- At Selection screen
 AT SELECTION-SCREEN ON VALUE-REQUEST FOR pa_file.
   CALL FUNCTION 'WS_FILENAME_GET'
     EXPORTING
       def_filename     = pa_file
       mask             = ',*.txt.'
       mode             = 'O'
       title            = 'Upload File'(078)
     IMPORTING
       filename         = pa_file
     EXCEPTIONS
       inv_winsys       = 1
       no_batch         = 2
       selection_cancel = 3
       selection_error  = 4
       OTHERS           = 5.
 
 INITIALIZATION.
 *Program level Authorization check.
   CALL FUNCTION 'AUTHORITY_CHECK_TCODE'
     EXPORTING
       tcode  = 'ZMM_XXX'
     EXCEPTIONS
       ok     = 0
       not_ok = 2
       OTHERS = 3.
   IF sy-subrc NE 0.
     MESSAGE s000(zs) WITH text-014 DISPLAY LIKE 'E'.
     LEAVE PROGRAM.
   ENDIF.
 
 START-OF-SELECTION.
 * -- Read the file contents on the internal table !!!
   CALL FUNCTION 'IMPORT_TEXT'
     EXPORTING
       codepage        = c_codepage  " 4110
       file            = pa_file
       format_type     = c_ascii
       word_langu      = sy-langu
     TABLES
       itf_lines       = gt_lines[]
     EXCEPTIONS
       file_open_error = 1
       file_read_error = 2
       upload_error    = 3
       no_contents     = 4
       OTHERS          = 5.
   IF sy-subrc <> 0.
     MESSAGE e000(zm) WITH text-err DISPLAY LIKE 'I'.
   ENDIF.
 
   DATA lx_columns TYPE REF TO cl_salv_columns_table.
 * -- Process the data for final output.
   gs_header-tdobject = 'MATERIAL'.
   gs_header-tdid     = 'BEST'.
   gs_header-tdspras  = sy-langu.
 
   LOOP AT gt_lines INTO gs_lines.
 
     REPLACE ALL OCCURRENCES OF '"' IN gs_lines-tdline WITH space.
 
     READ TABLE gt_lines INTO gs_line INDEX sy-tabix + 1 .
 
     IF gs_lines-tdformat = '/' . " Begin of new record !!!
       CLEAR gv_matnr.
       SPLIT gs_lines-tdline AT '~~' INTO gv_matnr gs_lines-tdline.
       CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
         EXPORTING
           input  = gv_matnr
         IMPORTING
           output = gv_matnr
         EXCEPTIONS
           OTHERS = 1.
 
       CLEAR gv_name.
       gs_header-tdname   =  gv_matnr.
       gs_lines_save      =  gs_lines.
 
       APPEND gs_lines_save TO gt_lines_save.
       CLEAR gs_lines_save.
     ELSE.
       gs_lines_save = gs_lines.
       APPEND gs_lines_save TO gt_lines_save.
       CLEAR  gs_lines_save.
     ENDIF.
 
     IF gs_line-tdformat EQ '/'.
       CALL FUNCTION 'SAVE_TEXT'
         EXPORTING
           client          = sy-mandt
           header          = gs_header
           savemode_direct = 'X'
         IMPORTING
           newheader       = gs_header
         TABLES
           lines           = gt_lines_save[]
         EXCEPTIONS
           id              = 1
           language        = 2
           name            = 3
           object          = 4
           OTHERS          = 5.
       IF sy-subrc <> 0.
         gs_err-matnr = gv_matnr.
         gs_err-error = text-upr.
         APPEND gs_err TO gt_err.
       ELSE.
         gs_err-matnr = gv_matnr.
         gs_err-error = text-ups.
         APPEND gs_err TO gt_err.
       ENDIF.
       CLEAR gs_err.
       REFRESH gt_lines_save[].
     ENDIF.
    ENDLOOP.
 
 ** -- Do it for last record.
   CALL FUNCTION 'SAVE_TEXT'
     EXPORTING
       client          = sy-mandt
       header          = gs_header
       savemode_direct = 'X'
     IMPORTING
       newheader       = gs_header
     TABLES
       lines           = gt_lines_save[]
     EXCEPTIONS
       id              = 1
       language        = 2
       name            = 3
       object          = 4
       OTHERS          = 5.
 
   IF sy-subrc <> 0.
     gs_err-matnr = gv_matnr.
     gs_err-error = text-upr.
     APPEND gs_err TO gt_err.
   ELSE.
     gs_err-matnr = gv_matnr.
     gs_err-error = text-ups.
     APPEND gs_err TO gt_err.
   ENDIF.
   CLEAR gs_err.
   REFRESH gt_lines_save[]..
 
   IF gt_err[] IS NOT INITIAL.
     TRY.
         cl_salv_table=>factory(
         IMPORTING
         r_salv_table = gx_alv
         CHANGING
         t_table      = gt_err ).
       CATCH cx_salv_msg INTO gr_message.
     ENDTRY.
 
     lx_columns = gx_alv->get_columns( ).
     lx_columns->set_optimize( 'X' ).
   ENDIF.
   gx_alv->display( ).

The output of the report

Material PO Text

Note: For our case, the material number is the first word in the file and it is separated by ‘~~’.

Let us validate the PO Text in the Material Master. Go to MM03 -> Purchase Order Text -> Give the Plant -> Hit the Text Editor.

Look the texts move to the next line whenever there is space crunch. The meaning of the sentence is intact. And the words are not split in between two lines.

LongText good

This is just a simple observation. Many of you might already be using SAVE_TEXT and getting your text auto formatted. But if you are a first-time ABAPer, I wanted to show you what should be your approach to tackle any SAP issue. You need to look for the answer in the Standard SAP Solutions. SAP has everything. We just need to learn to debug at the right place and figure out how to use them in our custom program.

Write and Earn at SAPYardIf interested to Earn some extra incomecheck here.

If you GENUINELY like our articles then it would be a HUGE help if you shared, subscribed and liked us on Facebook. It might seem insignificant, but it helps more than you might think.

Please write your comments & feedback below.

Step by Step Tutorials on S/4 HANA

Step by Step Code Snippet for Reference.


Viewing all articles
Browse latest Browse all 66

Trending Articles