Sunday, 21 February 2010

Posting more than 999 records through BAPI

 

Posting more than 999 records through BAPI
Welcome Guest
View a printable version of the current page.

Added by Krishna Chauhan , last edited by Craig Cmehil on Dec 28, 2009  (view change)

Labels:

abap, tutorial

Add Labels

Wait Image

Enter labels to add to this page:

Tip: Looking for a label? Just start typing.

Introduction: There are a number of scenarios when we found our BAPI does not post/check records more than 999 line items. Actually there is a limitation in BAPI where we can't post more than 999 records. This small piece of code will serve our purpose.
Suppose our XL file contains thousand of records & we have to check/post using BAPI in a bunch of 950 records.
We need to upload the data of XL to our internal table, let's say we have uploaded the records to internal table   ITAB_INDATA.

PERFORM POST_DOCUMENT .

FORM        POST_DOCUMENT .

DATA:  V_LINES  TYPE I,

              V_LINES1 TYPE I,

             COUNT(3)   TYPE N.*----declaration of internal tables used in BAPI

DATA: DOC_HEADER LIKE BAPIACHE09 OCCURS 0 WITH HEADER LINE,

            DOC_ITEM   LIKE BAPIACGL09 OCCURS 0 WITH HEADER LINE,

            DOC_VALUES LIKE BAPIACCR09 OCCURS 0 WITH HEADER LINE,

            CUSTOMER   LIKE BAPIACAR09 OCCURS 0 WITH HEADER LINE,   

            VENDOR     LIKE BAPIACAP09 OCCURS 0 WITH HEADER LINE,   

           CHECK_RETURN LIKE BAPIRET2 OCCURS 0 WITH HEADER LINE,

           OBJTYP TYPE  AWKEY.

  DATA:  V_BAL   TYPE P DECIMALS 4,

               V_BAL1  TYPE P DECIMALS 4,

               V_INDEX LIKE SY-TABIX.*---set the counter to '0' which will calculate the number of records

COUNT = 0.

CLEAR: ITAB_INDATA.  *---Calculating the number of records in internal table

DESCRIBE TABLE ITAB_INDATA LINES V_LINES.*----Logic to be implemented inside the internal table

  LOOP AT ITAB_INDATA.    *----Clearing the internal tables before appending data
    CLEAR: DOC_HEADER[], DOC_ITEM[], CUSTOMER[], VENDOR[], DOC_VALUES[].

    CLEAR: LWA_INPUT_FILE.*----Here we are filling the structure and internal tables corresponding to Transaction Number field in our  internal table

