-5

I want to extract all bkpf's fields into a tab delimited file in the background. How can we do it?

Thanks in advance Elias

PS: The code of the program that the Perform exec_in_bckgr doesn't understand the change of the Checkbox in SUBMIT WITH p_bckgr = space.

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.

The program is too simple and I do not understand why the site is asking me to add more comments. OK, 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?

Thanks in advance Elias

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
ekekakos
  • 563
  • 3
  • 20
  • 39

2 Answers2

1

The following code could be a solution. Select your lines from BSEG in 10.000 packages, bring it in csv-format and transfer it into the open file.

DO.
  SELECT * FROM bseg UP TO 10000 ROWS
     INTO TABLE @DATA(lt_bseg)
          WHERE bukrs = '0001'.

  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

  OPEN DATASET filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

  LOOP AT lt_bseg INTO DATA(ls_bseg).
    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE ls_bseg TO FIELD-SYMBOL(<fs_field>).
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      IF line IS INITIAL.
        line = <fs_field>.
      ELSE.
        line = line && ';' && <fs_field>.
      ENDIF.
      TRANSFER line TO filename.
      CLEAR: line.
    ENDDO.
  ENDLOOP.
ENDDO.

CLOSE DATASET filename.
Christian
  • 19
  • 1
  • Thanks a lot. I made some changes in your code as the SAP we have is SAP ECC 6.0. So the code is as I wrote above. The bad thing is that the background Perform is not working. It seems that it doesn't understand that I am changing the checkbox field **p_bckgr = space** in the SUBMIT. Does anyone knows why is this happening? Thanks Elias – ekekakos Nov 13 '17 at 07:52
0

Finally someone told me what was wrong in my code. I missed a D in WITH p_bckgr = space. I thought that if it is wrong the SAP will display an error, but this was a wrong thought. So the correct is WITH p_bkgrd = space.

Thanks all

ekekakos
  • 563
  • 3
  • 20
  • 39