Adding Sales Order Header Text to the Invoice Document in SAP ABAP.


Adding Sales Order Header Text to the Invoice Document in SAP ABAP.

Adding the sales order header text to an invoice document can be a crucial requirement for many businesses, allowing them to carry over specific information from the sales order to the invoice for further processing or display. In this guide, we'll cover how to achieve this in SAP ABAP using a combination of custom code and the READ_TEXT function module.

 Step 1: Define Constants for Text Retrieval

At the beginning of the sales_header_text form, two constants are defined:

abap

CONSTANTS: lc_vbbk TYPE tdobject VALUE 'VBBK',

           lc_id   TYPE tdid VALUE '0002'.

 lc_vbbk: Represents the Text Object (VBBK), which corresponds to Sales Documents in SAP.

 lc_id: Represents the Text ID (0002), identifying the specific type of text (in this case, header text).

These constants are used in the READ_TEXT function to specify the type of text we want to retrieve.

 Step 2: Initialize Variables for Text Handling

The code initializes several variables to handle the text:

abap

DATA: lt_header_text TYPE TABLE OF tline,

      lv_tdname      TYPE stxhtdname,

      lv_final_text  TYPE string.

 lt_header_text: A table of type tline, which will hold the fetched lines of text from the READ_TEXT function.

 lv_tdname: The unique identifier for the specific text (sales order number in this case).

 lv_final_text: A variable to concatenate and hold the final, condensed version of the header text.

 Step 3: Build the Text Name from the Sales Order Number

Here, the sales order number is converted to the proper format for text retrieval:

 

abap

DATA(lv_vbeln_vauf) = |{ vbdkrvbeln_vauf ALPHA = OUT } |.

lv_tdname = lv_vbeln_vauf.

 lv_vbeln_vauf: The sales order number (vbdkrvbeln_vauf) is converted to an alphanumeric format suitable for use with the READ_TEXT function.

 lv_tdname: Assigned the formatted sales order number to be used as the text name.

 Step 4: Fetch the Sales Order Header Text

The core part of the code is where the READ_TEXT function module is called to fetch the sales order header text:

abap

PERFORM read_text TABLES lt_header_text

      USING lc_vbbk lv_tdname lc_id vbdkrspras_we.

 read_text: The subroutine to retrieve the text from the database using the READ_TEXT function module.

 Parameters:

   lt_header_text: Output table for storing the fetched text lines.

   lc_vbbk: Text object type (Sales Document).

   lv_tdname: Text name (Sales Order Number).

   lc_id: Text ID.

   vbdkrspras_we: Language key, specifying the language for the text retrieval.

 Step 5: Concatenate the Fetched Text Lines

If the lt_header_text table contains data, it concatenates the lines into a single string:

abap

IF lt_header_text IS NOT INITIAL.

  LOOP AT lt_header_text ASSIGNING FIELDSYMBOL(<ls_header_text>).

    lv_final_text = |{ lv_final_text } { <ls_header_text>tdline }|.

  ENDLOOP.

  lv_final_text = condense( lv_final_text ).

ENDIF.

 Each line from lt_header_text is appended to the lv_final_text variable.

 CONDENSE is used to remove unnecessary spaces from the final concatenated text.

 Step 6: Split the Text into Segments for Display

The final text (lv_final_text) is split into chunks, each of a specific length, to fit into different segments (gv_final_text1 to gv_final_text8):

abap

DATA(lv_len) = strlen( lv_final_text ).

 gv_final_text1 = lv_final_text(57).
 gv_final_text2 = lv_final_text+58(23).
 gv_final_text3 = lv_final_text+81(11).
 gv_final_text4 = lv_final_text+93(15).
 gv_final_text5 = lv_final_text+109(20).
 gv_final_text6 = lv_final_text+130(44).
  gv_final_text7 = lv_final_text+174(188).
 lv_len = lv_len - 362.
 gv_final_text8 = lv_final_text+362(lv_len).

 gv_final_text1 to gv_final_text8: Segments to store split portions of the final text.

 The code conditionally checks the length of lv_final_text and assigns parts of the text to different segments, ensuring that longer texts are displayed correctly.

 Explanation of the READ_TEXT Function Module

The READ_TEXT function module is a standard SAP function used to read text lines stored in SAP's text tables. Here's a breakdown of how it works:

abap

CALL FUNCTION 'READ_TEXT'

  EXPORTING

    id        = in_tdid

    language  = in_tdspras

    name      = in_tdname

    object    = in_tdobject

  TABLES

    lines     = out_text_tab

 id: The Text ID (in_tdid) specifies the type of text to read, such as 0002 for header text.

 language: The language key (in_tdspras) to ensure the correct language version of the text is fetched.

 name: The Text Name (in_tdname) is the unique identifier, typically the sales order number in this case.

 object: The Text Object (in_tdobject) that identifies the context of the text, such as sales documents (VBBK).

 lines: The output table (out_text_tab) that will be filled with the lines of text fetched from the database.

 How It Works

