cancel
Showing results for 
Search instead for 
Did you mean: 

How to change Posting key from 11 to 15 for 2nd line item in FB03 by BAPI_ACC_DOCUMENT_POST (ABAP)

vinayak_zaware
Explorer
0 Kudos
894

Hi all,

I want to change posting key from 11 to 15 for 2ndline item in FB03 by BAPI_ACC_DOCUMENT_POST.

Currently using excel file to upload file to upload data in BAPI_ACC_DOCUMENT_POST.

after executing my program goes to check in FB03

after filling data press Enter.

As shown in above image I want Posting key 15 for 2nd item but as you see its coming as 11.

How can I solve this.

Thanks in Advance!!

My Code:

*&---------------------------------------------------------------------*
*& Report ZFI_COLLECTION_INTERFACE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZFI_COLLECTION_INTERFACE.

*------------------------------------------------------------------------------------->
*---- Data Structures
*------------------------------------------------------------------------------------->

"EXCEL FILE STRUCTURE
TYPES : BEGIN OF ST_UPLOAD,
          C1  TYPE BAPIDOCCUR, "CURR23_2,  "1 Amount
          C2  TYPE CURR23_2,  "2
          C3  TYPE CURR23_2,  "3
          C4  TYPE C,         "4
          C5  TYPE C,
          C6  TYPE C,
          C7  TYPE C,
          C8  TYPE C,
          C9  TYPE C,
          C10 TYPE C,
          C11 TYPE C,
          C12 TYPE WAERS, "CUKY52,      "Currency
          C13 TYPE CHAR10,      "Account(KUNNR)
          C14 TYPE CHAR4,       " Company Code , Business Place
          C15 TYPE C,
          C16 TYPE C,
          C17 TYPE C,
          C18 TYPE C,
          C19 TYPE C,
          C20 TYPE C,
          C21 TYPE C,
          C22 TYPE C,
          C23 TYPE C,
          C24 TYPE C,
          C25 TYPE C,
          C26 TYPE C,
          C27 TYPE C,
          C28 TYPE C,
          C29 TYPE HKONT, "CHAR10,    "Account(RACCT)
          C30 TYPE C,
          C31 TYPE CHAR16,    "Reference
          C32 TYPE C,
          C33 TYPE C,
          C34 TYPE C,
          C35 TYPE C,
          C36 TYPE C,
          C37 TYPE DATS,      "Value Date
          C38 TYPE C,
          C39 TYPE CHAR18,    "Doc.Header Text , Assignment
          C40 TYPE C,
          C41 TYPE C,
          C42 TYPE C,
          C43 TYPE C,
        END OF ST_UPLOAD.

*------------------------------------------------------------------------------------->
*---- Data Types
*------------------------------------------------------------------------------------->

"F4 help file name upload data type--->
DATA: LV_RC TYPE I.
DATA: LT_FILE_TABLE TYPE FILETABLE,
      LS_FILE_TABLE TYPE FILE_TABLE.



"IT & WA of Excel File
DATA : IT_UPLOAD TYPE TABLE OF ST_UPLOAD,
       WA_UPLOAD TYPE ST_UPLOAD.

DATA : G_RAW_DATA TYPE TRUXS_T_TEXT_DATA,
       File_NM    TYPE RLGRAP-FILENAME.

*DATA: File_NM TYPE RLGRAP-FILENAME.



"DATA Types For Checking File Name
DATA: STR_LEN TYPE I,
      DZ      TYPE Char2.


" Data Types for BAPI : (BAPI_ACC_DOCUMENT_POST)
DATA : IT_ACCOUNTGL  TYPE TABLE  OF BAPIACGL09,
       WA_ACCOUNTGL  TYPE  BAPIACGL09.

DATA : IT_ACCOUNTRECEIVABLE TYPE TABLE  OF  BAPIACAR09,
       WA_ACCOUNTRECEIVABLE TYPE BAPIACAR09.

DATA : IT_CURRENCYAMOUNT TYPE TABLE  OF BAPIACCR09,
       WA_CURRENCYAMOUNT TYPE BAPIACCR09.