AT NEW TNO.

      LOOP AT ITAB_INDATA INTO LWA_INPUT_FILE WHERE TNO = ITAB_INDATA-TNO.* Fill Document Header

        DOC_HEADER-USERNAME = SY-UNAME.

        DOC_HEADER-COMP_CODE  = 'BP01'.

        DOC_HEADER-HEADER_TXT = LWA_INPUT_FILE-BKTXT.

        DOC_HEADER-REF_DOC_NO = LWA_INPUT_FILE-XBLNR.

        DOC_HEADER-DOC_DATE   =  LWA_INPUT_FILE-DDATE.

        DOC_HEADER-PSTNG_DATE =  LWA_INPUT_FILE-PDATE.

        DOC_HEADER-DOC_TYPE   = LWA_INPUT_FILE-BLART.

        ADD  1 TO COUNT.  ** Fill Document Item

        MOVE COUNT TO DOC_ITEM-ITEMNO_ACC.

        MOVE COUNT TO DOC_VALUES-ITEMNO_ACC.

          DOC_ITEM-GL_ACCOUNT = LWA_INPUT_FILE-GLACC.

          DOC_ITEM-PSTNG_DATE = LWA_INPUT_FILE-PDATE.

          DOC_ITEM-DOC_TYPE   = LWA_INPUT_FILE-BLART.

           DOC_ITEM-PROFIT_CTR = LWA_INPUT_FILE-PC.

           DOC_ITEM-ALLOC_NMBR = LWA_INPUT_FILE-ASSGN.

           DOC_ITEM-ITEM_TEXT  = LWA_INPUT_FILE-TEXT.

           DOC_ITEM-COSTCENTER = LWA_INPUT_FILE-CC.

           DOC_ITEM-ACCT_TYPE  = LWA_INPUT_FILE-ACTYP.

           DOC_ITEM-COMP_CODE  = 'BP01'.

          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

            EXPORTING

              INPUT  = DOC_ITEM-GL_ACCOUNT

            IMPORTING

              OUTPUT = DOC_ITEM-GL_ACCOUNT. 
          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

            EXPORTING

              INPUT  = DOC_ITEM-COSTCENTER

            IMPORTING

              OUTPUT = DOC_ITEM-COSTCENTER. 
          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

            EXPORTING

              INPUT  = DOC_ITEM-PROFIT_CTR

            IMPORTING

              OUTPUT = DOC_ITEM-PROFIT_CTR.          APPEND DOC_ITEM.
          CLEAR DOC_ITEM. 
          MOVE COUNT TO VENDOR-ITEMNO_ACC.

          VENDOR-VENDOR_NO  = LWA_INPUT_FILE-GLACC.

          VENDOR-ALLOC_NMBR = LWA_INPUT_FILE-ASSGN.

          VENDOR-ITEM_TEXT  = LWA_INPUT_FILE-TEXT. 
          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

            EXPORTING

              INPUT  = VENDOR-VENDOR_NO

            IMPORTING

              OUTPUT = VENDOR-VENDOR_NO.

          APPEND VENDOR.

          CLEAR VENDOR.

          MOVE COUNT TO CUSTOMER-ITEMNO_ACC.

          CUSTOMER-CUSTOMER   = LWA_INPUT_FILE-GLACC.

          CUSTOMER-ALLOC_NMBR = LWA_INPUT_FILE-ASSGN.

          CUSTOMER-ITEM_TEXT  = LWA_INPUT_FILE-TEXT.

          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

            EXPORTING

              INPUT  = CUSTOMER-CUSTOMER

            IMPORTING

              OUTPUT = CUSTOMER-CUSTOMER.

          APPEND CUSTOMER.

          CLEAR CUSTOMER. * Fill Document Value.

        DOC_VALUES-CURRENCY_ISO = 'USD'.

        DATA: NUM TYPE P DECIMALS 2.

        CLEAR NUM.*---in case of Credit amount available

          DOC_VALUES-AMT_DOCCUR = LWA_INPUT_FILE-CRAMT * -1.*---in case of Debit  amount available

          DOC_VALUES-AMT_DOCCUR = LWA_INPUT_FILE-DRAMT.        APPEND DOC_VALUES.
        CLEAR DOC_VALUES.         V_LINES1 = V_LINES1 + 1.                "Counting line no
        READ TABLE ITAB_INDATA INDEX V_INDEX.

        IF ITAB_INDATA-DRAMT <> SPACE.

          CLEAR V_BAL.          "clear when found debit amount

          V_BAL1 = ITAB_INDATA-DRAMT.

          V_BAL = V_BAL + V_BAL1.

        ENDIF.

        IF SY-SUBRC = 0.

          IF ITAB_INDATA-CRAMT <> SPACE.

            V_BAL1 = ITAB_INDATA-CRAMT * -1.

            V_BAL = V_BAL + V_BAL1.

          ENDIF.

        ENDIF.    *----V_LINES contains total no of records in internal table
        IF V_LINES >= 950.

          IF V_LINES1 >= 950

                        AND V_BAL = 0.*---call bapi

              PERFORM CHECK_BAPI.

           ENDIF.   "IF CHECK <> 'X'.*---refreshing tables for storing another chunk of records

            REFRESH: DOC_ITEM, CUSTOMER, VENDOR, DOC_VALUES.

            clear COUNT.

            V_LINES = V_LINES - V_LINES1.

            CLEAR: V_LINES1, V_BAL.   "clearing balance & line counts

            CONTINUE.

          ELSE.

            CONTINUE.

        ENDIF.

      ENDLOOP.*----In case records less than 950

      IF V_LINES < 950

                 and V_LINES <> 0.  "avoiding if remaining records = 0.*---call bapi

            PERFORM CHECK_BAPI.*---refreshing tables for storing another chunk of records

            REFRESH: DOC_ITEM, CUSTOMER, VENDOR, DOC_VALUES.

          ENDIF.   "IF CHECK <> 'X'.

    ENDAT.

  ENDLOOP.

ENDFORM.                    " POST_DOCUMENT 
FORM CHECK_BAPI .

  CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK'

    EXPORTING

      DOCUMENTHEADER          = DOC_HEADER

    TABLES

     ACCOUNTGL               = DOC_ITEM

     ACCOUNTRECEIVABLE       = CUSTOMER

     ACCOUNTPAYABLE          = VENDOR

     CURRENCYAMOUNT          = DOC_VALUES

      RETURN                  = CHECK_RETURN    .*----If CHECK_RETURN contains Success result then post documents using

CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'

    EXPORTING

      DOCUMENTHEADER    = DOC_HEADER

    IMPORTING

      OBJ_KEY           = OBJTYP

    TABLES

      ACCOUNTGL         = DOC_ITEM

      ACCOUNTRECEIVABLE = CUSTOMER

      ACCOUNTPAYABLE    = VENDOR

      CURRENCYAMOUNT    = DOC_VALUES

      RETURN            = RETURN.

*---If no error found commit BAPI   'BAPI_TRANSACTION_COMMIT'

  ENDFORM.                    " CHECK_BAPI

When the documents get posted the 'BAPI_ACC_DOCUMENT_POST' will generate an document no.

We can pass that document number to FB03 Transaction to check out the posted documents.

SAP Community Network Wiki - ABAP Development - Posting more than 999 records through BAPI

No comments:

Post a Comment