Skip to Content
Technical Articles
Author's profile photo Mithun Kumar

New way of dynamic variable replacement in a standard text

It’s a simple requirement at times to use a standard text (SO10) in a code, and dynamically replace the symbols in the text before output, either in a PDF or email, or any other way.

So I tried the way posted in this blog.

The code given in the above blog works fine in most cases, however, I encountered a specific case, when this code failed, for no reason that I could understand. I’ve posted the same in here.

Thus for easy reference to everyone in future, here’s the new code that you can use irrespective of whether you’re coding in a report program or a class method (in my case).

DATA: lt_syms    TYPE TABLE OF itcst,
      ekko       TYPE ekko.

    CALL FUNCTION 'ME_EKKO_SINGLE_READ'
      EXPORTING
        pi_ebeln         = CONV ebeln( is_nast-objky )                " Purchasing Document Number
      IMPORTING
        po_ekko          = ekko                 " Purchasing Document Header
      EXCEPTIONS
        no_records_found = 1
        OTHERS           = 2.

    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      RETURN.
    ENDIF.

    CHECK sy-subrc EQ 0.
    SELECT SINGLE name1 FROM  adrc AS a
                        INNER JOIN t001 AS t
                                ON t~adrnr = a~addrnumber
                               AND t~bukrs  = @ekko-bukrs
           INTO @DATA(x_t001_name1).

    CALL FUNCTION 'INIT_TEXTSYMBOL'.

    CALL FUNCTION 'TEXT_SYMBOL_COLLECT'
      TABLES
        lines   = lt_tlines                 " Text lines
        symbols = lt_syms.                " Symbols found

    LOOP AT lt_syms INTO DATA(ls_symb).
      ASSIGN (ls_symb-name) TO FIELD-SYMBOL(<fs_textsym>).
      DATA(lv_textsym) = |&{ ls_symb-name }&|.

      IF <fs_textsym> IS ASSIGNED.
        CALL FUNCTION 'TEXT_SYMBOL_SETVALUE'
          EXPORTING
            name  = lv_textsym                 " Symbol name
            value = <fs_textsym>.                 " Symbol value
      ENDIF.
    ENDLOOP.

    CALL FUNCTION 'TEXT_SYMBOL_REPLACE'
      EXPORTING
        header = ls_thead
      TABLES
        lines  = lt_tlines.

    CALL FUNCTION 'FORMAT_TEXTLINES'
      EXPORTING
        formatwidth = 128
      TABLES
        lines       = lt_tlines
      EXCEPTIONS
        bound_error = 1
        OTHERS      = 2.

    IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*       WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

LT_TLINES and LS_THEAD are filled with READ_TEXT as usual.

In short, we’re first retrieving a list of all the unique symbols used in the entire text. Then looping on it and finding the correct replacement value for each of those symbols. Finally, the whole list of symbols get appropriately replaced with the corresponding values into the text. In some cases, the replacement might cause the lines to break into two, thus the last FM helps to consolidate it again.

Do let me know in comments if this helps you in some requirements, or if you have any other feedback on this approach.

PS: I also just learnt that TEXT_SYMBOL_* function modules are released, while *_TEXTSYMBOL are not. So the above is actually the right way to get this done.

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Reinold Müller
      Reinold Müller

      Thank you for the interesting blog post.

      I adapted your coding to solve my problem to replace several variables within Material PO texts which I have to display in an popup of my report.

       

      best regards

      Reinhold