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: 

Extract itab into a tab delimeted file in background

ekekakos
Participant
0 Kudos

Dear all,

I must create a program that will read a table with some selection criteria and if the checkbox background is enable, the program will create the file in background and if the checkbox is disable it will create in foreground.

1 of the tables that will be extract is bseg. So we are speaking about a lot of records with all fields of the table.

Any idea?

Thanks in advance

Elias

1 ACCEPTED SOLUTION

raymond_giuseppi
Active Contributor

You forget a 'd' in the 'p_bckgrd' parameter, so your report receive the default value 'X' and create another background job ad infinitum.

WITH p_bckgrD = space
6 REPLIES 6

Former Member
0 Kudos

Background means, that the file is stored in the file system of application server?

Foreground means, that the file is downloaded to client with CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD?

Sandra_Rossi
Active Contributor
0 Kudos

Any idea of what? Among : How to define some selection criteria, how to download in background, how to download in foreground, how to read a table, how to convert an internal table into a file, is it possible to have big files, etc. ? All these questions have been asked already in the forum.

Jelena
Active Contributor
0 Kudos

Uhm... Do not extract bseg?

Sorry, I'm confused - what kind of ideas are expected here?

0 Kudos

Auditors want to extract specific records from BKPF, BSEG and some other tables in a Delimited file. Our SAP is ECC6 and the code for BKPF is the following:

REPORT  zfor_get_bkrf_bseg4.
TABLES: bkpf.
DATA: it_bkpf TYPE STANDARD TABLE OF bkpf,
      ls_bkpf TYPE bkpf,
      i_filename LIKE rlgrap-filename.
FIELD-SYMBOLS: <fs_field>.

SELECTION-SCREEN BEGIN OF BLOCK selection WITH FRAME TITLE text-s01.
SELECT-OPTIONS: so_bukrs FOR bkpf-bukrs,
                so_budat FOR bkpf-budat OBLIGATORY.

SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN ULINE.
PARAMETERS: p_bkgrd AS CHECKBOX USER-COMMAND check DEFAULT 'X'.
" File Path on Application Server or on Local PC according to p_bkgrd
PARAMETERS: p_paths TYPE btcxpgpar DEFAULT '/tmp' MODIF ID sg1.
SELECTION-SCREEN END OF BLOCK selection.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_paths.
  DATA: c_fnh_mask TYPE dxfields-filemask VALUE '*',
        search_dir TYPE dxfields-longpath .

  CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
    EXPORTING
      directory        = search_dir
      filemask         = c_fnh_mask
    IMPORTING
      serverfile       = p_paths
    EXCEPTIONS
      canceled_by_user = 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.

START-OF-SELECTION.
  IF p_bkgrd = 'X'.
    PERFORM exec_in_bckgr.
  ELSE.
    PERFORM get_data.
    PERFORM download_tables_paths.
  ENDIF.

END-OF-SELECTION.
  WRITE: i_filename , ' is created' .

FORM get_data .

  SELECT * INTO TABLE it_bkpf
    FROM bkpf
    WHERE bukrs IN so_bukrs AND
          budat IN so_budat.

ENDFORM.                    " GET_DATA

FORM download_tables_paths .

  DATA:lv_line(4096) TYPE c,
       lv_field_type(10) TYPE c,
       lv_field_text(10) TYPE c.

  " Build FineName
  CONCATENATE p_paths '/' 'BKPF' sy-datum sy-uzeit '.txt'
                                                  INTO i_filename.
  REPLACE ALL OCCURRENCES OF '//' IN i_filename WITH '/'.
* Process further only if found some data
  IF NOT it_bkpf[] IS INITIAL.
    " Open file for Output
    OPEN DATASET  i_filename  FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
    IF sy-subrc NE 0 .
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
       WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ELSE.
      LOOP AT it_bkpf INTO ls_bkpf.
        DO.
          ASSIGN COMPONENT sy-index OF STRUCTURE ls_bkpf TO <fs_field>.
          IF sy-subrc <> 0.
            EXIT.
          ENDIF.
          IF lv_line IS INITIAL.
            lv_line = <fs_field>.
          ELSE.
            DESCRIBE FIELD <fs_field> TYPE lv_field_type.
            IF lv_field_type = 'P' OR lv_field_type = 'I'.
              lv_field_text = <fs_field>.
              CONDENSE lv_field_text NO-GAPS.
              CONCATENATE lv_line '|' lv_field_text INTO lv_line.
            ELSE.
              CONCATENATE lv_line '|' <fs_field> INTO lv_line.
            ENDIF.
          ENDIF.
        ENDDO.
        TRANSFER lv_line TO i_filename.
        CLEAR: lv_line.
      ENDLOOP.
    ENDIF.

    CLOSE DATASET i_filename.
  ENDIF.
ENDFORM. 

FORM exec_in_bckgr .
  DATA: jobname1 TYPE tbtcjob-jobname,
        jobcount1 TYPE tbtcjob-jobcount.

  jobname1 = 'ZFOR_GET_BKRF_BSEG2'.

  CALL FUNCTION 'JOB_OPEN'
    EXPORTING
      jobname          = jobname1
    IMPORTING
      jobcount         = jobcount1
    EXCEPTIONS
      cant_create_job  = 1
      invalid_job_data = 2
      jobname_missing  = 3
      OTHERS           = 4.

  IF sy-subrc NE 0.
    MESSAGE s368(00) WITH 'Error Creating Job'
    sy-subrc.
    EXIT.
  ENDIF.
  SUBMIT zfor_get_bkrf_bseg2
    WITH so_bukrs IN so_bukrs
    WITH so_budat IN so_budat
    WITH p_bckgr = space
    WITH p_paths = p_paths
   VIA JOB jobname1 NUMBER jobcount1
    AND RETURN.

  CALL FUNCTION 'JOB_CLOSE'
    EXPORTING
      jobcount          = jobcount1
      jobname           = jobname1
*      sdlstrtdt         = sdate
*      sdlstrttm         = stime
*      strtimmed         = 'X' " Start immediately
    EXCEPTIONS
      invalid_startdate = 1
      jobname_missing   = 2
      job_close_failed  = 3
      job_nosteps       = 4
      job_notex         = 5
      lock_failed       = 6
      OTHERS            = 7.

  IF sy-subrc > 0.
    MESSAGE s368(00) WITH 'Closing Job Failed'
    sy-subrc.
    EXIT.
  ENDIF.    
ENDFORM.

if I enable the strtimmed = 'X' in CALL FUNCTION 'JOB_CLOSE', then the program is creating jobs continuously and I have to kill it through SM50. If I disable the STRTIMED then it create a job as SCHEDULE. I run it as Immediate and it creates and run a job without creating a file while in foreground the program is working perfect.

So my question is: Is this code OK for SAP ECC6? Because in Netweaver 7.5 is working perfect.

Thanks

Elias

raymond_giuseppi
Active Contributor

You forget a 'd' in the 'p_bckgrd' parameter, so your report receive the default value 'X' and create another background job ad infinitum.

WITH p_bckgrD = space

0 Kudos

Thanks a lot Raymond. This D cost me 2 days of looking in the internet what was wrong.

Many thanks again.