Save internal table to directory as CSV file? - file-io

I'm trying to save a SAP internal table to SAP Directory as .CSV file. I've learned that I should convert the contents to character type but i get an illegal casting error.
This is my code:
FORM f_opendata TABLES p_tab TYPE STANDARD TABLE USING
p_work TYPE any.
FIELD-SYMBOLS: <lfs_wa>.
CONCATENATE c_headerfile c_initials sy-datum c_ext INTO v_dtasetfile.
OPEN DATASET v_dtasetfile FOR OUTPUT IN TEXT MODE ENCODING UTF-8.
IF sy-subrc EQ 0.
LOOP AT p_tab INTO p_work.
ASSIGN p_work TO <lfs_wa> CASTING TYPE c.
TRANSFER <lfs_wa> TO v_dtasetfile.
ENDLOOP.
CLOSE DATASET v_dtasetfile.
MESSAGE i899(f2) WITH 'SUCCESS' v_dtasetfile.
ENDIF.
ENDFORM.
This is the error:
An exception occurred that is explained in detail below.
The exception, which is assigned to class CX_SY_ASSIGN_CAST_ILLEGAL_CAST, was
not caught in procedure F_OPENDATA, nor was it propagated by a RAISING clause.
Since the caller of the procedure could not have anticipated that the
exception would occur, the current program is terminated.
The reason for the exception is:
The error occurred at a statement of the form
ASSIGN f TO CASTING.
ASSIGN f TO CASTING TYPE t.
or
ASSIGN f TO CASTING LIKE f1.
or
at table statements with the addition
ASSIGNING CASTING.
The following error causes are possible:
1. The type of field f or the target type determined by , t or f1
contains data references, object references, strings or internal tables
as components.
These components must match exactly as far as their position (offset)
and their type are concerned.
2. The specified type t or the type of f1 are not suited for the type of
the field symbol .
3. The row type of the related table is not suited for the target type
specified by according to the rules described in 1).

Converting isn't the same thing as casting. If your table contains fields that are not CLIKE, the system will refuse to cast the entire line. You have two options at this point:
ensure that your output table only contains character-like fields or
walk through each field of each line (you can use RTTI to do this) and convert the contents as required

I used a function to covert to text format before I transfered data to the dataset:
CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT'
EXPORTING
I_FIELD_SEPERATOR = c_colon
TABLES
I_TAB_SAP_DATA = ig_final
CHANGING
I_TAB_CONVERTED_DATA = ig_text
EXCEPTIONS
CONVERSION_FAILED = 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.
OPEN DATASET v_g_dtasetfile FOR OUTPUT IN TEXT MODE ENCODING UTF-8.
IF sy-subrc EQ 0.
LOOP AT ig_text ASSIGNING <fs_l_wa>.
TRANSFER <fs_l_wa> TO v_g_dtasetfile.
ENDLOOP.
CLOSE DATASET v_g_dtasetfile.
MESSAGE i899(f2) WITH text-020 v_g_dtasetfile text-021.
ENDIF.

Related

Error trying to screen an internal table abap

I´m learning ABAP and I keep trying to write that internal table and show it. There's syntax error message at line WRITE: / I_EJSEIS:
"I_EJSEIS" cannot be converted to a character-like value
I just don't understand it.
TYPES: S_EJSEIS LIKE SPFLI.
DATA: I_EJSEIS TYPE TABLE OF S_EJSEIS WITH HEADER LINE,
WA_EJSEIS TYPE S_EJSEIS.
SELECT FLTYPE
FROM SPFLI
INTO TABLE I_EJSEIS
WHERE CARRID = 'LH'.
LOOP AT I_EJSEIS.
WRITE: / I_EJSEIS.
ENDLOOP.
According to the ABAP documentation of WRITE dobj:
For dobj, those data types are allowed that are grouped under the generic type simple:
All flat data types; flat structures are handled like a data object of type c and can only contain any character-like components.
The data types string and xstring
enumerated types; the name (maximum three characters) of the enumerated constant is used in uppercase letters, which defines the current enumerated value.
In your case, I_EJSEIS is a (flat) structure containing at least one non-character component (e.g. fltime, distance, period), so it doesn't fall in any of the categories above.
The workaround is to display the fields individually:
WRITE: / I_EJSEIS-FLTYPE, I_EJSEIS-FLTIME.

Changing IDoc segment SDATA via field-symbols?