The READ_TEXT function reads the specified text stored in the SAP database based on the combination of the parameters mentioned above. If the function succeeds, it populates the out_text_tab table with the corresponding lines of text. These lines can then be processed or displayed according to the requirement.

By following the above code, you can successfully fetch the header text from a sales order and include it in the invoice document. This is particularly useful for carrying over important information that needs to be reflected in the invoicing process. The use of the READ_TEXT function module allows efficient text retrieval, ensuring that the information is fetched accurately from the SAP text tables.

Below is my full code implementation based on my requirement, Alter the code as per your requirement.

 

FORM sales_header_text .
  CONSTANTS: lc_vbbk TYPE tdobject VALUE 'VBBK',
             lc_id   TYPE tdid VALUE '0002'.

  DATA: lt_header_text TYPE TABLE OF tline,
        lv_tdname      TYPE stxh-tdname,
        lv_final_text  TYPE string.

  CLEAR: lt_header_text[],
         lv_tdname,
         lv_final_text.

  DATA(lv_vbeln_vauf) = |{ vbdkr-vbeln_vauf ALPHA = OUT } |.
  lv_tdname = lv_vbeln_vauf.

  PERFORM read_text TABLES lt_header_text
        USING lc_vbbk lv_tdname lc_id vbdkr-spras_we.

  IF lt_header_text IS NOT INITIAL.
    LOOP AT lt_header_text ASSIGNING FIELD-SYMBOL(<ls_header_text>).
      lv_final_text = |{ lv_final_text } { <ls_header_text>-tdline }|.
    ENDLOOP.
    lv_final_text = condense( lv_final_text ).
  ENDIF.
  CLEAR: gv_final_text1,
         gv_final_text2,
         gv_final_text3,
         gv_final_text4,
         gv_final_text5,
         gv_final_text6,
         gv_final_text7,
         gv_final_text8.
  DATA(lv_len) = strlen( lv_final_text ).
  IF lv_len >= 57.
    gv_final_text1 = lv_final_text(57).
  ELSE.
    gv_final_text1 = lv_final_text.
    RETURN.
  ENDIF.
  IF lv_len >= 81.
    gv_final_text2 = lv_final_text+58(23).
  ELSE.
    gv_final_text2 = lv_final_text+58.
    RETURN.
  ENDIF.
  IF lv_len >= 93.
    gv_final_text3 = lv_final_text+81(11).
  ELSE.
    gv_final_text3 = lv_final_text+81.
    RETURN.
  ENDIF.

  IF lv_len >= 109.
    gv_final_text4 = lv_final_text+93(15).
  ELSE.
    gv_final_text4 = lv_final_text+93.
    RETURN.
  ENDIF.
  IF lv_len >= 130.
    gv_final_text5 = lv_final_text+109(20).
  ELSE.
    gv_final_text5 = lv_final_text+109.
    RETURN.
  ENDIF.
  IF lv_len >= 174.
    gv_final_text6 = lv_final_text+130(44).
  ELSE.
    gv_final_text6 = lv_final_text+130.
    RETURN.
  ENDIF.
  IF lv_len >= 362.
    gv_final_text7 = lv_final_text+174(188).
    lv_len = lv_len - 362.
    gv_final_text8 = lv_final_text+362(lv_len).
  ELSE.
    gv_final_text7 = lv_final_text+174.
    gv_final_text8 = ''.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  READ_TEXT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LT_HEADER_TEXT  text
*      -->P_LC_VBBK  text
*      -->P_LV_TDNAME  text
*      -->P_LC_ID  text
*      -->P_VBDKR_SPRAS_WE  text
*----------------------------------------------------------------------*
FORM read_text  TABLES   out_text_tab STRUCTURE tline
                USING    in_tdobject  LIKE stxh-tdobject
                         in_tdname    LIKE stxh-tdname
                         in_tdid      LIKE stxh-tdid
                         in_tdspras   LIKE stxh-tdspras.
  CONSTANTS: lc_readtxt(9) TYPE c VALUE 'READ_TEXT'.
  REFRESH out_text_tab.
  CALL FUNCTION lc_readtxt
    EXPORTING
      id                      = in_tdid
      language                = in_tdspras
      name                    = in_tdname
      object                  = in_tdobject
    TABLES
      lines                   = out_text_tab
    EXCEPTIONS
      id                      = 1
      language                = 2
      name                    = 3
      not_found               = 4
      object                  = 5
      reference_check         = 6
      wrong_access_to_archive = 7
      OTHERS                  = 8.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.


Post a Comment

Previous Post Next Post