Hi Siddesh,
BAPI_ACC_DOCUMENT_POST bapi alone will not clear the customer or vendor open line items, additionally you need to call FM: CLEAR_DOCUMENTS also.
Its worked for me.
Please find the coding snippet for clearing customer open line items.
*&---------------------------------------------------------------------*
*& Report ZR_kunnr_CLEAR
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zr_kunrr_clear NO STANDARD PAGE HEADING LINE-SIZE 160.
TABLES: bsid.
TYPES: BEGIN OF ty_budat,
sign(1),
option(2),
low TYPE budat,
high TYPE budat,
END OF ty_budat.
TYPES: BEGIN OF ty_kunnr,
sign(1),
option(2),
low TYPE kunnr,
high TYPE kunnr,
END OF ty_kunnr.
DATA: l_kunnr TYPE kunnr VALUE '474',
t_budat TYPE TABLE OF ty_budat,
w_budat TYPE ty_budat,
t_kunnr TYPE TABLE OF ty_kunnr,
w_kunnr TYPE ty_kunnr,
t_zdt_cust_det TYPE TABLE OF zdt_cust_det,
w_zdt_cust_det TYPE zdt_cust_det,
t_errormsg TYPE TABLE OF zst_errormsg1,
w_errormsg TYPE zst_errormsg1,
t_bsid TYPE TABLE OF bsid,
w_bsid TYPE bsid,
l_fiyrs TYPE budat,
l_fiyrt TYPE allgstid,
l_index TYPE sy-index,
l_belnr TYPE belnr_d,
l_message TYPE char20,
l_postingdate TYPE budat,
e_recordcount TYPE int4.
SELECT-OPTIONS: so_kunnr FOR bsid-kunnr.
PARAMETERS : p_fiyrt TYPE char4 OBLIGATORY DEFAULT '2010',
p_bukrs TYPE bukrs OBLIGATORY DEFAULT '1000'.
CLEAR: w_kunnr, w_budat, l_kunnr,l_fiyrs,l_fiyrt,l_postingdate.
CONCATENATE p_fiyrt '1231' INTO l_postingdate.
l_fiyrs = '19500101'.
CONCATENATE p_fiyrt '1231' INTO l_fiyrt .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = l_kunnr
IMPORTING
output = l_kunnr.
w_budat-sign = 'I'.
w_budat-option = 'BT'.
w_budat-low = l_fiyrs.
IF l_fiyrt IS NOT INITIAL.
w_budat-high = l_fiyrt.
ELSE.
w_budat-high = l_fiyrs.
ENDIF.
APPEND w_budat TO t_budat.
SELECT * FROM bsid INTO TABLE t_bsid WHERE bukrs = p_bukrs AND
kunnr IN so_kunnr AND
budat IN t_budat.
SORT t_bsid[] BY kunnr ASCENDING.
DELETE ADJACENT DUPLICATES FROM t_bsid COMPARING kunnr.
IF t_bsid[] IS NOT INITIAL.
CALL FUNCTION 'ZFM_KUNNR_CLEAR'
EXPORTING
i_bukrs = p_bukrs
i_fiyrt = p_fiyrt
IMPORTING
e_recordcount = e_recordcount
TABLES
t_bsid = t_bsid
et_zdt_cust_det = t_zdt_cust_det
et_errormsg = t_errormsg.
ELSE.
w_errormsg-sno = 1.
w_errormsg-kunnr = ' '.
w_errormsg-msg = 'No records found for search criteria'.
APPEND w_errormsg TO t_errormsg.
ENDIF.
IF t_errormsg[] IS NOT INITIAL.
WRITE:'Error Entries:'.
SKIP.
ENDIF.
LOOP AT t_errormsg INTO w_errormsg.
IF sy-tabix = 1.
WRITE:/1 'S.NO', 15 'KUNNR', 22 'MESSAGE'.
ULINE.
SKIP.
ENDIF.
WRITE:/1 w_errormsg-sno, 15 w_errormsg-kunnr, 22 w_errormsg-msg.
ENDLOOP.
ULINE.
SKIP 3 .
WRITE:'Success Entries:'.
SKIP.
LOOP AT t_zdt_cust_det INTO w_zdt_cust_det.
IF sy-tabix = 1.
WRITE:/1 'Record Count:',e_recordcount.
WRITE:/1 'S.NO',35 'BUKRS', 45 'KUNNR', 56 'BELNR', 71 'STATUS'.
ULINE.
SKIP.
ENDIF.
WRITE:/1 w_zdt_cust_det-z_sno, 35 w_zdt_cust_det-z_bukrs, 45 w_zdt_cust_det-z_kunnr, 55 w_zdt_cust_det-z_belnr, 70 w_zdt_cust_det-z_desc.
ENDLOOP.
*-------------------------------------------------------------------------------------------------------------------------------*
FUNCTION zfm_kunnr_clear.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(I_BUKRS) TYPE BUKRS OPTIONAL
*" VALUE(I_FIYRT) TYPE CHAR4 OPTIONAL
*" EXPORTING
*" VALUE(E_RESULT) TYPE CHAR20
*" VALUE(E_RECORDCOUNT) TYPE INT4
*" TABLES
*" T_BSID STRUCTURE BSID OPTIONAL
*" ET_ZDT_CUST_DET STRUCTURE ZDT_CUST_DET OPTIONAL
*" ET_ERRORMSG STRUCTURE ZST_ERRORMSG1 OPTIONAL
*"----------------------------------------------------------------------
DATA: w_bsid TYPE bsid,
t_lineitems TYPE TABLE OF bapi3007_2,
w_lineitems TYPE bapi3007_2,
w_return TYPE bapireturn,
l_kunnr TYPE kunnr VALUE '474',
t_zdt_cust_det TYPE TABLE OF zdt_cust_det,
w_errormsg TYPE zst_errormsg1.
DATA: w_doc_header TYPE bapiache09,
t_accountreceivable TYPE TABLE OF bapiacar09,
w_accountreceivable TYPE bapiacar09,
t_accountpayable TYPE TABLE OF bapiacap09,
w_accountpayable TYPE bapiacap09,
t_accounttax TYPE TABLE OF bapiactx09,
w_accounttax TYPE bapiactx09,
t_currencyamount TYPE TABLE OF bapiaccr09,
w_currencyamount TYPE bapiaccr09,
t_extension2 TYPE TABLE OF bapiparex,
w_extension2 TYPE bapiparex,
t_extension1 TYPE TABLE OF bapiacextc,
w_extension1 TYPE bapiacextc,
t_accountgl TYPE TABLE OF bapiacgl09,
w_accountgl TYPE bapiacgl09,
t_return TYPE TABLE OF bapiret2,
l_amount TYPE char18,
l_dmbtrp TYPE bapidmbtr,
l_result TYPE bapidmbtr,
l_dmbtrn TYPE bapidmbtr,
lv_type TYPE bapiache09-obj_type,
lv_key TYPE bapiache09-obj_key,
lv_sys TYPE bapiache09-obj_sys,
l_fiyrs TYPE budat,
l_fiyrt TYPE allgstid,
l_index TYPE sy-index,
l_belnr TYPE belnr_d,
l_datum TYPE bapi3007-key_date,
l_message TYPE char20,
l_postingdate TYPE budat,
l_count TYPE i.
CONCATENATE i_fiyrt '1231' INTO l_fiyrt.
l_postingdate = l_fiyrt.
REFRESH: et_zdt_cust_det[],et_errormsg[].
CLEAR: l_kunnr,l_belnr.
LOOP AT t_bsid INTO w_bsid.
l_kunnr = w_bsid-kunnr.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = l_kunnr
IMPORTING
output = l_kunnr.
REFRESH t_lineitems[].
CALL FUNCTION 'BAPI_AR_ACC_GETOPENITEMS'
EXPORTING
companycode = i_bukrs
customer = l_kunnr
keydate = l_fiyrt
IMPORTING
return = w_return
TABLES
lineitems = t_lineitems.
DELETE t_lineitems WHERE clr_doc_no <> space.
*---Fill GL details-----*
CLEAR: w_accountgl,w_doc_header,w_accountpayable,
w_currencyamount, l_dmbtrp,l_dmbtrn,w_return,w_errormsg.
REFRESH: t_accountgl[],t_accountpayable[],t_currencyamount[],
t_extension1[],t_return[].
w_accountgl-itemno_acc = '0000000001'.
IF i_bukrs = '5000'.
w_accountgl-gl_account = '0000111200'.
ELSE.
w_accountgl-gl_account = '0000113100'.
ENDIF.
w_accountgl-item_text = 'Test'.
w_accountgl-comp_code = i_bukrs.
w_accountgl-value_date = l_postingdate.
w_accountgl-pstng_date = l_postingdate.
w_accountgl-doc_type = 'SA'.
w_accountgl-fis_period = l_postingdate+4(2).
w_accountgl-fisc_year = l_fiyrt.
APPEND w_accountgl TO t_accountgl.
*---Fill Header details-----*
w_doc_header-bus_act = 'RFBU'.
w_doc_header-obj_type = 'BKPFF'.
w_doc_header-obj_key = '$'.
w_doc_header-obj_sys = 'T90CLNT090'.
w_doc_header-username = sy-uname.
w_doc_header-header_txt = 'Customer Openitem clearance'.
w_doc_header-comp_code = i_bukrs.
w_doc_header-doc_date = l_postingdate.
w_doc_header-pstng_date = l_postingdate.
w_doc_header-doc_type = 'SA'.
*---Fill Vendor details---------*
w_accountreceivable-itemno_acc = '0000000002'.
w_accountreceivable-customer = l_kunnr.
w_accountreceivable-comp_code = i_bukrs.
APPEND w_accountpayable TO t_accountpayable.
*------Segregating Positive and Negative Amounts---------------------*
SORT t_lineitems[] ASCENDING BY amt_doccur.
CLEAR: w_lineitems.
LOOP AT t_lineitems INTO w_lineitems.
IF w_lineitems-db_cr_ind = 'S'.
l_dmbtrp = w_lineitems-lc_amount + l_dmbtrp.
ELSE.
l_dmbtrn = w_lineitems-lc_amount + l_dmbtrn.
ENDIF.
ENDLOOP.
l_dmbtrn = -1 * l_dmbtrn.
l_result = l_dmbtrp + l_dmbtrn.
*------Segregating Positive and Negative Amounts--------------------*
*------Fill Currency Amount and posting key if amount is Positive---*
IF l_dmbtrp IS NOT INITIAL.
w_currencyamount-itemno_acc = '0000000001'.
w_currencyamount-currency_iso = 'EUR'.
w_currencyamount-amt_doccur = l_dmbtrp.
APPEND w_currencyamount TO t_currencyamount.
l_dmbtrp = -1 * l_dmbtrp.
w_currencyamount-itemno_acc = '0000000002'.
w_currencyamount-currency_iso = 'EUR'.
w_currencyamount-amt_doccur = l_dmbtrp.
APPEND w_currencyamount TO t_currencyamount.
w_extension1-field1 = '000000000140'.
APPEND w_extension1 TO t_extension1.
w_extension1-field1 = '000000000237'.
APPEND w_extension1 TO t_extension1.
ENDIF.
*------Fill Currency Amount and posting key if amount is Positive----*
*------Fill Currency Amount and posting key if amount is negative------*
IF l_dmbtrn IS NOT INITIAL.
IF l_dmbtrp IS INITIAL.
w_currencyamount-itemno_acc = '0000000001'.
w_extension1-field1 = '000000000150'.
ELSE.
w_currencyamount-itemno_acc = '0000000003'.
w_extension1-field1 = '000000000350'.
ENDIF.
w_currencyamount-currency_iso = 'EUR'.
w_currencyamount-amt_doccur = l_dmbtrn.
APPEND w_currencyamount TO t_currencyamount.
APPEND w_extension1 TO t_extension1.
l_dmbtrn = -1 * l_dmbtrn.
IF l_dmbtrp IS INITIAL.
w_currencyamount-itemno_acc = '0000000002'.
w_extension1-field1 = '000000000227'. "posting key
ELSE.
w_currencyamount-itemno_acc = '0000000004'.
w_extension1-field1 = '000000000427'. "posting key
ENDIF.
w_currencyamount-currency_iso = 'EUR'.
w_currencyamount-amt_doccur = l_dmbtrn.
APPEND w_currencyamount TO t_currencyamount.
APPEND w_extension1 TO t_extension1.
ENDIF.
*------Fill Currency Amount and posting key if amount is negative----*
CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK'
EXPORTING
documentheader = w_doc_header
TABLES
accountgl = t_accountgl
accountreceivable = t_accountreceivable
currencyamount = t_currencyamount
extension1 = t_extension1
return = t_return.
REFRESH: t_return[].
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader = w_doc_header
IMPORTING
obj_type = lv_type
obj_key = lv_key
obj_sys = lv_sys
TABLES
accountgl = t_accountgl
accountreceivable = t_accountreceivable
currencyamount = t_currencyamount
extension1 = t_extension1
return = t_return.
READ TABLE t_return INTO w_return WITH KEY type = 'E'.
IF sy-subrc <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
*------Clear Customer Open lineitems--------*
l_belnr = lv_key+0(10).
CLEAR: l_message.
WAIT UP TO 1 SECONDS.
CALL FUNCTION 'ZFM_CLEAR_DOCUMENTS_KUNNR'
EXPORTING
i_kunnr = l_kunnr
i_belnr = l_belnr
i_keydate = l_postingdate
i_fisyr = i_fiyrt
i_bukrs = i_bukrs
IMPORTING
e_message = l_message
e_recordcount = e_recordcount
TABLES
et_zdt_vend_det = t_zdt_cust_det
it_lineitems = t_lineitems.
*------Clear Customer Open lineitems--------*
ELSE.
LOOP AT t_return INTO w_return.
l_count = l_count + 1.
w_errormsg-sno = l_count.
w_errormsg-kunnr = l_kunnr.
w_errormsg-msg = w_return-message.
APPEND w_errormsg TO et_errormsg.
CLEAR:w_errormsg,w_return,l_belnr,l_kunnr.
ENDLOOP.
ENDIF.
APPEND LINES OF t_zdt_cust_det[] TO et_zdt_cust_det[].
CLEAR: w_lineitems.
ENDLOOP.
ENDFUNCTION.
*-------------------------------------------------------------------------------------------------------------------------------*
FUNCTION zfm_clear_documents_kunnr.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(I_KUNNR) TYPE LIFNR
*" VALUE(I_BELNR) TYPE BELNR_D
*" VALUE(I_KEYDATE) TYPE BAPI3007-KEY_DATE DEFAULT '20141231'
*" VALUE(I_FISYR) TYPE CHAR4
*" VALUE(I_BUKRS) TYPE BUKRS
*" EXPORTING
*" VALUE(E_MESSAGE) TYPE CHAR20
*" VALUE(E_RECORDCOUNT) TYPE INT4
*" TABLES
*" ET_ZDT_CUST_DET STRUCTURE ZDT_VEND_DET OPTIONAL
*" IT_LINEITEMS STRUCTURE BAPI3007_2 OPTIONAL
*"----------------------------------------------------------------------
TYPES: BEGIN OF ty_belnr,
sign(1),
option(2),
low TYPE augbl,
high TYPE augbl,
END OF ty_belnr.
DATA: t_lineitems TYPE TABLE OF bapi3007_2,
w_lineitems TYPE bapi3007_2,
t_belnr TYPE TABLE OF ty_belnr,
w_belnr TYPE ty_belnr,
t_bsad TYPE TABLE OF bsad,
w_bsid TYPE bsid,
w_bsad TYPE bsad,
t_zdt_cust_det TYPE TABLE OF zdt_cust_det,
w_zdt_cust_det TYPE zdt_cust_det,
w_return TYPE bapireturn,
l_kunnr TYPE kunnr VALUE '474',
t_ausz1 TYPE TABLE OF ausz1,
w_ausz1 TYPE ausz1,
t_ausz2 TYPE TABLE OF ausz2,
w_ausz2 TYPE ausz2,
l_belnr TYPE belnr_d,
l_count TYPE int4,
l_fisyr TYPE char4,
l_sno TYPE int4,
l_postingdate TYPE budat,
t_bsik TYPE TABLE OF bsik.
CLEAR: w_ausz1,w_ausz2,w_lineitems,l_count,l_kunnr,
l_postingdate,w_lineitems,l_belnr,l_fisyr,
e_recordcount.
REFRESH: t_lineitems[],t_ausz1,t_ausz2.
l_kunnr = i_kunnr.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = l_kunnr
IMPORTING
output = l_kunnr.
l_belnr = i_belnr.
l_fisyr = i_fisyr.
l_postingdate = i_keydate.
CALL FUNCTION 'BAPI_AP_ACC_GETOPENITEMS'
EXPORTING
companycode = i_bukrs
customer = l_kunnr
keydate = l_postingdate
IMPORTING
return = w_return
TABLES
lineitems = t_lineitems.
SORT t_lineitems BY doc_no ASCENDING.
DELETE t_lineitems WHERE clr_doc_no <> space.
* delete ADJACENT DUPLICATES FROM t_lineitems COMPARING doc_no.
SELECT SINGLE * FROM bsid INTO w_bsid WHERE bukrs = i_bukrs AND
kunnr = l_kunnr AND
belnr = l_belnr.
IF sy-subrc = 0.
SORT t_lineitems[] BY pstng_date ASCENDING item_num ASCENDING.
DESCRIBE TABLE t_lineitems LINES l_count.
LOOP AT t_lineitems INTO w_lineitems.
e_recordcount = e_recordcount + 1.
w_ausz1-belnr = w_lineitems-doc_no.
w_ausz1-bukrs = w_lineitems-comp_code.
w_ausz1-gjahr = w_lineitems-fisc_year.
w_ausz1-buzei = w_lineitems-item_num.
APPEND w_ausz1 TO t_ausz1.
w_ausz2-bukrs = w_bsid-bukrs.
w_ausz2-aktio = 'A'.
w_ausz2-augbl = w_bsid-belnr.
w_ausz2-augdt = l_postingdate.
w_ausz2-auggj = w_bsid-gjahr.
APPEND w_ausz2 TO t_ausz2.
CALL FUNCTION 'CLEAR_DOCUMENTS' " IN UPDATE TASK
TABLES
t_ausz1 = t_ausz1
t_ausz2 = t_ausz2.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
w_belnr-sign = 'I'.
w_belnr-option = 'EQ'.
w_belnr-low = w_lineitems-doc_no.
APPEND w_belnr TO t_belnr.
CLEAR: w_lineitems,w_belnr.
REFRESH: t_ausz2[],t_ausz1[].
ENDLOOP.
SELECT * FROM bsad INTO TABLE t_bsad WHERE bukrs = i_bukrs AND
kunnr = l_kunnr AND
belnr IN t_belnr.
IF t_bsad[] IS NOT INITIAL.
CLEAR: w_lineitems ,w_bsad,w_zdt_cust_det,l_sno.
LOOP AT t_lineitems INTO w_lineitems.
READ TABLE t_bsad INTO w_bsad WITH KEY bukrs = w_lineitems-comp_code
kunnr = w_lineitems-customer
belnr = w_lineitems-doc_no.
CALL FUNCTION 'GUID_CREATE'
IMPORTING
ev_guid_16 = w_zdt_cust_det-z_sno.
IF sy-subrc = 0.
w_zdt_cust_det-z_bukrs = w_bsad-bukrs.
w_zdt_cust_det-z_kunnr = w_bsad-kunnr.
w_zdt_cust_det-z_belnr = w_bsad-belnr.
w_zdt_cust_det-z_status = 'X'.
w_zdt_cust_det-z_clearingdoc = l_belnr.
w_zdt_cust_det-z_desc = 'Cleared'.
APPEND w_zdt_cust_det TO t_zdt_cust_det.
ELSE.
w_zdt_cust_det-z_bukrs = w_lineitems-comp_code.
w_zdt_cust_det-z_kunnr = w_lineitems-customer.
w_zdt_cust_det-z_belnr = w_lineitems-doc_no.
w_zdt_cust_det-z_status = ' '.
w_zdt_cust_det-z_clearingdoc = ' '.
w_zdt_cust_det-z_desc = 'Not cleared'.
APPEND w_zdt_cust_det TO t_zdt_cust_det.
ENDIF.
CLEAR: w_lineitems ,w_bsad,w_zdt_cust_det.
ENDLOOP.
*-----Update Log Table------------*
INSERT zdt_cust_det FROM TABLE t_zdt_cust_det.
IF sy-subrc = 0.
e_message = 'update sucess'.
ELSE.
e_message = 'update fail'.
ENDIF.
et_zdt_cust_det[] = t_zdt_cust_det[].
REFRESH:t_zdt_cust_det[].
ENDIF.
ENDIF.
ENDFUNCTION.