Inserting data into BAPIRET2_TAB structure - abap

My method is using an export parameter of type BAPIRET2_TAB. I need to fill the values of this structure, but I cant access the structure directly. For example, parameter-message = 'text', etc.
How can I do this?
These are the parameters I need to pass:
lv_msg_line. type i
lv_syntax_text. //Error message
And this is syntax checker.
syntax-check for l_tab_code
program lv_progname
message l_error_message
line l_error_line
word l_error_word
id 'ERR' table l_tab_errors.

Like said above in the comments, BAPIRET2_TAB is not a structure, and therefore cannot have its components accessed directly via STRUCTURE-FIELD paradigm.
What you need is to declare an structure like this
DATA error_line TYPE LINE OF BAPIRET2_TAB.
Then, you can use it to fill the data in...
error_line-program = sy-repid.
error_line-id = sy-msgid.
... and so forth. Then, lastly, you append the error_line item to your BAPIRET2_TAB.
APPEND error_line TO bapi2tab.
CLEAR error_line.
Hope it helps.

Related

Convert a spool into text format

I want to send the spool generated by a Smart Form, by email as attachment in TXT format.
The issue is to get the spool in a TXT format, without technical stuff, just the characters in the form.
I have used the function module RSPO_RETURN_SPOOLJOB for getting it, but it returns a technical format like this:
//XHPLJIIID 0700 00000+00000+
IN01ES_CA930_DEMO_3 FIRST
OPINCH12 P 144 240 1728020160000010000100001
IN02MAIN
MT0100808400
CP11000000E
FCCOURIER 120 00144 SF001SF001110000144E
UL +0000000000000
ST0201614Dear Customer,
MT0214209000
ST0864060We would like to take this opportunity to confirm the flight
MT0100809360
ST0763253reservations listed below. Thank you for your custom.
...
I want something as follows, without the technical stuff:
Dear Customer,
We would like to take this opportunity to confirm the flight
reservations listed below. Thank you for your custom.
...
This is the code I have used :
PARAMETERS spoolnum type TSP01-RQIDENT.
DATA spool_contents type soli_tab.
CALL FUNCTION 'RSPO_RETURN_SPOOLJOB'
exporting
rqident = spoolnum
tables
buffer = spool_contents
exceptions
others = 1.
If the parameter DESIRED_TYPE is not passed or has the value 'OTF', and the spool is of type SAPscript/Smart Form, the function module returns the technical format you have experienced.
Instead, you should use the parameter DESIRED_TYPE = 'RAW' so that all the technical stuff is interpreted and the form is returned as text, the way you request, as follows :
CALL FUNCTION 'RSPO_RETURN_SPOOLJOB'
exporting
rqident = spoolnum
desired_type = 'RAW'
tables
buffer = spool_contents
exceptions
others = 1.

How to retrieve a specific field from a list output?