DATA : IT_DOCUMENTHEADER TYPE TABLE OF BAPIACHE09,
       WA_DOCUMENTHEADER TYPE BAPIACHE09.

DATA : IT_RETURN TYPE TABLE OF BAPIRET2,
       WA_RETURN TYPE BAPIRET2.

DATA : OBJ_TYPE TYPE  BAPIACHE09-OBJ_TYPE,
       OBJ_KEY  TYPE  BAPIACHE09-OBJ_KEY,
       OBJ_SYS  TYPE  BAPIACHE09-OBJ_SYS.

"For BAPI Extension
DATA : IT_EXTENSION_2 TYPE TABLE OF BAPIPAREX,
       WA_EXTENSION_2 TYPE BAPIPAREX.

"For Item no cal culation
DATA LV_NO TYPE NUM10.

"For Assign No
DATA LV_AS TYPE Char18.

"For Doc Amount (CURRENCYAMOUNT-AMT_DOCCUR) to get total
DATA LV_AMT TYPE  BAPIDOCCUR. "CURR23_2.
DATA LV_CURR TYPE WAERS. "CUKY.


*------------------------------------------------------------------------------------->
*---- Selection Screen
*------------------------------------------------------------------------------------->

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
  PARAMETERS : P_FILE TYPE IBIPPARMS-PATH OBLIGATORY.
SELECTION-SCREEN END OF BLOCK B1 .

*------------------------------------------------------------------------------------->
*---- F4 Help for File uploading Loc
*------------------------------------------------------------------------------------->
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      PROGRAM_NAME  = SYST-CPROG
      DYNPRO_NUMBER = SYST-DYNNR
      FIELD_NAME    = ' '
    IMPORTING
      FILE_NAME     = P_FILE.



  " Write path on input area
  LOOP AT LT_FILE_TABLE INTO P_FILE.
  ENDLOOP.


AT SELECTION-SCREEN.

  "To check File Name Starts with DZ or not
  STR_LEN = STRLEN( P_FILE ).
  STR_LEN = STR_LEN - 18.
  DZ = P_FILE+STR_LEN(2).

  IF DZ IS NOT INITIAL.
    IF DZ EQ 'DZ' OR DZ EQ 'dz' OR
       DZ EQ 'Dz' OR DZ EQ 'dZ'.
***      MESSAGE 'File Name is Verified' TYPE 'S'.
    ELSE.
      MESSAGE 'File Name should be starts with "DZ" format only' TYPE 'E'.
    ENDIF.
  ENDIF.



START-OF-SELECTION.
  PERFORM UPLOAD_EXCEL.
  PERFORM BAPI_PROCESS.