My scenario is that I am getting an IDOC segment's data into a field symbol and change some fields based on some validations.
My code:
READ TABLE idoc_data ASSIGNING FIELD-SYMBOL(<idocdata>) with key = 'E1EDK01'
IF sy-subrc = 0.
    lcl_struc ?= cl_abap_typedescr=>describe_by_name( 'E1EDK01' ).
    CREATE DATA dref TYPE HANDLE lcl_struc.
    ASSIGN dref->* TO FIELD-SYMBOL(<sdata>).
    IF <sdata> IS ASSIGNED.
      <sdata> = <idocdata>-sdata.
....
<idocdata>-sdata = <sdata>.
    ENDIF.
ENDIF.
Though the above snippet works fine, the continuity of field symbols is broken and now I have to pass back the changed data. How do I use ASSIGN and let the field symbols take care of the changes rather than an explicit statement?
Something similar to below snippet though this won't work since <IDOC_DATA>-SDATA and <SDATA> aren't compatible.
READ TABLE idoc_data ASSIGNING FIELD-SYMBOL(<idocdata>) with key = 'E1EDK01'
IF sy-subrc = 0.
FIELD-SYMBOLS: <sdata> TYPE E1EDK01.
ASSIGN <idocdata>-sdata TO <sdata>.
....
ENDIF.
My expectation is that when I change the data in <SDATA>-FIELD1, I want the changes to flow into <IDOCDATA>-SDATA without using <idocdata>-sdata = <sdata>.
As #Sandra mentioned above, the incompatibility of field-symbols can be resolved by using CASTING while assigning them. This would make the second snippet work.
...
IF sy-subrc = 0.
FIELD-SYMBOLS: <sdata> TYPE E1EDK01.
ASSIGN <idocdata>-sdata TO <sdata> CASTING.
...
ENDIF.

How to get a datatype DF16_RAW with precision 16 by using CL_ABAP_ELEMDESCR for internal table in ABAP?

I have internal type A for DF16_RAW and E for DF34_RAW and now at runtime I am creating a dynamic table for that I want datatype DF16_RAW and DF34_RAW with specified precision based on internal type. My code is like below:
CASE WA_COL-INTTYPE.
WHEN 'A'. LO_DESCR_RESULT = CL_ABAP_ELEMDESCR=>GET_DECFLOAT16( ).
WHEN 'E'. LO_DESCR_RESULT = CL_ABAP_ELEMDESCR=>GET_DECFLOAT34( ).
Here I want to get datatype with specified precision. I don't know how it be done?
Some parts of a variable are specific to the ABAP dictionary a.k.a. "DDIC" (search help, output style for the DF* types, etc.) If you want to create one variable with information specific to the ABAP dictionary, then you must refer to an element in the DDIC (i.e. a data element or table/structure component), then use:
lo_descr_result = cl_abap_typedescr=>describe_by_name( 'DDICdataelement' ).
or
lo_descr_result = cl_abap_typedescr=>describe_by_name( 'DDICtablestruct-Component' ).

Casting from a Packed(8) type to a TMSTMP (DEC15) type in a Unicode system (and back)

Background:
I have several tables that are connected for maintenance in a view cluster (SE54). Each of these tables have the standard Created/Changed By/On fields. For created data updating the fields are easy, and I use event 05 (On Create) in the Table Maintenance generator. For defaulting the changing fields it's a little bit more involved. I have to use event 01 (Before Save), and then update the tables TOTAL[] and EXTRACT[] with the field values as needed.
When maintaining the table in SM30, the format of TOTAL[] and EXTRACT[] is the same as the view I'm maintaining with an additional flag to identify what type of change is made (update/create/delete)
However, when maintaining in SM54 (which is the business requirement), the format of TOTAL[] and EXTRACT[] is just an internal table of character lines.
Problem:
I can figure out what the type of the table that is being edited is. But when I try to move the character line to the type line I get the following run-time errors: (Depending on how I try to move/assign it)
ASSIGN_BASE_TOO_SHORT
UC_OBJECTS_NOT_CONVERTIBLE
UC_OBJECTS_NOT_CHAR
All my structures are in the following format:
*several generic (flat) types
CREATED TYPE TMSTMP, "not a flat type
CHANGED TYPE TMSTMP, "not a flat type
CREATED_BY TYPE ERNAM,
CHANGED_BY TYPE AENAM,
The root of the problem is that the two timestamp fields are not flat types. I can see in the character line, that the timestamps are represented by 8 Characters.
Edit: Only after finding the solution could I identify the Length(8) field as packed.
I have tried the following in vain:
"try the entire structure - which would be ideal
assign ls_table_line to <fs_of_the_correct_type> casting.
"try isolating just the timestamp field(s)
assign <just_the_8char_representation> to <fs_of_type_tmpstmp> casting.
I've tried a few other variations on the "single field only" option with no luck.
Any ideas how I can cast from the Character type to type TMSTMP and then back again in order to update the internal table values?
I've found that the following works:
In stead of using:
field-symbols: <structure> type ty_mystructure,
<changed> type tmstmp.
assign gv_sapsingle_line to <structure> casting. "causes a runtime error
assign gv_sap_p8_field to <changed> casting. "ditto
I used this:
field-symbols: <structure> type any,
<changed> type any.
assign gv_sapsingle_line to <structure> casting type ty_mystructure.
assign gv_sap_p8_field to <changed> casting type ty_tmstmp.
For some reason it didn't like that I predefined the field symbols.
I find that odd as the documentation states the following:
Casting with an Implicit Type Declaration Provided the field symbol is
either fully typed or has one of the generic built-in ABAP types – C,
N, P, or X – you can use the following statement:
ASSIGN ... TO <FS> CASTING.
When the system accesses the field symbol, the content of the
assigned data object is interpreted as if it had the same type as the
field symbol.
I can only assume that my structure wasn't compatible (due to the P8 -> TMSTMP conversion)
The length and alignment of the data object must be
compatible with the field symbol type. Otherwise the system returns a
runtime error. If the type of either the field symbol or the data
object is – or contains – a string, reference type, or internal table,
the type and position of these components must match exactly.
Otherwise, a runtime error occurs.

