Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Upload long text to ABAP text object

alan_alis
Explorer
0 Kudos

I wonder if someone knows how to upload a long text from a flat file to a text object in SAP. I'm trying to upload data from legacy system to SAP. Thanks!

1 ACCEPTED SOLUTION

glio_ad
Active Participant
0 Kudos

Hi Alan.

First of all, some assumption MUST be made as how the input file will be formatted.

So, if you needed to update the long text id 'BEST' and object 'MATERIAL' of all materials given in a file, that file must have in its structure the material code, a line id and a fixed length field with the text. So if the long text for material code 10 comprises of 260 characters, you could requeste that the input txt file has the following structure (tab delimited):

matnr(18), line(3), line(100)

-


10(tab)1(tab) bla bla ..... (100 chars)

10(tab)2(tab) bla bla ...... (100 chars)

10(tab)3(tab) bla bla.... (remaining 60 characters)

the respective program then would be as follows:

REPORT zupload_texts.

                                    • EXTERNAL TABLES DEFINITIONS *************************

TABLES: mara.

                                        • VARIABLES DEFINITIONS *****************************

DATA: wl_file TYPE string.

                                    • INTERNAL TABLES DEFINITIONS *************************

DATA: BEGIN OF i_data OCCURS 0,

matnr LIKE mara-matnr,

lino(3) TYPE n,

txt(100) TYPE c,

END OF i_data.

DATA: wa_data LIKE i_data.

DATA: i_lines LIKE tline OCCURS 15 WITH HEADER LINE,

i_thead LIKE thead.

                            • SELECTION SCREEN / OPTIONS - PARAMETERS *****************

SELECTION-SCREEN SKIP 2.

SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME.

PARAMETERS: p_file LIKE ibipparms-path OBLIGATORY DEFAULT 'C:\'.

SELECTION-SCREEN SKIP 1.

PARAMETERS: p_lang LIKE makt-spras OBLIGATORY DEFAULT sy-langu.

SELECTION-SCREEN END OF BLOCK bl1.

                                        • INITIALIZATION EVENT ***************************

INITIALIZATION.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'

IMPORTING

file_name = p_file.

                                  • START OF SELECTION EVENTS ****************************

START-OF-SELECTION.

MOVE p_file TO wl_file.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = wl_file

filetype = 'ASC'

has_field_separator = ';'

TABLES

data_tab = i_data

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

OTHERS = 17.

IF sy-subrc <> 0.

WRITE:/ text-001.

EXIT.

ENDIF.

END-OF-SELECTION.

LOOP AT i_data.

wa_data = i_data.

i_lines-tdformat = 'TX'.

i_lines-tdline = i_data-txt. " current line's characters

APPEND i_lines.

"at the and of the material, save its long text

AT END OF matnr.

i_data = wa_data.

DATA: w_size TYPE i.

i_thead-tdname = '000000000000000000'.

w_size = STRLEN( i_data-matnr ).

w_size = 18 - w_size.

i_thead-tdname+w_size = i_data-matnr.

i_thead-tdobject = 'MATERIAL'.

CALL FUNCTION 'CREATE_TEXT'

EXPORTING

fid = 'BEST'

flanguage = p_lang

fname = i_thead-tdname

fobject = i_thead-tdobject

save_direct = 'X'

fformat = '*'

TABLES

flines = i_lines

EXCEPTIONS

no_init = 1

no_save = 2

OTHERS = 3.

IF sy-subrc <> 0.

WRITE:/ text-002, i_data-matnr.

ENDIF.

CLEAR: i_lines, i_lines[].

ENDAT.

ENDLOOP.

WRITE:/ 'Program ended'.

Reward if it helps.

Regards,

George

3 REPLIES 3

glio_ad
Active Participant
0 Kudos

Hi Alan.

First of all, some assumption MUST be made as how the input file will be formatted.

So, if you needed to update the long text id 'BEST' and object 'MATERIAL' of all materials given in a file, that file must have in its structure the material code, a line id and a fixed length field with the text. So if the long text for material code 10 comprises of 260 characters, you could requeste that the input txt file has the following structure (tab delimited):

matnr(18), line(3), line(100)

-


10(tab)1(tab) bla bla ..... (100 chars)

10(tab)2(tab) bla bla ...... (100 chars)

10(tab)3(tab) bla bla.... (remaining 60 characters)

the respective program then would be as follows:

REPORT zupload_texts.

                                    • EXTERNAL TABLES DEFINITIONS *************************

TABLES: mara.

                                        • VARIABLES DEFINITIONS *****************************

DATA: wl_file TYPE string.

                                    • INTERNAL TABLES DEFINITIONS *************************

DATA: BEGIN OF i_data OCCURS 0,

matnr LIKE mara-matnr,

lino(3) TYPE n,

txt(100) TYPE c,

END OF i_data.

DATA: wa_data LIKE i_data.

DATA: i_lines LIKE tline OCCURS 15 WITH HEADER LINE,

i_thead LIKE thead.

                            • SELECTION SCREEN / OPTIONS - PARAMETERS *****************

SELECTION-SCREEN SKIP 2.

SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME.

PARAMETERS: p_file LIKE ibipparms-path OBLIGATORY DEFAULT 'C:\'.