*&---------------------------------------------------------------------*
*& Form UPLOAD_EXCEL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM UPLOAD_EXCEL .
  BREAK-POINT.
  MOVE P_FILE TO FILE_NM.
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      I_FIELD_SEPERATOR    = 'X'
      I_LINE_HEADER        = 'X'
      I_TAB_RAW_DATA       = G_RAW_DATA
      I_FILENAME           = FILE_NM  "P_FILE
    TABLES
      I_TAB_CONVERTED_DATA = IT_UPLOAD[]
    EXCEPTIONS
      CONVERSION_FAILED    = 1
      OTHERS               = 2.
  IF SY-SUBRC <> 0.
    MESSAGE 'FILE UPLOAD FAILED' TYPE 'S' DISPLAY LIKE 'E'.
  ENDIF.

  DELETE IT_UPLOAD WHERE C1 IS INITIAL.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form BAPI_PROCESS
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM BAPI_PROCESS .

  "Here Data will be processed & then Passes to BAPI & Checking its Status

  LOOP AT IT_UPLOAD INTO WA_UPLOAD.

    "IT Header

    "Item No.
    LV_NO = LV_NO + 1.
    WA_ACCOUNTGL-ITEMNO_ACC = LV_NO.
    WA_CURRENCYAMOUNT-ITEMNO_ACC = LV_NO.

    "WA_ACCOUNTRECEIVABLE-ITEMNO_ACC to get +1 in extra for scenario
    WA_ACCOUNTRECEIVABLE-ITEMNO_ACC = LV_NO + 1.





    "Document Date(2)
    WA_DOCUMENTHEADER-DOC_DATE = WA_UPLOAD-C37.

    "Document Type(3)
    WA_DOCUMENTHEADER-DOC_TYPE = 'DZ'.

    "Company Code(4)
    WA_DOCUMENTHEADER-COMP_CODE = WA_UPLOAD-C14.
    WA_ACCOUNTRECEIVABLE-COMP_CODE = WA_UPLOAD-C14.
    WA_ACCOUNTGL-COMP_CODE = WA_UPLOAD-C14.

    "Posting Date(5)
    WA_DOCUMENTHEADER-PSTNG_DATE = WA_UPLOAD-C37.

    "Currency(6)
    WA_CURRENCYAMOUNT-CURRENCY = WA_UPLOAD-C12.

    IF LV_CURR IS INITIAL.
      LV_CURR = WA_UPLOAD-C12.
    ENDIF.


    "Reference(7)
    WA_DOCUMENTHEADER-REF_DOC_NO =  WA_UPLOAD-C31.

    "Doc Header txt(8)
    WA_DOCUMENTHEADER-HEADER_TXT = WA_UPLOAD-C39.


    "Account(10)
    WA_ACCOUNTRECEIVABLE-CUSTOMER = WA_UPLOAD-C13.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = WA_UPLOAD-C29
      IMPORTING
        OUTPUT = WA_UPLOAD-C29.

    WA_ACCOUNTGL-GL_ACCOUNT = WA_UPLOAD-C29.

    "Amount(12)
    WA_CURRENCYAMOUNT-AMT_DOCCUR = WA_UPLOAD-C1.

    "For Calculating total amount
    LV_AMT = LV_AMT + WA_CURRENCYAMOUNT-AMT_DOCCUR.

    "Profit Center(13)
    SELECT SINGLE KUNNR, KONZS FROM KNA1 INTO @DATA(WA_KNA1)
      WHERE KUNNR = @WA_UPLOAD-C13.
    IF SY-SUBRC = 0.
      WA_ACCOUNTRECEIVABLE-PROFIT_CTR = WA_KNA1-KONZS.
      WA_ACCOUNTGL-PROFIT_CTR = WA_KNA1-KONZS.
    ENDIF.

    "Value Date(14)
    WA_ACCOUNTGL-VALUE_DATE = WA_UPLOAD-C37.


    "Assignment(15)
    WA_ACCOUNTRECEIVABLE-ALLOC_NMBR =  WA_UPLOAD-C39.
    WA_ACCOUNTGL-ALLOC_NMBR =  WA_UPLOAD-C39.


    "Business Place(16)
    WA_ACCOUNTRECEIVABLE-BUSINESSPLACE = WA_UPLOAD-C14.

    "Section Code(17)
    WA_ACCOUNTRECEIVABLE-SECTIONCODE = 'BDX0'.


    "Debit Credit Indicator
    IF SY-TABIX = 1.
      WA_ACCOUNTGL-DE_CRE_IND = 'S'.
    ELSE.
      WA_ACCOUNTGL-DE_CRE_IND = 'H'.
    ENDIF.




***    "ASSIGN
***    SHIFT LV_NO LEFT DELETING LEADING '0'.
***    LV_AS = 'Assign'.
***    CONCATENATE LV_AS LV_NO INTO LV_AS.
***    WA_ACCOUNTGL-ALLOC_NMBR = LV_AS.

    "User Name
    WA_DOCUMENTHEADER-USERNAME = SY-UNAME.


*    APPEND WA_DOCUMENTHEADER TO IT_DOCUMENTHEADER.
    APPEND WA_ACCOUNTRECEIVABLE TO IT_ACCOUNTRECEIVABLE.
    APPEND WA_ACCOUNTGL TO IT_ACCOUNTGL.
    APPEND WA_CURRENCYAMOUNT TO IT_CURRENCYAMOUNT.

    "Deleting duplicates to solve (E RW  014 FI/CO interface: Line item entered several times) Issue
