You are currently browsing the archives for the ABAP category


get BSP URL parameters – page redirector

Background: I had to write these BSP’s a few times. Once was for a plain vanilla redirector that got its values pushed from another application and a different time is when I needed to create a RESTful web service.

What: BSP application ZBSP_REDIR (SAPlink nugget)

How to use:

Under the Event Handler tab within your BSP. Select the “OnRequest” method.


"For this example, we will be pushing in appname
 "  then we will use our make believe business logic
 "  and generate a redirection URL based off the
 "  appname.

 "     Name value pair's
 DATA: lt_HTTPNVP TYPE TIHTTPNVP,
       ls_HTTPNVP TYPE IHTTPNVP.

 "Get the URL variables and save them into the table.
 runtime->server->request->GET_FORM_FIELDS( CHANGING FIELDS = lt_HTTPNVP ).

 "Get the variable from the table
 READ TABLE lt_HTTPNVP INTO ls_HTTPNVP WITH TABLE KEY NAME = 'appname'.

 "Save to the Page Attribute which will hold the redirection URL
 CASE ls_HTTPNVP-VALUE.
   WHEN 'redirect_example'.
      redirection_url = 'http://markszcz.net/get-bsp-url-parameters-page-redirector'.
   WHEN OTHERS.
     redirection_url = 'http://markszcz.net/'.
 ENDCASE.

The URL I used to test the was    http://***/sap/bc/bsp/sap/zbsp_redir/index.htm?appname=redirect_example

CSV utility class

*Note* This page links to SAPlink compliant code. If you are unfamiliar with SAPlink, please visit this page.

Background: From time to time, I have had requests to work with custom data in excel and import it in an ABAP program or was asked to export some processed data back into excel, that I decided to write a CSV (Comma Separated Values) utility class that would make my life a little bit easier.

What: My custom SAP class ZCL_CSV (SAPlink nugget)

Methods:

ZCL_CSV methods list.

How to use: Pass in two parameters and one changing parameter. The two parameters are the CSV file and its defined structure, and the changing parameter is the table you want your data to be put into.

CSV_TO_TABLE parameters

Code Sample:


ZCL_CSV=>CSV_TO_TABLE(
  EXPORTING
     IV_CSV = "CSV File as a string.
     IS_CSV = "The structure of the CSV file
  CHANGING
     ET_CONVERTED_TABLE = "The table to store the data  ).

Full Code Sample:

 DATA: lv_data type string.

 TYPES: begin of ty_struct,
   comp_foo type string,
   comp_bar type string,
   comp_baz type string,
   end of ty_struct.

 TYPES: begin of ty_struct2,

   comp_bar type string,
   comp_baz type string,
   comp_qux type string,
   end of ty_struct2.

 DATA: ls_struct TYPE ty_struct. "CSV structure
 DATA: lt_struct TYPE TABLE OF ty_struct2. "My table structure

 DATA: lv_filename TYPE string.
 DATA: lv_file_data_string TYPE STRING.

 DATA: lv_file_save TYPE STRING.
 DATA: lt_csv_table TYPE /SAPTRX/TABLE_OF_STRINGS.

 CONCATENATE 'foo,bar,baz' cl_abap_char_utilities=>cr_lf 'foo2,bar2,baz2' INTO lv_data.

 ZCL_CSV=>SHOW_OPEN_DIALOG( CHANGING iv_filename = lv_filename ).

 lv_file_data_string = ZCL_CSV=>GET_FILE( lv_filename ) .

 ZCL_CSV=>CSV_TO_TABLE(
   EXPORTING
     IV_CSV =  lv_file_data_string
     IS_CSV = ls_struct
   CHANGING
     ET_CONVERTED_TABLE = lt_struct   ).

 lt_csv_table = ZCL_CSV=>TABLE_TO_CSV( lt_struct ) .
 lv_file_save = ZCL_CSV=>SHOW_SAVE_DIALOG( ).
 ZCL_CSV=>SAVE_FILE( IV_FILENAME = lv_file_save IV_CSV_TABLE = lt_csv_table ).