• BWABAP code using BAPI's to load data into Cube from SpreadSheets


    1 Requirement Description

    We had a requirement to load data from a Spread sheet directly onto a Cube through an ABAP Program using the BAPI s in SAP BW.
    We required a dialog screen to give location of file and to mention whether the load is Plan or Actual. Plan data load would delete the previous loads and Actual would add upon the data. This is done by the settings at the info package level.
    Once the code is executed it loads file on the application server, and executes the following BAPI s to load data onto the cube.

    2 Relation Function List

    BAPI_IPAK_GETLIST - We pass the name of the Info source and Source system to get the list of related info packages.
    BAPI_IPAK_GETDETAIL - It is used to get details of the listed info packages 
    BAPI_IPAK_START - We pass the required info package name to start the load.
    BAPI_ISREQUEST_GETSTATUS - To check the status of the request after an elapsed time and show a message regarding the same.
    I am not an experienced ABAPer so the code has a lot of room for improvements, but as a BW guy I wanted to share the code because it was rather difficult in finding help on how to use the BAPI's 

    3 SourCode list:

    REPORT  FILEUPLOAD.

    data: w_dataset(255) value <path and file name on Application server>,
          req(30) type c,
          stt type c,
          g_t_data    type STANDARD TABLE OF <same as transfer Structure>
                                                       with header line."#EC

    data: int_message type standard table of BAPIRET2   with header line.
                                                                "#EC

    * Declaration for BAPI_IPAK_GETLIST
    data: int_ins     type standard table of BAPI6102SL  with header line,
          int_infopac type standard table of BAPI6109L   with header line,
          int_msg     type standard table of BAPIRET2    with header line,
          int_src     type standard table of BAPI6101SL  with header line,
          int_dtsrc   type standard table of BAPI6109DSSL
                                                       with header line."#EC

    * Declaration for BAPI_IPAK_GETDETAIL
    data: int_det     type standard table of BAPI6109     with header line,
          int_ret     type standard table of BAPIRET2     with header line,
          int_dlt     type standard table of BAPI6109IC   with header line,
          int_sel     type standard table of BAPI6109SEL  with header line,
          int_thrdp   type standard table of BAPI6109TCP  with header line,
          int_flparam type standard table of BAPI6109FILE
                                                       with header line."#EC


    SELECTION-SCREEN BEGIN OF BLOCK BLOCK1.
    PARAMETERS: P_FILE(255) type c.
    SELECTION-SCREEN END OF BLOCK BLOCK1.

    SELECTION-SCREEN COMMENT /1(30) text-h01.
    PARAMETERS: Actual radiobutton group rg1,
                Plan   radiobutton group rg1.


    AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
    *Providing F4 help for upload File name.
      PERFORM F3000_GET_FILE_NAME USING P_FILE.

    AT SELECTION-SCREEN ON P_FILE.
    * Check for file Existance on the Presentaion server
      PERFORM F3100_FILE_EXISTS USING P_FILE.

    START-OF-SELECTION.
    * Get data from File to internal table.
      PERFORM F3200_GET_DATA_FROM_FILE USING P_FILE.

    FORM F3000_GET_FILE_NAME USING    P_FILE.
    *   Displays Windows popup to get filename

      CALL FUNCTION 'WS_FILENAME_GET'
        EXPORTING
          DEF_FILENAME     = SPACE
          MASK             = ',*.*,*.*.'
          MODE             = 'O'
        IMPORTING
          FILENAME         = P_FILE
        EXCEPTIONS
          INV_WINSYS       = 1
          NO_BATCH         = 2
          SELECTION_CANCEL = 3
          SELECTION_ERROR  = 4
          OTHERS           = 5.
      IF SY-SUBRC = 4.
        MESSAGE .......
      ENDIF.

    ENDFORM.                    " f3000_get_file_name

    FORM F3100_FILE_EXISTS USING    P_FILE.

      DATA : P_EXISTS TYPE C.
    * Check if File Exists on Presentation Server
      CALL FUNCTION 'WS_QUERY'
        EXPORTING
          FILENAME = P_FILE
          QUERY    = 'FE'
        IMPORTING
          RETURN   = P_EXISTS.
      IF P_EXISTS = 0.
        MESSAGE ....
      ENDIF.

    ENDFORM.                    " F3100_FILE_EXISTS


    FORM F3200_GET_DATA_FROM_FILE USING    P_FILE.

      clear g_t_data.
      data : loc_file type string.
      loc_file = P_FILE.

      CALL FUNCTION 'GUI_UPLOAD'
        EXPORTING
          FILENAME                = loc_file
          FILETYPE                = 'ASC'
        TABLES
          DATA_TAB                = g_t_data
        EXCEPTIONS
          FILE_OPEN_ERROR         = 1
          FILE_READ_ERROR         = 2
          NO_BATCH                = 3
          GUI_REFUSE_FILETRANSFER = 4
          INVALID_TYPE            = 5
          NO_AUTHORITY            = 6
          UNKNOWN_ERROR           = 7
          BAD_DATA_FORMAT         = 8
          HEADER_NOT_ALLOWED      = 9
          SEPARATOR_NOT_ALLOWED   = 10
          HEADER_TOO_LONG         = 11
          UNKNOWN_DP_ERROR        = 12
          ACCESS_DENIED           = 13
          DP_OUT_OF_MEMORY        = 14
          DISK_FULL               = 15
          DP_TIMEOUT              = 16
          OTHERS                  = 17.
      IF SY-SUBRC <> 0.
        MESSAGE .....
      else.
        MESSAGE .....
      ENDIF.

      open dataset w_dataset for output in text mode encoding default.
      loop at g_t_data.
        transfer g_t_data to w_dataset.
      endloop.
      close dataset w_dataset.

      clear int_ins.
      clear int_infopac.
      clear int_msg.
      clear int_src.
      clear int_dtsrc.

      int_ins-SIGN = 'I'.
      int_ins-OPTION = 'EQ'.
      int_ins-INFOSOURCELOW = <Infosource Name>.
      append int_ins.

      int_src-SIGN = 'I'.
      int_src-OPTION = 'EQ'.
      int_src-SOURCESYSTEMLOW = <Source system Name>.
      append int_src.


      CALL FUNCTION 'BAPI_IPAK_GETLIST'
        TABLES
          SELINFOSOURCE    = int_ins
          SELSOURCESYSTEM  = int_src
          INFOPACKAGE_LIST = int_infopac
          RETURN           = int_msg.

      loop at int_infopac.


    *--------------------------------------------------------------
    * Clearing of the Standard parameter Tables
    *--------------------------------------------------------------

        clear int_det.
        clear int_ret.
        clear int_dlt.
        clear int_sel.
        clear int_thrdp.
        clear int_flparam.


        CALL FUNCTION 'BAPI_IPAK_GETDETAIL'
          EXPORTING
            INFOPACKAGE        = int_infopac-INFOPACKAGE
          IMPORTING
            DETAILS            = int_det
            FILE_PARAMS        = int_flparam
          TABLES
            SELECTIONS         = int_sel
            INFOCUBES          = int_dlt
            THIRD_PARTY_PARAMS = int_thrdp
            RETURN             = int_ret.

    *--------------------------------------------------------
    * When Actual Flag is on
    *--------------------------------------------------------
        if int_dlt-DELETEALLBEFORE <> 'X' and Actual EQ 'X'.
          if int_flparam-FILENAME EQ <path and file name on Application server>.
            clear req.
            clear stt.
            CALL FUNCTION 'BAPI_IPAK_START'
              EXPORTING
                INFOPACKAGE = int_infopac-INFOPACKAGE
              IMPORTING
                REQUESTID   = req
              TABLES
                RETURN      = int_message.

            wait up to 10 seconds.

            CALL FUNCTION 'BAPI_ISREQUEST_GETSTATUS'
              EXPORTING
                REQUESTID  = req
              IMPORTING
                TECHSTATUS = stt.

            if stt EQ 'Y'.
              MESSAGE .....
            elseif stt EQ 'G'.
              MESSAGE .....
            elseif stt EQ 'R'.
              MESSAGE .....
            endif.
          endif.

    *--------------------------------------------------------
    * When Plan Flag is on
    *--------------------------------------------------------
        elseif int_dlt-DELETEALLBEFORE EQ 'X' AND Plan EQ 'X'.
          if int_flparam-FILENAME EQ <path and file name on Application server>.
            clear req.
            clear stt.
            CALL FUNCTION 'BAPI_IPAK_START'
              EXPORTING
                INFOPACKAGE = int_infopac-INFOPACKAGE
              IMPORTING
                REQUESTID   = req
              TABLES
                RETURN      = int_message.

            wait up to 10 seconds.

            CALL FUNCTION 'BAPI_ISREQUEST_GETSTATUS'
              EXPORTING
                REQUESTID  = req
              IMPORTING
                TECHSTATUS = stt.

            if stt EQ 'Y'.
              MESSAGE .....
            elseif stt EQ 'G'.
              MESSAGE .....
            elseif stt EQ 'R'.
              MESSAGE .....
            endif.
          endif.

        endif.

      endloop.

    ENDFORM.                    " f3200_get_data_from_file

    Source URL:https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/2075

  • 相关阅读:
    设计模式工厂模式
    设计模式原型模式
    Excel自定义格式千分符
    浏览器报:net::ERR_EMPTY_RESPONSE解决方案
    git branch a无法显示远程分支解决办法
    .Net启动程序报错:It was not possible to find any compatible framework version
    自动化测试框架pytest 安装和入门到精通实战
    2020非常全的接口测试面试题及参考答案软件测试工程师没有碰到算我输!
    Python+unittest+requests+excel实现接口自动化测试框架项目实战
    软件测试必学之python+unittest+requests+HTMLRunner编写接口自动化测试集
  • 原文地址:https://www.cnblogs.com/xiaomaohai/p/6157330.html
Copyright © 2020-2023  润新知