I don't have any developer rights in my SAP-System but I found a way to write some ABAP-Code in a tiny "User-Exit" box (I don't know if that's what you call it) inside a report.
I'm trying to submit a HR-Report and plug it's outcoming PERNR into that same report again.
There's a syntax-error that is telling me that t_list doesn't have a component with the Name PERNR.
What do I have to do in order to get this to work?
DATA: t_list TYPE TABLE OF abaplist WITH HEADER LINE,
seltab TYPE TABLE OF rsparams,
selline LIKE LINE OF seltab.
*I found out that the name of the selection field in the Report-GUI is "PNPPERNR" and tested it
selline-selname = 'PNPPERNR'.
selline-sign = 'I'.
selline-option = 'EQ'.
SUBMIT Y5000112
USING SELECTION-SET 'V1_TEST'
EXPORTING LIST TO MEMORY
AND RETURN.
CALL FUNCTION 'LIST_FROM_MEMORY'
TABLES
listobject = t_list
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE 'Unable to get list from memory'.
ELSE.
LOOP AT t_list.
*The Problem is here: how do I get the pnppernr out of t_list, it's the first column of the report output
selline-low = t_list-pernr.
append selline to seltab.
ENDLOOP.
SUBMIT Y5000112
WITH SELECTION-TABLE seltab
USING SELECTION-SET 'V2_TEST'
AND RETURN.
ENDIF.
Use the function module LIST_TO_ASCI to decode the contents of t_list into something readable. This answer contains some sample code including the data types required. At this point, the data you're looking for will probably occur at the same column range in the output. Use the standard substring access methods - e. g. line+42(21) to obtain the part of the line you need.
The vwegert's answer is more than useful! In my previous answer I forgot to mention LIST_TO_ASCI FM :)
The only thing I can add is that parsing of result lines has no universal solution and greatly depends on its structure. Usually it is done like:
LOOP AT t_list.
SPLIT t_list AT '|' INTO <required_structure>.
selline-low = <required_structure>-pernr.
APPEND selline TO seltab.
ENDLOOP.
where <`required_structure> is your Y5000112 output structure. But this may be not so simple and may require additional manipulations.

Function module to export local table to excel

I am working on a program in Business Warehouse that allows you to map out all of your process chains by following the hierarchy of parent to sub-chains by using the rspcchain table. As of right now I have it printing the output to the screen, but would like to export this output to excel instead. I have been unable to find a function module that serves this purpose, so any help would be greatly appreciated
note - after learning about the SALV classes available I changed the code to display the table differently.
REPORT Z_PC_VARIANT_MAPPING.
*Declaring types and variables
TYPES: BEGIN OF t_chains,
chain_id LIKE rspcchain-chain_id,
variant LIKE rspcchain-variante,
END OF t_chains.
DATA: lt_rspcchain TYPE STANDARD TABLE OF t_chains,
lwa_rspcchain TYPE t_chains,
o_alv TYPE REF TO cl_salv_table,
lx_msg TYPE REF TO cx_salv_msg.
TABLES: rspcchain.
*selection screen setup
SELECT-OPTIONS chain_id FOR rspcchain-chain_id.
SELECT-OPTIONS type FOR rspcchain-type.
*filling local table
SELECT chain_id variante
FROM rspcchain INTO TABLE lt_rspcchain
WHERE chain_id IN chain_id AND
type IN type AND
objvers = 'A'.
*original code to test printing output on screen
*LOOP AT lt_rspcchain INTO lwa_rspcchain.
* skip.
* WRITE lwa_rspcchain-chain_id.
* WRITE lwa_rspcchain-variant.
*ENDLOOP.
IF sy-subrc NE 0. "sy-subrc = return code
WRITE 'Data not found'.
ENDIF.
*loading data from local table into alv object table
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = o_alv
CHANGING
t_table = lt_rspcchain ).
CATCH cx_salv_msg INTO lx_msg.
ENDTRY.
*calling display method to display table
o_alv->display( ).
You can use the SALV framework for this, it comes with a class to export whatever would be displayed to various formats, including .MHTML and .XML formats that are understood by Excel. The class CL_SALV_TABLE has a method TO_XML to support this; additionally, you might need the CL_SALV_BS_XML_UTILS to handle the transformations. See the report SALV_TEST_TABLE_DISPLAY_OR_XML for example coding.
You should look at the ABAP2XLSX project.
http://wiki.sdn.sap.com/wiki/display/ABAP/abap2xlsx
Not sure if all the necessary components exist in BW, but this is really the best solution for creating spreadsheets out of ABAP code that I have found.
You can try the following which will download a CSV file, which with the .xls extension opens flawlessly in Excel:
Convert lt_rspcchain to a csv internal table by calling SAP_CONVERT_TO_CSV_FORMAT
Figure out where the user wants to store the file by calling cl_gui_frontend_services=>file_save_dialog( )
Store the file by calling cl_gui_frontend_services=>gui_download( )
I assume you will be able to find how these work by experience or through Google.

Looking for Non-Printable characters inside internal table ABAP

I have an internal table this is written to file and then pulled into the BW as a datasource. Occasionally a non printable character makes it into the file output and breaks the import process into the BW. Below is a sample of my code. Since the itab is not type c or string I am unable to use the find/replace regex on it. Has anyone else had to solve this type of problem before?
FORM eliminate_non_print_char TABLES p_shiptab STRUCTURE shiptab.
LOOP AT p_shiptab INTO wa_shiptab.
FIND REGEX '[^[:print:]]+(?!$)'
IN wa_shiptab
IGNORING CASE.
"RESULTS result.
IF sy-subrc = 0.
REPLACE REGEX '[^[:print:]]+(?!$)'
IN wa_shiptab WITH ''
IGNORING CASE.
ENDIF.
ENDLOOP.
DATA: BEGIN OF shiptab OCCURS 2000.
INCLUDE STRUCTURE ship1.
INCLUDE STRUCTURE ship2.
DATA: landtx LIKE vbrk-landtx,
bl_konwa LIKE vbak-waerk.
INCLUDE STRUCTURE ship3.
INCLUDE STRUCTURE ship4.
DATA: frght_amnt_usd LIKE konv-kwert,
revenue_amnt_usd LIKE vbap-netwr,
unit_price_usd LIKE vbap-netpr,
pgi_posting_date LIKE mkpf-budat,
ord_line_item_qty LIKE lips-lfimg,
asm_no LIKE kna1-kunnr,
asm_username LIKE adrc-sort1,
va_augru_t LIKE tvaut-bezei,
ship_to_name LIKE adrc-name1,
bill_to_name LIKE adrc-name1,
forward_to_name LIKE adrc-name1,
fmv_amnt LIKE konv-kbetr,
va_butxt LIKE t001-butxt,
sold_to_search_term LIKE adrc-sort1,
bill_to_search_term LIKE adrc-sort1,
va_prctr LIKE vbap-prctr,
va_bezei LIKE tvrot-bezei.
INCLUDE STRUCTURE zorder_attr.
DATA: extended_bits_count(20),
va_bstkd_hdr LIKE char32.
DATA: gsm_bp_katr6 LIKE kna1-katr6,
gsm_bp_vtext6 LIKE tvk6t-vtext,
asm_ze_katr7 LIKE kna1-katr7,
asm_ze_vtext7 LIKE tvk7t-vtext,
gsm_ze_katr6 LIKE kna1-katr6,
gsm_ze_vtext6 LIKE tvk7t-vtext.
DATA: END OF shiptab
.
The error I get is: "WA_SHIPTAB" must be a character-type data object (data type C, N, D,T, or STRING). I have non character types in the itab shiptab. I know I can do this lookup on each field individually, but the itab has 235 fields and that does not seem efficient.
The program has another copy of the main itab that is char based fields, I looped through this and placed the following code:
REPLACE ALL OCCURRENCES OF REGEX '[^[:print:]]+$'
IN transtab WITH ''
IGNORING CASE.
Links for the answer:
SCN Link
Help.SAP Link
You could use Runtime Type Sevices to loop through the definition of each field in the table to determine which are type-compatable with the REGEX operation. You can then use dynamic assignment to process only those fields which are compatable, one at a time. For example:
" Initalize Range of typekinds that are compatable with REGEX. See constant
" attributes of CL_ABAP_DATADESCR for typekind definitions. You'll have to
" define these explicitly
lr_typekind_regex = <...>.
" Get components of table structure
lo_tabdescr = cl_abap_typedescr=>describe_by_data( p_shiptab ).
lo_strdescr = lo_tabdescr->get_table_ine_type( ).
li_comp = lo_structdescr->get_components( ).
" Loop through table, sanitizing regex-compatable fields in each row
LOOP AT p_shiptab ASSIGNING <la_shiptab>.
LOOP AT li_comp INTO la_comp.
CHECK la_comp-type->type_kind IN lr_typekind_regex.
" Call subroutine containing Regex to remove non-printable chars from field
ASSIGN COMPONENT la_comp-name OF STRUCTURE <la_shiptab> TO <l_charvalue>.
PERFORM sanitize_field CHANGING <l_charvalue>.
ENDLOOP.
ENDLOOP.

How do I get my data to show up in my ALV?

I'm thinking that I'm probably missing an export parameter (from my Function Call POV).
In the REUSE_ALV_GRID_DISPLAY function call, the parameters I pass around are:
Exporting:
i_callback_program,
i_callback_pf_status_set,
i_callback_user_command,
is_layout,
it_fieldcat,
i_save
Tables:
t_outtab
And the exceptions plus handling.
I've checked that the internal table that I pass has data and it does.
I think the information I put up will suffice but if you really need to see the code, I'll do so.
I'm a noob and any help would be appreciated.
Thanx.
There are several ways to use ALV, so we may indeed need more info on your code to help.
First Method is to use the function module REUSE_ALV_GRID_DISPLAY. This will directly display the table content in the output dynpro. If all you need is a display, then go for it, as this is the simpliest : If the table structure is in the dictionnary, this call can be as simple as the following (this will display all members of the struct as column)
myreport = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = myreport
it_excluding = exclude_tab
TABLES
t_outtab = display_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
If the structure is declared in the program, then you have to create a field catalog.
the following code can serve as basis :
FORM fill_fieldcat CHANGING p_fieldcat TYPE slis_t_fieldcat_alv.
* Data definition
DATA ls_fieldcat TYPE slis_fieldcat_alv.
* Macro definition
DEFINE append_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = &1. * name of the field in struct
ls_fieldcat-tabname = &2. * name of the table
ls_fieldcat-row_pos = &3. * column
ls_fieldcat-ref_fieldname = &4. * field in ref table
ls_fieldcat-ref_tabname = &5. * ref table
ls_fieldcat-outputlen = &6. * size of output
ls_fieldcat-seltext_m = &7. * text (space if using the element typetext)
ls_fieldcat-ddictxt = 'M'.
ls_fieldcat-key = &8. * is this a key field in table
ls_fieldcat-emphasize = &9. * emphisze column display
append ls_fieldcat to p_fieldcat.
END-OF-DEFINITION.
* Init.
REFRESH p_fieldcat.
* Append fielcatalog for ALV
append_fieldcat:
'FORMATIONCODE' 'DISPLAY_TAB' 1 'SHORT' 'HRP1000' 12 'Code Stage' space space,
'FORMATIONTEXT' 'DISPLAY_TAB' 1 'STEXT' 'HRP1000' 20 'Libelle Stage' space space,
'SESSIONID' 'DISPLAY_TAB' 1 'OBJID' 'HRP1000' space 'Session' space space,
'BEGDA' 'DISPLAY_TAB' 1 'BEGDA' 'HRP1000' space 'Debut' space space,
'ENDDA' 'DISPLAY_TAB' 1 'BEGDA' 'HRP1000' space 'Fin' space space,
ENDFORM. "fill_fieldCat
you then call the form to create the field catalog, and use it in the it_fieldcat parameter of the function call.
Second method is to use ABAP-Object. Use check se83 for exemples of this use. the basis is as follows :
In your Dynpro you declare a custom container with a given name ("ALV_CONT"). Then in then PBO of the dynpro you initialize the container and put an ALV objct inside :
* global variables :
DATA : delegationlist_table TYPE REF TO cl_gui_alv_grid,
delegationlist_container TYPE REF TO cl_gui_custom_container.
data : gs_layout TYPE lvc_s_layo.
in PBO
IF delegationlist_container IS INITIAL.
* create a custom container control for our ALV Control
CREATE OBJECT delegationlist_container
EXPORTING
container_name = 'ALV_CONT'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
* create an instance of alv control
CREATE OBJECT delegationlist_table
EXPORTING
i_parent = delegationlist_container.
* Set a titlebar for the grid control
gs_layout-grid_title = 'Délégations'.
gs_layout-sel_mode = 'A'.
gs_layout-cwidth_opt ='X'.
* set table as data source
* the struct name *must* be uppercase
* the table must have this struc
CALL METHOD delegationlist_table->set_table_for_first_display
EXPORTING
i_structure_name = 'ZPRT_DELEGATIONLIST'
is_layout = gs_layout
CHANGING
it_outtab = delegationlist.
ENDIF.
Hopes this help,
Regards
Guillaume PATRY
EDIT: Oh, and another thing - if you're really in POV (process on Value-Request = F4), be aware that there are limitations to what you can do. Try your code in a simple report right after START-OF-SELECTION, and if that works, try the same code in a POV module.
===
If you don't pass a structure name, you have to ensure that you pass a complete (!) field catalog, otherwise the ALV grid might start to work erratically or not at all. Use the function modules LVC_FIELDCATALOG_MERGE and LVC_FIELDCAT_COMPLETE (in this order) to get a LVC field catalog that can be used with the classes or REUSE_ALV_GRID_DISPLAY_LVC.
A couple people here suggested using the REUSE_ALV_GRID_DISPLAY. I'm sure this is a common way to get things done (I used to use it myself), but I've taken a sap delta course recently and they strongly suggested to not use it anymore (you can look it up, REUSE_ALV_GRID_DISPLAY is not officialy supported by SAP anymore).
Instead, use CL_SALV_TABLE, documentation here: http://help.sap.com/erp2005_ehp_04/helpdata/EN/d7/b22041aa7df323e10000000a155106/frameset.htm
It's actually rather convenient to use too.
Thanks for the effort but as it turned out, the mistake I did was that I didn't capitalize field names in building the Field Catalog. Such a newbie mistake. I guess I won't be doing that mistake again any time soon.
-migs
Pass the output internal table to the FM Parameter "t_outtab".
It will print your data output.