SELECTION-SCREEN SKIP 1.

PARAMETERS: p_lang LIKE makt-spras OBLIGATORY DEFAULT sy-langu.

SELECTION-SCREEN END OF BLOCK bl1.

                                        • INITIALIZATION EVENT ***************************

INITIALIZATION.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'

IMPORTING

file_name = p_file.

                                  • START OF SELECTION EVENTS ****************************

START-OF-SELECTION.

MOVE p_file TO wl_file.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = wl_file

filetype = 'ASC'

has_field_separator = ';'

TABLES

data_tab = i_data

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

OTHERS = 17.

IF sy-subrc <> 0.

WRITE:/ text-001.

EXIT.

ENDIF.

END-OF-SELECTION.

LOOP AT i_data.

wa_data = i_data.

i_lines-tdformat = 'TX'.

i_lines-tdline = i_data-txt. " current line's characters

APPEND i_lines.

"at the and of the material, save its long text

AT END OF matnr.

i_data = wa_data.

DATA: w_size TYPE i.

i_thead-tdname = '000000000000000000'.

w_size = STRLEN( i_data-matnr ).

w_size = 18 - w_size.

i_thead-tdname+w_size = i_data-matnr.

i_thead-tdobject = 'MATERIAL'.

CALL FUNCTION 'CREATE_TEXT'

EXPORTING

fid = 'BEST'

flanguage = p_lang

fname = i_thead-tdname

fobject = i_thead-tdobject

save_direct = 'X'

fformat = '*'

TABLES

flines = i_lines

EXCEPTIONS

no_init = 1

no_save = 2

OTHERS = 3.

IF sy-subrc <> 0.

WRITE:/ text-002, i_data-matnr.

ENDIF.

CLEAR: i_lines, i_lines[].

ENDAT.

ENDLOOP.

WRITE:/ 'Program ended'.

Reward if it helps.

Regards,

George

0 Kudos

Thanks George for the detailed help. I've also been researching & found some function modules for text processing under function grp STXD. One of them is SAVE_TEXT. This fm is also contained in fm CREATE_TEXT which your program is using so I think this solves my problem. Have a great day!

glio_ad
Active Participant
0 Kudos

Hi again Alan and thanks for the full points!

Keep in mind also that if you are about to upload long texts for example in a Purchase Order, it might not be enough just to use the SAVE_TEXT function module and set the correct TDNAME (for example, the tdname for a PO header long text would be just the PO's number -ekko-ebeln).

I have created a migration program that uploads MM services (transaction AC02) and I needed to upload also some long texts. In that case, I also had to explicitely update a specific flag in table ASMDT in order for the user to be able to see that long text online. I mean that, the text was actually saved in the DB (you could read it with the FM READ_TEXT_INLINE) but the user could not see it in AC03 as that flag in ASMDT was not set !

In that case I used the following form to save the text and update ASMDT correctly:

"&----


"*& Form save_text

"&----


"* Save text

"----


"* -->P_TDOBJECT Text Object

"* -->P_TDID Text-ID

"* -->P_TDNAME TDName

"* -->P_NOTES Comments to add

"* <--P_OK_FLAG Set to C_TRUE if Sales Order created

"----


FORM save_text USING value(p_tdobject)

value(p_tdid)

value(p_tdname)

value(p_notes)

CHANGING p_ok_flag.

DATA: BEGIN OF t_header.

INCLUDE STRUCTURE thead.

DATA: END OF t_header.

DATA: BEGIN OF tlines OCCURS 0.

INCLUDE STRUCTURE tline.

DATA: END OF tlines.

DATA: wl_len TYPE i,

wl_off TYPE i,

wl_cnt TYPE i,

wl_msg(100).

t_header-tdobject = p_tdobject.

t_header-tdname = p_tdname.

t_header-tdid = p_tdid.

t_header-tdspras = sy-langu.

t_header-tdform = 'SYSTEM'.

t_header-tdlinesize = '070'.

  • Split p_notes at 72 columns

wl_len = STRLEN( p_notes ).

IF wl_len <= 70.

tlines-tdline = p_notes.

APPEND tlines.

ELSE.

tlines-tdline = p_notes+0(70).

APPEND tlines.

tlines-tdline = p_notes+70.

APPEND tlines.

ENDIF.

DESCRIBE TABLE tlines LINES t_header-tdtxtlines.

CALL FUNCTION 'SAVE_TEXT'

EXPORTING

header = t_header

insert = 'X'

savemode_direct = 'X'

TABLES

lines = tlines

EXCEPTIONS

id = 1

language = 2

name = 3

object = 4

OTHERS = 5.

IF sy-subrc <> 0.

p_ok_flag = c_false.

WRITE text-015 TO wl_msg.

REPLACE '%' WITH p_tdname INTO wl_msg.

EXIT.

ENDIF.

    • Prepare long texts for update (but keep them in the memory)*

CALL FUNCTION 'COMMIT_TEXT'

EXPORTING

keep = 'X'

object = 'ASMD'.

UPDATE asmdt SET kzltx = 'X'

WHERE asnum = p_tdname.

ENDFORM. "save_text

Regards,

George