***    DELETE ADJACENT DUPLICATES FROM IT_ACCOUNTRECEIVABLE.
***    DELETE ADJACENT DUPLICATES FROM IT_ACCOUNTGL.


    CLEAR : WA_ACCOUNTRECEIVABLE,WA_ACCOUNTGL,WA_CURRENCYAMOUNT, WA_KNA1."WA_DOCUMENTHEADER,
    CLEAR : LV_AS.


  ENDLOOP.


** --------------------------------------------------------------------------------------------->
**  Logic for All values addition or Deletion for BAPI last line of CURRENCYAMOUNT Table-------->
** --------------------------------------------------------------------------------------------->

  CLEAR WA_CURRENCYAMOUNT.

  WA_CURRENCYAMOUNT-ITEMNO_ACC = LV_NO + 1.
  WA_CURRENCYAMOUNT-CURRENCY = LV_CURR.
  WA_CURRENCYAMOUNT-AMT_DOCCUR = - LV_AMT.

  APPEND WA_CURRENCYAMOUNT TO IT_CURRENCYAMOUNT.


*  *     Populate the Extension table
  WA_EXTENSION_2-STRUCTURE  = 'POSTING_KEY'.
  WA_EXTENSION_2-VALUEPART1 = '02'.            " Item number
  WA_EXTENSION_2-VALUEPART2 = '15'.            " Posting Key
  APPEND WA_EXTENSION_2 TO IT_EXTENSION_2.


** ----------------------------------------------------------------------------------->

** ----------------------------------------------------------------------------------->
**  BAPI CALLING --------------------------------------------------------------------->
** ----------------------------------------------------------------------------------->

  "1st BAPI
  CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
    EXPORTING
      DOCUMENTHEADER    = WA_DOCUMENTHEADER
*     CUSTOMERCPD       =
*     CONTRACTHEADER    =
*    IMPORTING
*     OBJ_TYPE          = OBJ_TYPE
*     OBJ_KEY           = OBJ_KEY
*     OBJ_SYS           = OBJ_SYS
    TABLES
      ACCOUNTGL         = IT_ACCOUNTGL
      ACCOUNTRECEIVABLE = IT_ACCOUNTRECEIVABLE
*     ACCOUNTPAYABLE    =
*     ACCOUNTTAX        =
      CURRENCYAMOUNT    = IT_CURRENCYAMOUNT
*     CRITERIA          =
*     VALUEFIELD        =
*     EXTENSION1        =
      RETURN            = IT_RETURN
*     PAYMENTCARD       =
*     CONTRACTITEM      =
      EXTENSION2        = IT_EXTENSION_2
*     REALESTATE        =
*     ACCOUNTWT         =
    .
* Check for errors.
  CLEAR : WA_RETURN.
  READ TABLE IT_RETURN INTO WA_RETURN WITH KEY TYPE = 'E'.
  IF SY-SUBRC NE 0.
    "2nd BAPI
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
    READ TABLE IT_RETURN INTO WA_RETURN INDEX 1.
    MESSAGE WA_RETURN-MESSAGE TYPE 'S'.
  ELSE.
** display the issues output
    LOOP AT IT_RETURN INTO WA_RETURN.
      WRITE :  WA_RETURN-TYPE,
               WA_RETURN-ID,
               WA_RETURN-NUMBER,
               WA_RETURN-MESSAGE.
    ENDLOOP.
  ENDIF.ENDFORM.

Please Help!

View Entire Topic
e_enginyilmaz
Participant

Hello,

I think the blog posts below are quite good

Using BadI:

https://blogs.sap.com/2018/03/27/enlarging-the-use-of-bapi_acc_document_post/

Using business transaction event RWBAPI01:

https://blogs.sap.com/2019/05/10/role-of-bte-process-rwbapi01-in-bapi_acc_document_post/

Alternatively you can also refer to BTE 1120 by changing the table T_BSEGSUB but above recommendations are better.

Kind regards,

Engin

vinayak_zaware
Explorer
0 Kudos

Thanks You