Syntax error "field ITAB unknown" in APPEND wa TO itab

I am trying to add a new record to my internal table and this code is giving me an error, but I am doing exactly the same thing as in my SAP book. What am I doing wrong?
TYPES : BEGIN OF personel_bilgileri,
Ad TYPE c LENGTH 20,
Soyad TYPE c LENGTH 20,
Telefon_no Type n LENGTH 12,
END OF personel_bilgileri.
TYPES personel_bilgi_tablo_tipi TYPE STANDARD TABLE OF
personel_bilgileri WITH NON-UNIQUE KEY ad soyad.
DATA : personel_bilgi_kaydi TYPE personel_bilgileri,
personel_bilgi_tablosu TYPE personel_bilgi_tablo_tipi.
personel_bilgi_kaydi-ad = 'Murat'.
personel_bilgi_kaydi-soyad = 'Sahin'.
personel_bilgi_kaydi-telefon_no = '5556677'.
APPEND personel_bilgi_kaydi TO personel_bilgileri.
personel_bilgi_kaydi-ad = 'Ayse'.
personel_bilgi_kaydi-soyad = 'Bil'.
personel_bilgi_kaydi-telefon_no = '5556611'.
APPEND personel_bilgi_kaydi TO personel_bilgileri.
personel_bilgi_kaydi-ad = 'Mehmet'.
personel_bilgi_kaydi-soyad = 'Kalan'.
personel_bilgi_kaydi-telefon_no = '5556622'.
APPEND personel_bilgi_kaydi TO personel_bilgileri.
Actually, I don't know which adding record method I should use. I mean there is too many ways to do this operation. Which method will be the true one?
I am getting this error:
The field Personel_bilgileri is unknown, but there are following fields similar names...
Moreover, I can do this with LOOP AT, but I didn't understand the usage of LOOP AT. What does it do?
In your code sample, you first defined PERSONEL_BILGILERI as a TYPE, then PERSONEL_BILGI_TABLO_TIPI as a internal table TYPE of PERSONEL_BILGILERI.
Up to that point, no variables are declared yet. Only data types.
Then:
PERSONEL_BILGI_KAYDI is defined of type PERSONEL_BILGILERI. This is a structure that you use as a work area (which is fine).
PERSONEL_BILGI_TABLOSU is defined of type PERSONEL_BILGI_TABLO_TIPI. So PERSONEL_BILGI_TABLOSU is your internal table.
When you APPEND your work area, you have to append to an internal table, not a data type. try with PERSONEL_BILGI_TABLOSU instead of your type PERSONEL_BILGI:
APPEND personel_bilgi_kaydi TO personel_bilgileri_tablosu.
You need to APPEND your WA(workarea, personel_bilgi_kaydi) in to your table (personel_bilgi_tablosu). You cant append the WA to the defined type.
So it should look like this:
APPEND personel_bilgi_kaydi TO personel_bilgi_tablosu.
Also you can use this code to show them on the page.
LOOP AT personel_bilgi_tablosu into personel_bilgi_kaydi.
write: / 'İSİM: ' ,personel_bilgi_kaydi-ad,
'SOYİSİM: ',personel_bilgi_kaydi-soyad,
'TEL NO: ', personel_bilgi_kaydi-telefon_no.
ENDLOOP.
You can use other methods to show your table on the page, such as REUSE_ALV_GRID_DISPLAY. You can get more information about that in scn.sap.com
Hope it was helpful.
Kolay gelsin.
Talha