SAP Text Edit Control To DB
SAP Text Edit Control To DB
* Z_TEXT_EDIT_CONTROL_TO_DB *
*=====================================================================*
* This small program is to demonstrate a rudimentary way to read and *
* save entries from the CL_GUI_TEXTEDIT control to a text-object. *
* The requirement was a simple possibility of text entry with no text *
* formatting (except line breaks) which could be displayed in an *
* embedded dynpro. *
* *
* I found many examples of the EDIT_TEXT functions and how to use the *
* CL_GUI_TEXTEDIT class but non in combination. *
*---------------------------------------------------------------------*
* AUTHOR: Harry Fumey *
* DATE: 29.07.2009 *
* *
*=====================================================================*
* COMMENTS: *
* Create a dynpro 0100 and three push buttons named "PB_EXIT" *
* "PB_READ" and "PB_SAVE" as well as a custom container named *
* "CONTAINER". *
* The push buttons have function code "EXIT", "SAVE" and "READ". *
* Create your own text objects and id’s using SE75 to play around *
*PROGRAM name: *
REPORT z_text_edit_control_to_db.
**********************************************************************
* Transport table for Control to internal table
**********************************************************************
TYPES:
BEGIN OF ty_ttable,
line(length) TYPE c,
END OF ty_ttable.
DATA:
wa_ttable TYPE ty_ttable,
i_ttable TYPE TABLE OF ty_ttable.
SELECTION-SCREEN BEGIN OF BLOCK t1 WITH FRAME.
PARAMETERS:
p_object LIKE stxh-tdobject DEFAULT 'ZADM',
p_name LIKE stxh-tdname,
p_id LIKE stxh-tdid DEFAULT 'ZAD1',
p_spras LIKE stxh-tdspras DEFAULT sy-langu.
START-OF-SELECTION.
PERFORM read_text_from_db.
*&---------------------------------------------------------------------*
*& Form READ_TEXT_FROM_DB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM read_text_from_db.
IF sy-subrc = 0.
* Loop over data table and fill internal table for the text edit control
* The field for formatting is omitted.
LOOP AT xnote INTO wa_xnote.
wa_ttable-line = wa_xnote-tdline.
APPEND wa_ttable TO i_ttable.
CLEAR wa_ttable.
ENDLOOP.
g_init = 'X'.
ENDIF.
SET SCREEN '100'.
ENDFORM. " read_text_from_db
*---------------------------------------------------------------------*
* MODULE USER_COMMAND_0100 INPUT *
*---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN 'READ'.
PERFORM read_text_into_control.
WHEN 'SAVE'.
PERFORM save_text_to_db.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
WHEN OTHERS.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
* Check if control is present
IF editor IS INITIAL.
*Read data from control (front end) to internal table and change flag "is_m
odified"
g_subrc = sy-subrc.
*Empty the data table of old entries
IF g_subrc = 0.
CLEAR xnote.
REFRESH xnote.
ENDIF.
*In case the entry does not exist initialize a new one
*Note that table XNOTE will be emptied
IF g_subrc <> 0.
CALL FUNCTION 'INIT_TEXT'
EXPORTING
id = p_id
language = p_spras
name = p_name
object = p_object
IMPORTING
header = x_head
TABLES
lines = xnote
EXCEPTIONS
id = 1
language = 2
name = 3
object = 4
OTHERS = 5.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF.
*Fill the data table with new values from internal table coming from the te
xt edit control
LOOP AT i_ttable INTO wa_ttable.
wa_xnote-tdline = wa_ttable-line.
APPEND wa_xnote TO xnote.
CLEAR wa_xnote.
ENDLOOP.
CLEAR i_ttable.
FREE i_ttable.
*If the entry already existed on the data base the new text will be saved t
o the data base.
*Otherwise the data will be saved using the newly created x_head entry into
the data base.