Automatic Vendor Debit Posting Please note that there are some custom fields added in the VBAP table, custom message class and custom include program used in this program. Unfortunately, the contributor couldn't provide us with the functional/technical specifications for this program.
REPORT ZAUTO_DEBIT_VENDOR NO STANDARD PAGE HEADING LINE-SIZE 250 LINE-COUNT 65(2) MESSAGE-ID ZSCM. ************************************************************************ * Program name : ZAUTO_DEBIT_VENDOR * Type : Report * Description : Automatic Vendor Dedit Postings ************************************************************************ *---------------------------------------------------------------------* * TABLES *---------------------------------------------------------------------* TABLES: VBAK, ADR6, TRDYSE01CM. *---------------------------------------------------------------------* * INCLUDE *---------------------------------------------------------------------* INCLUDE ZZUTI_STD_HEADER_FOOTER. "Standard header and footer *----------------------------------------------------------------------* * TYPES DECLARATION *----------------------------------------------------------------------* TYPES: BEGIN OF T_CRMEMO, VBELN TYPE VBAK-VBELN, POSNR TYPE VBAP-POSNR, ERDAT TYPE VBAK-ERDAT, AUART TYPE VBAK-AUART, AUGRU TYPE VBAK-AUGRU, WAERK TYPE VBAK-WAERK, VKORG TYPE VBAK-VKORG, VTWEG TYPE VBAK-VTWEG, SPART TYPE VBAK-SPART, VDATU TYPE VBAK-VDATU, BSTNK TYPE VBAK-BSTNK, BSARK TYPE VBAK-BSARK, BSTDK TYPE VBAK-BSTDK, AEDAT TYPE VBAK-AEDAT, BUKRS_VF TYPE VBAK-BUKRS_VF, MATNR TYPE VBAP-MATNR, ZMENG TYPE VBAP-ZMENG, ZIEME TYPE VBAP-ZIEME, END OF T_CRMEMO, BEGIN OF T_CRMEMO_CHANGE, VBELN(90) TYPE C,
POSNR TYPE VBAP-POSNR, ERDAT TYPE VBAK-ERDAT, AUART TYPE VBAK-AUART, AUGRU TYPE VBAK-AUGRU, WAERK TYPE VBAK-WAERK, VKORG TYPE VBAK-VKORG, VTWEG TYPE VBAK-VTWEG, SPART TYPE VBAK-SPART, VDATU TYPE VBAK-VDATU, BSTNK TYPE VBAK-BSTNK, BSARK TYPE VBAK-BSARK, BSTDK TYPE VBAK-BSTDK, AEDAT TYPE VBAK-AEDAT, BUKRS_VF TYPE VBAK-BUKRS_VF, MATNR TYPE VBAP-MATNR, ZMENG TYPE VBAP-ZMENG, ZIEME TYPE VBAP-ZIEME, END OF T_CRMEMO_CHANGE, BEGIN OF T_MVKE, MATNR TYPE MVKE-MATNR, VKORG TYPE MVKE-VKORG, VTWEG TYPE MVKE-VTWEG, MTPOS TYPE MVKE-MTPOS, END OF T_MVKE, BEGIN OF T_ORDERS, VBELV TYPE VBFA-VBELV, POSNV TYPE VBFA-POSNV, VBELN TYPE VBFA-VBELN, POSNN TYPE VBFA-POSNN, VBTYP_N TYPE VBFA-VBTYP_N, END OF T_ORDERS, BEGIN OF T_EKPO, EBELN TYPE EKKO-EBELN, EBELP TYPE EKPO-EBELP, WAERS TYPE EKKO-WAERS, MENGE TYPE EKPO-MENGE, MEINS TYPE EKPO-MEINS, NETPR TYPE EKPO-NETPR, PEINH TYPE EKPO-PEINH, mwskz TYPE EKPO-mwskz, TXJCD TYPE EKPO-TXJCD, END OF T_EKPO, BEGIN OF T_EKBE, EBELN TYPE EKBE-EBELN, EBELP TYPE EKBE-EBELP, ZEKKN TYPE EKBE-ZEKKN, VGABE TYPE EKBE-VGABE, GJAHR TYPE EKBE-GJAHR, BELNR TYPE EKBE-BELNR, BUZEI TYPE EKBE-BUZEI, END OF T_EKBE, BEGIN OF T_RBKP,
POSNR TYPE VBAP-POSNR, ERDAT TYPE VBAK-ERDAT, AUART TYPE VBAK-AUART, AUGRU TYPE VBAK-AUGRU, WAERK TYPE VBAK-WAERK, VKORG TYPE VBAK-VKORG, VTWEG TYPE VBAK-VTWEG, SPART TYPE VBAK-SPART, VDATU TYPE VBAK-VDATU, BSTNK TYPE VBAK-BSTNK, BSARK TYPE VBAK-BSARK, BSTDK TYPE VBAK-BSTDK, AEDAT TYPE VBAK-AEDAT, BUKRS_VF TYPE VBAK-BUKRS_VF, MATNR TYPE VBAP-MATNR, ZMENG TYPE VBAP-ZMENG, ZIEME TYPE VBAP-ZIEME, END OF T_CRMEMO_CHANGE, BEGIN OF T_MVKE, MATNR TYPE MVKE-MATNR, VKORG TYPE MVKE-VKORG, VTWEG TYPE MVKE-VTWEG, MTPOS TYPE MVKE-MTPOS, END OF T_MVKE, BEGIN OF T_ORDERS, VBELV TYPE VBFA-VBELV, POSNV TYPE VBFA-POSNV, VBELN TYPE VBFA-VBELN, POSNN TYPE VBFA-POSNN, VBTYP_N TYPE VBFA-VBTYP_N, END OF T_ORDERS, BEGIN OF T_EKPO, EBELN TYPE EKKO-EBELN, EBELP TYPE EKPO-EBELP, WAERS TYPE EKKO-WAERS, MENGE TYPE EKPO-MENGE, MEINS TYPE EKPO-MEINS, NETPR TYPE EKPO-NETPR, PEINH TYPE EKPO-PEINH, mwskz TYPE EKPO-mwskz, TXJCD TYPE EKPO-TXJCD, END OF T_EKPO, BEGIN OF T_EKBE, EBELN TYPE EKBE-EBELN, EBELP TYPE EKBE-EBELP, ZEKKN TYPE EKBE-ZEKKN, VGABE TYPE EKBE-VGABE, GJAHR TYPE EKBE-GJAHR, BELNR TYPE EKBE-BELNR, BUZEI TYPE EKBE-BUZEI, END OF T_EKBE, BEGIN OF T_RBKP,
BELNR TYPE GJAHR TYPE BLDAT TYPE BUDAT TYPE XBLNR TYPE LIFNR TYPE RMWWR TYPE WMWST1 TYPE XRECH TYPE STBLG TYPE END OF T_RBKP,
RBKP-BELNR, RBKP-GJAHR, RBKP-BLDAT, RBKP-BUDAT, RBKP-XBLNR, RBKP-LIFNR, RBKP-RMWWR, RBKP-WMWST1, RBKP-XRECH, RBKP-STBLG,
BEGIN OF T_CDHDR, OBJECTCLAS TYPE OBJECTID TYPE CHANGENR TYPE UDATE TYPE UTIME TYPE END OF T_CDHDR,
CDHDR-OBJECTCLAS, CDHDR-OBJECTID, CDHDR-CHANGENR, CDHDR-UDATE, CDHDR-UTIME,
BEGIN OF T_CDPOS, OBJECTCLAS TYPE OBJECTID TYPE CHANGENR TYPE TABNAME TYPE TABKEY TYPE FNAME TYPE CHNGIND TYPE VALUE_NEW TYPE VALUE_OLD TYPE END OF T_CDPOS,
CDHDR-OBJECTCLAS, CDHDR-OBJECTID, CDHDR-CHANGENR, CDPOS-TABNAME, CDPOS-TABKEY, CDPOS-FNAME, CDPOS-CHNGIND, CDPOS-VALUE_NEW, CDPOS-VALUE_OLD,
BEGIN OF T_CHANGE_DOCS, OBJECTCLAS TYPE CDHDR-OBJECTCLAS, OBJECTID TYPE CDHDR-OBJECTID, CHANGENR TYPE CDHDR-CHANGENR, UDATE TYPE CDHDR-UDATE, UTIME TYPE CDHDR-UTIME, TABNAME TYPE CDPOS-TABNAME, TABKEY TYPE CDPOS-TABKEY, FNAME TYPE CDPOS-FNAME, CHNGIND TYPE CDPOS-CHNGIND, VALUE_NEW TYPE CDPOS-VALUE_NEW, VALUE_OLD TYPE CDPOS-VALUE_OLD, END OF T_CHANGE_DOCS, BEGIN OF T_SUCCESS, crmemo TYPE VBAK-vbeln, cmemo TYPE VBAK-vbeln, orders TYPE VBAK-vbeln, po TYPE VBAK-vbeln, miro_no TYPE VBAK-vbeln, text(20) TYPE C, END OF T_SUCCESS, BEGIN OF T_ERROR, crmemo TYPE VBAK-vbeln,
cmemo TYPE orders TYPE po TYPE miro_no TYPE text(100) TYPE END OF T_ERROR.
VBAK-vbeln, VBAK-vbeln, VBAK-vbeln, VBAK-vbeln, C,
*----------------------------------------------------------------------* * WORKAREA DECLARATION *----------------------------------------------------------------------* DATA: WA_CRMEMO TYPE T_CRMEMO, WA_CRMEMO_CHANGE TYPE T_CRMEMO_CHANGE, WA_MVKE TYPE T_MVKE, WA_ORDERS TYPE T_ORDERS, WA_CMEMO TYPE T_ORDERS, WA_PO TYPE T_ORDERS, WA_EKPO TYPE T_EKPO, WA_EKBE TYPE T_EKBE, WA_EKBE_TEMP TYPE T_EKBE, WA_RBKP TYPE T_RBKP, WA_RBKP_TMP TYPE T_RBKP, WA_CDHDR TYPE T_CDHDR, WA_CDPOS TYPE T_CDPOS, WA_CHANGE_DOCS TYPE T_CHANGE_DOCS, WA_CRMEMO_MR8M TYPE T_CRMEMO, WA_SUCCESS TYPE T_SUCCESS, WA_ERROR TYPE T_ERROR, WA_SUCCESS_MR8M TYPE T_SUCCESS, WA_ERROR_MR8M TYPE T_ERROR. *--Work area for all the internal tables used DATA : WA_OBJPACK TYPE SOPCKLSTI1, WA_OBJHEAD TYPE SOLISTI1, WA_OBJBIN TYPE SOLISTI1, WA_OBJTXT TYPE SOLISTI1, WA_RECLIST TYPE SOMLRECI1 .
for Sending
Mail
DATA: *-- Structure to hold Invoice Header Data x_docheader LIKE bapi_incinv_create_header, *-- Structure to hold Reversal Posting Data x_rev_post LIKE BAPI_INCINV_FLD. *----------------------------------------------------------------------* * INTERNAL TAABLE DECLARATION *----------------------------------------------------------------------* DATA: IT_CRMEMO TYPE STANDARD TABLE OF T_CRMEMO WITH HEADER LINE, IT_CRMEMO_CHANGE TYPE STANDARD TABLE OF T_CRMEMO_CHANGE WITH HEADER LINE, IT_MVKE TYPE STANDARD TABLE OF T_MVKE WITH HEADER LINE, IT_CRMEMO_MIRO TYPE STANDARD TABLE OF T_CRMEMO WITH HEADER LINE, IT_CRMEMO_MR8M TYPE STANDARD TABLE OF T_CRMEMO WITH HEADER LINE, IT_ORDERS TYPE STANDARD TABLE OF T_ORDERS WITH HEADER LINE, IT_CMEMO TYPE STANDARD TABLE OF T_ORDERS WITH HEADER LINE, IT_PO TYPE STANDARD TABLE OF T_ORDERS WITH HEADER LINE,
IT_EKPO TYPE STANDARD TABLE OF T_EKPO WITH HEADER LINE, IT_EKBE TYPE STANDARD TABLE OF T_EKBE WITH HEADER LINE, IT_EKBE_TEMP TYPE STANDARD TABLE OF T_EKBE WITH HEADER LINE, IT_RBKP TYPE STANDARD TABLE OF T_RBKP WITH HEADER LINE, IT_CDHDR TYPE STANDARD TABLE OF T_CDHDR WITH HEADER LINE, IT_CDPOS TYPE STANDARD TABLE OF T_CDPOS WITH HEADER LINE, IT_CHANGE_DOCS TYPE STANDARD TABLE OF T_CHANGE_DOCS WITH HEADER LINE, IT_SUCCESS TYPE STANDARD TABLE OF T_SUCCESS WITH HEADER LINE, IT_ERROR TYPE STANDARD TABLE OF T_ERROR WITH HEADER LINE, IT_SUCCESS_MR8M TYPE STANDARD TABLE OF T_SUCCESS WITH HEADER LINE, IT_ERROR_MR8M TYPE STANDARD TABLE OF T_ERROR WITH HEADER LINE.
*-- Internal table to hold Invoice Item Data DATA:BEGIN OF it_itemdata OCCURS 0. INCLUDE STRUCTURE bapi_incinv_create_item. DATA:END OF it_itemdata. *-- Internal table to hold Invoice Acct Assignment Data DATA:BEGIN OF it_ACCOUNTingdata OCCURS 0. INCLUDE STRUCTURE BAPI_INCINV_CREATE_ACCOUNT. DATA:END OF it_accountingdata. *-- Internal table to hold BAPI return data DATA: BEGIN OF it_return OCCURS 0. INCLUDE STRUCTURE bapiret2. "Return Parameter. DATA: END OF it_return. *-- Internal table to hold BAPI return data DATA: BEGIN OF it_return_mr8m OCCURS 0. INCLUDE STRUCTURE bapiret2. "Return Parameter. DATA: END OF it_return_mr8m. *--MAIL related Internal tables DATA: X_DOC_CHNG TYPE SODOCCHGI1, " document attributes IT_OBJPACK TYPE STANDARD TABLE OF SOPCKLSTI1, " attachment table IT_OBJHEAD TYPE STANDARD TABLE OF SOLISTI1, " object header table IT_OBJBIN TYPE STANDARD TABLE OF SOLISTI1, " binary table IT_OBJTXT TYPE STANDARD TABLE OF SOLISTI1, " object text IT_RECLIST TYPE STANDARD TABLE OF SOMLRECI1, " mail recipients it_compressed_list TYPE STANDARD TABLE OF SOLI, it_pdf_list TYPE STANDARD TABLE OF TLINE, it_pdf TYPE tline OCCURS 10 WITH HEADER LINE, it_html TYPE solisti1 OCCURS 10 WITH HEADER LINE, *-- Internal table for MIME data (in CHAR format) it_temp TYPE bapiqcmime OCCURS 10 WITH HEADER LINE, *-- Internal table for storing data it_mime(255) TYPE c OCCURS 10 WITH HEADER LINE. *----------------------------------------------------------------------* * GLOBAL VARIABLES * *----------------------------------------------------------------------* DATA: V_PGM_ID TYPE SY-REPID, " Program id v_zterm TYPE lfb1-zterm, " Payment Terms v_text(75) TYPE c, " To store error or success text
v_itemno(6) for BAPI v_docnr number v_year1 v_spono v_type V_CRMEMO_QTY V_LINE_AMOUNT V_GROSSAMT Qty. v_tax_per(3) amount % v_tax_amt amount v_flag(1) fruther v_tot_s_mr8m Records v_tot_e_mr8m Records V_DATE V_DATE2(10) V_TIME(10) V_TIME2(10) V_INFO(100) V_TABLE_LINES Count V_TEXT2 v_lines v_temp(500) char format v_offset char format v_lineslen char format v_mimelen char format v_tabix char format
TYPE n,
" To Increment Item No.
LIKE bapi_incinv_fld-inv_doc_ no, LIKE bapi_incinv_fld-fisc_year , type TSP01-RQIDENT, type SOODK-OBJTP, TYPE VBAP-ZMENG, TYPE EKPO-NETPR, TYPE EKPO-NETPR,
" " " " "
" For MIRO document For For For For For "
type n,
MIRO Fiscal year Spool No. Spool Type CR Memo Qty. CR Memo Item Qty. For CR Memo Grand " For CR Memo Tax
TYPE EKPO-NETPR,
" For CR Memo Tax
type c,
" Flag to process
TYPE i,
" For MR8M Success
TYPE i,
" For MR8M Error
TYPE TYPE TYPE TYPE TYPE TYPE
SY-DATUM, C, C, C, C, SY-TABIX,
TYPE STRING TYPE i, TYPE c,
,
" " " " "
For For For For For "
Sel. Screen Date EMail Date EMail Time EMail Time EMail Body Text To Increment Email
" For EMail Id text " To Store PDF Lines " To Convert to 255
TYPE p,
" To Convert to 255
TYPE i,
" To Convert to 255
TYPE i,
" To Convert to 255
type sy-tabix.
" To Convert to 255
*----------------------------------------------------------------------* * CONSTANTS * *----------------------------------------------------------------------* CONSTANTS: C_X(1) TYPE C VALUE 'X', " Constant X or Flag C_E(1) TYPE C VALUE 'E', " For Error check c_v(1) TYPE C VALUE 'V', " For PO Docs c_o(1) TYPE C VALUE 'O', " For Order Docs c_c(1) TYPE C VALUE 'C', " For Credit Memo Docs c_k(1) TYPE C VALUE 'K', " For CR Memo Docs c_u(1) TYPE C VALUE 'U', " For Change tables Update c_ali(3) TYPE C VALUE 'ALI', " For ALI Format c_vbak(4) TYPE C VALUE 'VBAK', " For Change docs table C_01(2) TYPE C VALUE '01', " For Serial No. & Reversal Reason
C_02(2) c_blart C_MTPOS C_AUGRU(5) C_0000500205(10) C_VERKBELEG(9)
TYPE TYPE TYPE TYPE TYPE TYPE
C bkpf-blart MVKE-MTPOS C C C
VALUE VALUE VALUE VALUE VALUE VALUE
'02', " For Reversal Reason 'RE', " Document type 'BANS', " Item Category group 'AUGRU', " Reason Code '0000500205', " GL Account # 'VERKBELEG'. " For Object Class
*-- Horizontal tab & line feed constants CLASS CL_ABAP_CHAR_UTILITIES DEFINITION LOAD. CONSTANTS: C_TAB TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>HORI ZONTAL_TAB, C_CR_LF TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>CR_LF . *----------------------------------------------------------------------* * SELECTION SCREEN *----------------------------------------------------------------------* *-- Select Optons SELECTION-SCREEN: BEGIN OF BLOCK B01 WITH FRAME TITLE TEXT-001. SELECT-OPTIONS: S_AUART FOR VBAK-AUART OBLIGATORY NO INTERVALS, " Sales Document Type S_AUGRU FOR VBAK-AUGRU OBLIGATORY NO INTERVALS, " Order reason S_ERDAT FOR VBAK-ERDAT OBLIGATORY. " Date on which the record was created SELECTION-SCREEN: END OF BLOCK B01. *-- Mail Sending Options SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-004. PARAMETERS:P_EMAIL AS CHECKBOX DEFAULT SPACE USER-COMMAND V_COM . PARAMETERS : P_MODE TYPE SO_ESCAPE default 'U' MODIF ID US1. SELECT-OPTIONS: S_EMAIL FOR ADR6-SMTP_ADDR NO INTERVALS MODIF ID US2. * SELECT-OPTIONS: S_EMAIL FOR TRDYSE01CM-USERNAME * NO INTERVALS MODIF ID US2. SELECTION-SCREEN: BEGIN OF LINE. SELECTION-SCREEN: COMMENT 55(40) TEXT-019 MODIF ID US2. SELECTION-SCREEN: END OF LINE. SELECTION-SCREEN END OF BLOCK B2. *----------------------------------------------------------------------* * WORK AREA for SELECT-OPTIONS *----------------------------------------------------------------------* DATA : X_EMAIL LIKE S_EMAIL. "Work area for EMAIL select-options *----------------------------------------------------------------------* * INITIALIZATION *----------------------------------------------------------------------* INITIALIZATION. *-- Default values for Sales Document Type CLEAR S_AUART[]. S_AUART-SIGN = 'I'. S_AUART-OPTION = 'EQ'. S_AUART-LOW = 'CR'. APPEND S_AUART. CLEAR S_AUART.
*-- Default values for Date on which the record was created V_DATE = ( SY-DATUM - 1 ). CLEAR S_ERDAT[]. S_ERDAT-SIGN = 'I'. S_ERDAT-OPTION = 'EQ'. S_ERDAT-LOW = SY-DATUM. S_ERDAT-HIGH = SY-DATUM. APPEND S_ERDAT. CLEAR S_ERDAT. *----------------------------------------------------------------------* * TOP OF PAGE * *----------------------------------------------------------------------* TOP-OF-PAGE. *-- Displaying standard header. PERFORM std_top_of_page USING sy-title. *----------------------------------------------------------------------* * A T S E L E C T I O N S C R E E N O U T P U T *----------------------------------------------------------------------* *-- For modifying the selection screen layout AT SELECTION-SCREEN OUTPUT. PERFORM F_MODIFY_SCREEN. *----------------------------------------------------------------------* * AT SELECTION SCREEN. * *----------------------------------------------------------------------* AT SELECTION-SCREEN. *-- Subroutine to Validate selection screen fields PERFORM f_validate. *----------------------------------------------------------------------* * START-OF-SELECTION * *----------------------------------------------------------------------* START-OF-SELECTION. *-- Fetch the Credit Memo Requests documents PERFORM F_GET_CREDIT_MEMO_REQUEST. IF NOT IT_CRMEMO[] IS INITIAL. *-- Fetch Item Category Group PERFORM F_GET_MVKE_DOCS. *-- Fetch Sales Order documents, PO Documents and Credit Memos PERFORM F_GET_SO_PO_DOCS. ENDIF. *-- If Credit memo request internal table have an change records then * fetch the change documents from CDHDR and CDPOS IF NOT IT_CRMEMO_CHANGE[] IS INITIAL. *-- Fetch Change Documents for CR Memos from CDHDR and CDPOS tables PERFORM F_GET_CHANGED_DOCS. ENDIF. *-- Fetch Purchasing Document Item Details IF NOT IT_PO[] IS INITIAL. PERFORM F_GET_EKPO.
ENDIF. *-- Fetch MIRO Vendor based on the PO Details IF NOT IT_EKPO[] IS INITIAL. PERFORM F_GET_VENDOR. ENDIF. *----------------------------------------------------------------------* * END-OF-SELECTION * *----------------------------------------------------------------------* if v_flag is initial. *-- Process Vendor Debit Postings PERFORM F_PROCESS_MIRO. *-- Process Reversal Postings of Vendor Automatic Debits using TCODE MR8M PERFORM F_PROCESS_MR8M. *-- Send mail,if user enters Email id's and Email Mode is not initial IF P_EMAIL = C_X AND P_MODE IS NOT INITIAL AND S_EMAIL[] IS NOT INITIAL. *-- Get spool id perform f_spool_id. *-- EMail Logic PERFORM F_MAIL_LOGIC. ENDIF. *-- display the EMail RSCONN01 and Sussess and Error records of MIRO and MR8M perform f_display_report. else. *-- Else exit the process exit. endif. *&---------------------------------------------------------------------* *& Form F_MODIFY_SCREEN *&---------------------------------------------------------------------* * A Form routine to modify the screen *----------------------------------------------------------------------* FORM F_MODIFY_SCREEN . *-- To Modify Mail Sending Options LOOP AT SCREEN. IF P_EMAIL = C_X. IF SCREEN-GROUP1 = 'US1' . SCREEN-INVISIBLE = 0. SCREEN-ACTIVE = 1. SCREEN-INPUT = 0. MODIFY SCREEN. ENDIF. IF SCREEN-GROUP2 = 'US2' . SCREEN-INVISIBLE = 0. SCREEN-ACTIVE = 1. MODIFY SCREEN. ENDIF. ELSEIF P_EMAIL = SPACE.
IF SCREEN-GROUP1 = 'US1' . SCREEN-INVISIBLE = 1. SCREEN-ACTIVE = 0. MODIFY SCREEN. ENDIF. IF SCREEN-GROUP1 = 'US2' . SCREEN-INVISIBLE = 1. SCREEN-ACTIVE = 0. MODIFY SCREEN. ENDIF. ENDIF. ENDLOOP. ENDFORM.
" F_MODIFY_SCREEN
*&---------------------------------------------------------------------* *& Form f_validate *&---------------------------------------------------------------------* * Validation of Selection-Screen Inputs *----------------------------------------------------------------------* FORM f_validate . *-- Validate Sales Document Type if not s_AUART[] is initial. select AUART into s_AUART-low from TVAK up to 1 rows where AUART in s_AUART. endselect. if sy-subrc <> 0. message E000 with 'Invalid Sales Document Type'(e01) . endif. ENDIF. *-- Validate Order reason (reason for the business transaction) if not s_AUGRU[] is initial. select AUGRU into s_AUGRU-low from TVAU up to 1 rows where AUGRU in s_AUGRU. endselect. if sy-subrc <> 0. message E000 with 'Invalid Order reason'(e02) . endif. endif. ENDFORM. " f_validate *&---------------------------------------------------------------------* *& Form F_GET_CREDIT_MEMO_REQUEST *&---------------------------------------------------------------------* * Fetch Credit memo requests from VBAK & VBAP *----------------------------------------------------------------------* FORM F_GET_CREDIT_MEMO_REQUEST . SELECT A~VBELN B~POSNR A~ERDAT
A~AUART A~AUGRU A~WAERK A~VKORG A~VTWEG A~SPART A~VDATU A~BSTNK A~BSARK A~BSTDK A~AEDAT A~BUKRS_VF B~MATNR B~ZMENG B~ZIEME INTO TABLE IT_CRMEMO FROM ( VBAK AS A INNER JOIN VBAP AS B ON A~VBELN = B~VBELN ) WHERE ( A~ERDAT IN S_ERDAT OR A~AEDAT IN S_ERDAT ) AND A~AUART IN S_AUART. IF SY-SUBRC = 0. SORT IT_CRMEMO BY VBELN POSNR. ELSE. MESSAGE I000 WITH 'No Credit Memo Request selected'(002). v_flag = C_X. stop. ENDIF.
ENDFORM.
" F_GET_CREDIT_MEMO_REQUEST
*&---------------------------------------------------------------------* *& Form F_GET_MVKE_DOCS *&---------------------------------------------------------------------* * Fetch Sales Data for Material *----------------------------------------------------------------------* FORM F_GET_MVKE_DOCS . DATA: LV_TABIX TYPE SY-TABIX. *-- Fetch Sales Data for Material SELECT MATNR " Material Number VKORG " Sales Organization VTWEG " Distribution Channel MTPOS " Item category group from material master FROM MVKE " Sales Data for Material INTO TABLE IT_MVKE FOR ALL ENTRIES IN IT_CRMEMO WHERE MATNR = IT_CRMEMO-MATNR AND VKORG = IT_CRMEMO-VKORG AND VTWEG = IT_CRMEMO-VTWEG. IF SY-SUBRC = 0. SORT IT_MVKE BY MATNR VKORG VTWEG MTPOS. ENDIF. *-- Validate Item Request line item
category
group
if
MVKE-MTOPS
<>
'BANS' then
ignore
CM
LOOP AT IT_CRMEMO INTO WA_CRMEMO. LV_TABIX = SY-TABIX. READ TABLE IT_MVKE INTO WA_MVKE WITH KEY MATNR = WA_CRMEMO-MATNR VKORG = WA_CRMEMO-VKORG VTWEG = WA_CRMEMO-VTWEG MTPOS = C_MTPOS BINARY SEARCH. IF SY-SUBRC <> 0. DELETE IT_CRMEMO INDEX LV_TABIX. " Delete CR memo records ELSE. *-- Else check for credit memo request change date is not initial, and * append those records to IT_CRMEMO_CHANGE internal table to retrive * change documents IF NOT WA_CRMEMO-AEDAT IS INITIAL. WA_CRMEMO_CHANGE-VBELN = WA_CRMEMO-VBELN. WA_CRMEMO_CHANGE-POSNR = WA_CRMEMO-POSNR. WA_CRMEMO_CHANGE-ERDAT = WA_CRMEMO-ERDAT. WA_CRMEMO_CHANGE-AUART = WA_CRMEMO-AUART. WA_CRMEMO_CHANGE-AUGRU = WA_CRMEMO-AUGRU. WA_CRMEMO_CHANGE-WAERK = WA_CRMEMO-WAERK. WA_CRMEMO_CHANGE-VKORG = WA_CRMEMO-VKORG. WA_CRMEMO_CHANGE-VTWEG = WA_CRMEMO-VTWEG. WA_CRMEMO_CHANGE-SPART = WA_CRMEMO-SPART. WA_CRMEMO_CHANGE-VDATU = WA_CRMEMO-VDATU. WA_CRMEMO_CHANGE-BSTNK = WA_CRMEMO-BSTNK. WA_CRMEMO_CHANGE-BSARK = WA_CRMEMO-BSARK. WA_CRMEMO_CHANGE-BSTDK = WA_CRMEMO-BSTDK. WA_CRMEMO_CHANGE-AEDAT = WA_CRMEMO-AEDAT. WA_CRMEMO_CHANGE-BUKRS_VF = WA_CRMEMO-BUKRS_VF. WA_CRMEMO_CHANGE-MATNR = WA_CRMEMO-MATNR. WA_CRMEMO_CHANGE-ZMENG = WA_CRMEMO-ZMENG. WA_CRMEMO_CHANGE-ZIEME = WA_CRMEMO-ZIEME. APPEND WA_CRMEMO_CHANGE TO IT_CRMEMO_CHANGE. CLEAR WA_CRMEMO_CHANGE. ENDIF. ENDIF. ENDLOOP. ENDFORM.
" F_GET_MVKE_DOCS
*&---------------------------------------------------------------------* *& Form F_GET_SO_PO_DOCS *&---------------------------------------------------------------------* * Fetch the Sales Order documents, PO documents, & Credit Memo *----------------------------------------------------------------------* FORM F_GET_SO_PO_DOCS. *-- Fetch Preceding sales orders based on the credit memo request SELECT VBELV POSNV VBELN POSNN VBTYP_N FROM VBFA INTO TABLE IT_ORDERS FOR ALL ENTRIES IN IT_CRMEMO WHERE VBELN = IT_CRMEMO-VBELN
AND POSNN = IT_CRMEMO-POSNR AND VBTYP_N = C_K AND VBTYP_V = C_C. IF SY-SUBRC = 0. SORT IT_ORDERS BY VBELV POSNV. *-- Fetch Subsequent PO documents & Credit Memo based on the sales orders SELECT VBELV POSNV VBELN POSNN VBTYP_N FROM VBFA INTO TABLE IT_PO FOR ALL ENTRIES IN IT_ORDERS WHERE VBELV = IT_ORDERS-VBELV AND POSNV = IT_ORDERS-POSNV AND VBTYP_N IN (C_V,C_O) AND VBTYP_V = C_C. IF SY-SUBRC = 0. SORT IT_PO BY VBTYP_N. *-- Copy the IT_PO internal table contents to IT_CMEMO internal table IT_CMEMO[] = IT_PO[]. *-- Delete PO documents from IT_CMEMO internal table DELETE IT_CMEMO WHERE VBTYP_N = C_V. SORT IT_CMEMO BY VBELN POSNN. IF IT_CMEMO[] IS INITIAL. MESSAGE I000 WITH 'No Credit Memo are available for CM Request'(003). * v_flag = c_x. * stop. ENDIF. *-- Delete Credit Memo documents from IT_PO internal table DELETE IT_PO WHERE VBTYP_N = C_O. SORT IT_PO BY VBELN POSNN. ENDIF. ENDIF. ENDFORM.
" F_GET_SO_PO_DOCS
*&---------------------------------------------------------------------* *& Form F_GET_CHANGED_DOCS *&---------------------------------------------------------------------* * Fetch Change Documents for CR Memos from CDHDR and CDPOS tables *----------------------------------------------------------------------* FORM F_GET_CHANGED_DOCS . *-- Fetch Change Documents SELECT OBJECTCLAS OBJECTID CHANGENR UDATE UTIME FROM CDHDR
for CR
Memos from CDHDR
INTO TABLE IT_CDHDR FOR ALL ENTRIES IN IT_CRMEMO_CHANGE WHERE OBJECTCLAS = C_VERKBELEG AND OBJECTID = IT_CRMEMO_CHANGE-VBELN * AND UDATE = IT_CRMEMO_CHANGE-AEDAT . IF SY-SUBRC = 0. SORT IT_CDHDR BY OBJECTCLAS OBJECTID CHANGENR. *-- Fetch Change Documents for CR Memos from CDPOS SELECT OBJECTCLAS OBJECTID CHANGENR TABNAME TABKEY FNAME CHNGIND VALUE_NEW VALUE_OLD FROM CDPOS INTO TABLE IT_CDPOS FOR ALL ENTRIES IN IT_CDHDR WHERE OBJECTCLAS = C_VERKBELEG AND OBJECTID = IT_CDHDR-OBJECTID AND CHANGENR = IT_CDHDR-CHANGENR AND TABNAME = C_VBAK AND FNAME = C_AUGRU AND CHNGIND = C_U. IF SY-SUBRC = 0. SORT IT_CDPOS BY OBJECTCLAS OBJECTID CHANGENR. ENDIF. ENDIF. ENDFORM.
" F_GET_CHANGED_DOCS
*&---------------------------------------------------------------------* *& Form F_GET_EKPO *&---------------------------------------------------------------------* * Fetch Purchasing Document Item Details *----------------------------------------------------------------------* FORM F_GET_EKPO . *-- Fetch Purchasing Document Item Details SELECT A~EBELN B~EBELP A~WAERS B~MENGE B~MEINS B~NETPR B~PEINH B~mwskz B~TXJCD INTO TABLE IT_EKPO FROM ( EKKO AS A INNER JOIN EKPO AS B ON A~EBELN = B~EBELN ) FOR ALL ENTRIES IN IT_PO WHERE A~EBELN = IT_PO-VBELN
AND B~EBELP = IT_PO-POSNN+1(5). IF SY-SUBRC = 0. SORT IT_EKPO BY EBELN EBELP. ENDIF. ENDFORM.
" F_GET_EKPO
*&---------------------------------------------------------------------* *& Form F_GET_VENDOR *&---------------------------------------------------------------------* * Fetch MIRO Vendor details based on the PO Details *----------------------------------------------------------------------* FORM F_GET_VENDOR . DATA: LV_TABIX TYPE SY-TABIX. *-- Fetch History per Purchasing Document records SELECT EBELN EBELP ZEKKN VGABE GJAHR BELNR BUZEI FROM EKBE INTO TABLE IT_EKBE FOR ALL ENTRIES IN IT_EKPO WHERE EBELN = IT_EKPO-EBELN AND EBELP = IT_EKPO-EBELP AND VGABE = '2'. * and STBLG = space. IF SY-SUBRC = 0. SORT IT_EKBE BY BELNR GJAHR.
for all
the PO records
*-- Fetch the vendor details based on table IT_EKBE internal table SELECT BELNR GJAHR BLDAT BUDAT XBLNR LIFNR RMWWR WMWST1 XRECH STBLG FROM RBKP INTO TABLE IT_RBKP FOR ALL ENTRIES IN IT_EKBE WHERE BELNR = IT_EKBE-BELNR AND GJAHR = IT_EKBE-GJAHR. IF SY-SUBRC = 0. SORT IT_RBKP BY BELNR GJAHR. ENDIF. ENDIF. *-- Sort PO History documents based on PO # and PO Item SORT IT_EKBE BY EBELN EBELP.
*-- Copy the PO History documents into IT_EKBE_TEMP internal table IT_EKBE_TEMP[] = IT_EKBE[]. *-- Delete PO History documents from IT_EKBE_TEMP internal table * comparing with IT_RBKP where (STBLG) Reversal document number * is initial LOOP AT IT_EKBE_TEMP INTO WA_EKBE_TEMP. LV_TABIX = SY-TABIX. READ TABLE IT_RBKP INTO WA_RBKP WITH KEY BELNR = WA_EKBE_TEMP-BELNR GJAHR = WA_EKBE_TEMP-GJAHR * XRECH = SPACE STBLG = SPACE. IF SY-SUBRC <> 0. DELETE IT_EKBE_TEMP INDEX LV_TABIX. ENDIF. CLEAR: WA_EKBE_TEMP, WA_RBKP. ENDLOOP. ENDFORM.
" F_GET_VENDOR
*&---------------------------------------------------------------------* *& Form F_PROCESS_MIRO *&---------------------------------------------------------------------* * Process MIRO *----------------------------------------------------------------------* FORM F_PROCESS_MIRO . DATA: lv_itemno TYPE i, lv_tabix type sy-index, lc_check(1) TYPE c VALUE 'X', V_FILL_ITEM, lv_rbkp_bldat type sy-datum.
clear : it_itemdata, it_accountingdata, IT_CRMEMO_MIRO, IT_CRMEMO_MR8M. refresh : it_itemdata, it_accountingdata, IT_CRMEMO_MIRO, IT_CRMEMO_MR8M. *-- Copy all the CR Memo Docs and delete order reason not in S_AUGRU IT_CRMEMO_MR8M[] = IT_CRMEMO[]. *-- Delete Reversal postings which are not in S_AUGRU DELETE IT_CRMEMO_MR8M WHERE AUGRU IN S_AUGRU. *-- Delete Reversal postings where AEDAT is initial DELETE IT_CRMEMO_MR8M where aedat is initial. *-- Delete order reason which are not in S_AUGRU DELETE IT_CRMEMO WHERE AUGRU NOT IN S_AUGRU. *-- Loop through the credit memo request records LOOP AT IT_CRMEMO INTO WA_CRMEMO. lv_tabix = sy-tabix. *-- If credit memo request change date is not initial then * send change date (AEDAT) to lv_rbkp_bldat if not WA_CRMEMO-aedat is initial.
lv_rbkp_bldat = WA_CRMEMO-aedat. else. *-- If credit memo request change date is initial then * send create date (ERDAT) to lv_rbkp_bldat lv_rbkp_bldat = WA_CRMEMO-erdat. endif. READ TABLE IT_ORDERS INTO WA_ORDERS WITH KEY VBELN = WA_CRMEMO-VBELN POSNN = WA_CRMEMO-POSNR. IF SY-SUBRC = 0. READ TABLE IT_PO INTO WA_PO WITH KEY VBELV = WA_ORDERS-VBELV POSNV = WA_ORDERS-POSNV. IF SY-SUBRC = 0. READ TABLE IT_EKPO INTO WA_EKPO WITH KEY EBELN = WA_PO-VBELN EBELP = WA_PO-POSNN. IF SY-SUBRC = 0. *-- Read IT_EKBE_TEMP internal table which contains records which are not reversed READ TABLE IT_EKBE_TEMP INTO WA_EKBE_TEMP WITH KEY EBELN = WA_EKPOEBELN EBELP = WA_EKPOEBELP. IF SY-SUBRC = 0. *-- Read IT_RBKP internal table with XRECH (Indicator: post invoice) = 'X' , * check for invoice records for the correspnding CR Memo records READ TABLE IT_RBKP INTO WA_RBKP WITH KEY BELNR = WA_EKBE_TEMPBELNR GJAHR = WA_EKBE_TEMPGJAHR XRECH = C_X. IF SY-SUBRC = 0. *-- Read IT_RBKP internal table with XRECH (Indicator: post invoice) = SPACE, * and document date = CR Memo creation date * and reference document number = PO number READ TABLE IT_RBKP INTO WA_RBKP_TMP WITH KEY BELNR = WA_EKBE_TEMP-BELNR GJAHR = WA_EKBE_TEMP-GJAHR BLDAT = WA_CRMEMOERDAT XRECH = ' ' XBLNR+0(10) = WA_EKBE_TEMP-ebeln. * STBLG = SPACE. IF SY-SUBRC = 0. CONTINUE. ELSE. *-- Quantity Conversion (Get the Credit Memo Quantity by passing CMR unit per one PO unit) perform f_quantity_conv. *-- Filling the Line items data *-- Incrementing the item no lv_itemno = lv_itemno + 1. v_itemno = lv_itemno.
it_itemdata-invoice_doc_item it_itemdata-po_number it_itemdata-po_item it_itemdata-tax_code it_itemdata-TAXJURCODE
= = = = =
v_itemno. WA_ekPO-ebeln. WA_EKPO-EBELP. WA_ekpo-mwskz. WA_ekpo-TXJCD.
*-- Converting SAP amount to BAPI format by passing line item amount and currency PERFORM f_currency_amount_sap_to_b api USING WA_EKPO-waers V_LINE_AMOUNT CHANGING it_itemdataitem_amount . it_itemdata-quantity
=
(
WA_CRMEMO-ZMENG
*
V_CRMEMO_QTY ). it_itemdata-po_unit APPEND it_itemdata.
= WA_EKPO-MEINS.
*-- Populate Account Assignment data IT_ACCOUNTINGDATA-INVOICE_DOC_ITEM = v_itemno. IT_ACCOUNTINGDATA-XUNPL = space. IT_ACCOUNTINGDATA-SERIAL_NO = C_01. IT_ACCOUNTINGDATA-TAX_CODE = it_itemdata-tax_code. IT_ACCOUNTINGDATA-TAXJURCODE = it_itemdata-TAXJURCODE. IT_ACCOUNTINGDATA-ITEM_AMOUNT = it_itemdata-item_amount. IT_ACCOUNTINGDATA-QUANTITY = it_itemdata-quantity. IT_ACCOUNTINGDATA-PO_UNIT = it_itemdata-po_unit. IT_ACCOUNTINGDATA-GL_ACCOUNT = C_0000500205. append IT_ACCOUNTINGDATA. clear IT_ACCOUNTINGDATA. CLEAR it_itemdata. endif. endif. ENDIF. ENDIF. ENDIF. ENDIF. *-- At end of each credit memo request fill the header details of BAPI FM and *-- post the BAPI AT END OF VBELN. read table it_crmemo into wa_crmemo index lv_tabix. *-- Populate the document Header *-- Getting the header data for invoice x_docheader-invoice_ind = space. x_docheader-doc_type = c_blart. x_docheader-doc_date = lv_rbkp_bldat. x_docheader-pstng_date = lv_rbkp_bldat. x_docheader-ref_doc_no = WA_RBKP-XBLNR. x_docheader-comp_code = WA_CRMEMO-BUKRS_VF. x_docheader-CURRENCY = WA_CRMEMO-WAERK. x_docheader-calc_tax_ind = lc_check. *-- Logic to find the Tax % v_tax_per = ( WA_RBKP-WMWST1 / ( WA_RBKP-RMWWR - WA_RBKP-WMWST1 ) ) * 100.
v_tax_amt = ( V_GROSSAMT * v_tax_per ) / 100. V_GROSSAMT = ( V_GROSSAMT + v_tax_amt ). x_docheader-gross_amount = V_GROSSAMT. CLEAR:v_docnr, v_year1. *-- Calling BAPI to create PO invoice document CHECK NOT IT_ITEMDATA[] IS INITIAL. CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE' EXPORTING headerdata = x_docheader IMPORTING invoicedocnumber = v_docnr fiscalyear = v_year1 TABLES itemdata = it_itemdata[] ACCOUNTingDATA = it_ACCOUNTingDATA[] * GLACCOUNTDATA = * taxdata = = it_return. return *-- Commit work IF NOT v_docnr IS INITIAL. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. *-- Move the data to Success Internal Table wa_success-crmemo = wa_crmemo-vbeln. wa_success-cmemo = wa_cmemo-vbeln. wa_success-orders = wa_orders-vbelv. wa_success-po = wa_po-vbeln. wa_success-miro_no = v_docnr. wa_success-text = 'MIRO is Posted'. append wa_success TO it_success. clear wa_success. ENDIF. Read the values in the table IT_RETURN. CLEAR it_return. READ TABLE it_return INDEX 1. * If message type is error then roll back the work IF it_return-type = C_E. *-- Roll back CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. *--
*-- Move the data to Error Internal Table wa_error-crmemo = wa_crmemo-vbeln. wa_error-cmemo = wa_cmemo-vbeln. wa_error-orders = wa_orders-vbelv. wa_error-po = wa_po-vbeln. wa_error-text = it_return-MESSAGE. append wa_error TO it_error. clear wa_error. ENDIF. refresh: it_return, it_itemdata, it_accountingdata.
clear: V_GROSSAMT, it_return, it_itemdata, wa_crmemo, wa_orders, wa_po, wa_ekpo, WA_EKBE_TEMP, wa_rbkp, wa_rbkp_tmp, v_docnr, v_year1, WA_SUCCESS, WA_ERROR, it_accountingdata, v_tax_per, v_tax_amt, V_FILL_ITEM, lv_rbkp_bldat. ENDAT. ENDLOOP. ENDFORM.
" F_PROCESS_MIRO
*&---------------------------------------------------------------------* *& Form f_quantity_conv *&---------------------------------------------------------------------* * Quantity Conversion *----------------------------------------------------------------------* FORM f_quantity_conv . *-- Quantity Conversion (Get the Credit Memo Quantity by passing CMR unit per one PO unit) CLEAR: V_LINE_AMOUNT, V_CRMEMO_QTY. call function 'MC_UNIT_CONVERSION' EXPORTING MATNR = WA_CRMEMO-MATNR nach_meins = WA_EKPO-MEINS von_meins = WA_CRMEMO-ZIEME IMPORTING UMREF = V_CRMEMO_QTY EXCEPTIONS CONVERSION_NOT_FOUND = 1 MATERIAL_NOT_FOUND = 2 NACH_MEINS_MISSING = 3 OVERFLOW = 4 VON_MEINS_MISSING = 5 OTHERS = 6. if sy-subrc <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ELSE. V_LINE_AMOUNT = ( V_CRMEMO_QTY * WA_CRMEMO-ZMENG WA_EKPO-NETPR ). V_GROSSAMT = V_GROSSAMT + V_LINE_AMOUNT. ENDIF. ENDFORM.
*
WA_EKPO-peinh
" f_quantity_conv
*&---------------------------------------------------------------------* *& Form f_currency_amount_sap_to_b api *&---------------------------------------------------------------------* * SAP Amount to BAPI Amount *----------------------------------------------------------------------* FORM f_currency_amount_sap_to_b api USING p_waers p_kbetr CHANGING p_amt_doccur. DATA : lv_waers LIKE tcurc-waers, lv_sapamount LIKE bapicurr-bapicurr,
*
lv_bapi_amount LIKE bapicurr-bapicurr. lv_waers = p_waers. lv_sapamount = p_kbetr.
CALL FUNCTION 'CURRENCY_AMOUNT_SAP_TO_BAPI' EXPORTING currency = lv_waers sap_amount = lv_sapamount IMPORTING bapi_amount = lv_bapi_amount. p_amt_doccur = lv_bapi_amount. ENDFORM.
" f_currency_amount_sap_to_bapi
*&---------------------------------------------------------------------* *& Form f_display_report *&---------------------------------------------------------------------* * Display Report *----------------------------------------------------------------------* FORM f_display_report . *-- Success Report IF NOT IT_SUCCESS[] IS INITIAL. ULINE AT /1(129). WRITE:/1 sy-vline, 'CR Memo', 19 sy-vline, 'Credit Memo', 39 sy-vline, 'Sales Order', 59 sy-vline, 'Purchase Order', 79 sy-vline, 'MIRO Number', 99 sy-vline, 'Text', 129 sy-vline. ULINE AT /1(129). LOOP AT it_success into wa_success. WRITE:/1 sy-vline, wa_success-crmemo, 19 sy-vline, wa_success-cmemo, 39 sy-vline, wa_success-orders, 59 sy-vline, wa_success-po, 79 sy-vline, wa_success-miro_no, 99 sy-vline, wa_success-text, 129 sy-vline. ENDLOOP. ULINE AT /1(129). SKIP 2. ENDIF. *-- Error Report IF NOT IT_ERROR[] IS INITIAL. ULINE AT /1(179). WRITE:/1 sy-vline, 'CR Memo', 19 sy-vline, 'Credit Memo',
39 sy-vline, 'Sales Order', 59 sy-vline, 'Purchase Order', 79 sy-vline, 'Error Text', 179 sy-vline. ULINE AT /1(179). LOOP AT IT_ERROR into WA_ERROR. WRITE:/1 sy-vline, WA_ERROR-crmemo, 19 sy-vline, WA_ERROR-cmemo, 39 sy-vline, WA_ERROR-orders, 59 sy-vline, WA_ERROR-po, 79 sy-vline, WA_ERROR-text, 179 sy-vline. ENDLOOP. ULINE AT /1(179). ENDIF.
IF NOT IT_SUCCESS_MR8M[] IS INITIAL. ULINE AT /1(129). WRITE:/1 sy-vline, 'CR Memo', 19 sy-vline, 'Credit Memo', 39 sy-vline, 'Sales Order', 59 sy-vline, 'Purchase Order', 79 sy-vline, 'Reversal Posting No.', 99 sy-vline, 'Text', 129 sy-vline. ULINE AT /1(129). LOOP AT it_success_mr8m into wa_success_mr8m. WRITE:/1 sy-vline, wa_success_mr8m-crmemo, 19 sy-vline, wa_success_mr8m-cmemo, 39 sy-vline, wa_success_mr8m-orders, 59 sy-vline, wa_success_mr8m-po, 79 sy-vline, wa_success_mr8m-miro_no, 99 sy-vline, wa_success_mr8m-text, 129 sy-vline. ENDLOOP. ULINE AT /1(129). SKIP 2. ENDIF. *-- Error Report IF NOT IT_ERROR_MR8M[] IS INITIAL. ULINE AT /1(179). WRITE:/1 sy-vline, 'CR Memo', 19 sy-vline, 'Credit Memo', 39 sy-vline, 'Sales Order', 59 sy-vline, 'Purchase Order', 79 sy-vline, 'Error Text', 179 sy-vline. ULINE AT /1(179).
LOOP AT IT_ERROR_MR8M into WA_ERROR_MR8M. WRITE:/1 sy-vline, WA_ERROR_MR8M-crmemo, 19 sy-vline, WA_ERROR_MR8M-cmemo, 39 sy-vline, WA_ERROR_MR8M-orders, 59 sy-vline, WA_ERROR_MR8M-po, 79 sy-vline, WA_ERROR_MR8M-text, 179 sy-vline. ENDLOOP. ULINE AT /1(179). ENDIF. ENDFORM.
" f_display_report
*&---------------------------------------------------------------------* *& Form F_PROCESS_MR8M *&---------------------------------------------------------------------* * Process Reversal postings *----------------------------------------------------------------------* FORM F_PROCESS_MR8M . clear: wa_CRMEMO_MR8M. LOOP AT IT_CRMEMO_MR8M read table it_cdpos VBELN. if sy-subrc <> 0. CONTINUE. ELSE. if wa_cdpos-value_old READ TABLE IT_ORDERS
into WA_CRMEMO_MR8M. into wa_cdpos with key
OBJECTID
=
WA_CRMEMO_MR8M-
in s_augru. INTO WA_ORDERS WITH KEY VBELN = wa_CRMEMO_MR8M-VBELN POSNN = wa_CRMEMO_MR8M-
POSNR. IF SY-SUBRC = 0. * READ TABLE IT_CMEMO INTO WA_CMEMO WITH KEY VBELV = WA_ORDERS-VBELV * POSNV = WA_ORDERS-POSNV. * IF SY-SUBRC = 0. READ TABLE IT_PO INTO WA_PO WITH KEY VBELV = WA_ORDERS-VBELV POSNV = WA_ORDERS-POSNV. IF SY-SUBRC = 0. READ TABLE IT_EKPO INTO WA_EKPO WITH KEY EBELN = WA_PO-VBELN EBELP = WA_PO-POSNN. IF SY-SUBRC = 0. * READ TABLE IT_EKBE INTO WA_EKBE WITH KEY EBELN = WA_EKPO-EBELN * EBELP = WA_EKPO-EBELP. * IF SY-SUBRC = 0. LOOP AT IT_EKBE into WA_EKBE WHERE EBELN = WA_EKPO-EBELN AND EBELP = WA_EKPO-EBELP. READ TABLE IT_RBKP INTO WA_RBKP WITH KEY BELNR = WA_EKBE-BELNR GJAHR = WA_EKBE-GJAHR * BLDAT = WA_CRMEMO_MR8MAEDAT XBLNR+0(10) = WA_EKBEEBELN XRECH = SPACE STBLG = SPACE.
*--
*--
*--
*--
*-*-*--
* *
IF SY-SUBRC = 0. move: WA_RBKP-BELNR TO X_REV_POST-INV_DOC_NO, WA_RBKP-GJAHR TO X_REV_POST-FISC_YEAR, WA_RBKP-BUDAT TO X_REV_POST-PSTNG_DATE. IF WA_RBKP-BUDAT+4(2) = SY-DATUM+4(2). X_REV_POST-REASON_REV = C_01. ELSE. X_REV_POST-REASON_REV = C_02. ENDIF. ENDIF. ENDLOOP. Call the FM for MIRO reversal CALL FUNCTION 'BAPI_INCOMINGINVOICE_CANCEL' EXPORTING INVOICEDOCNUMBER = X_REV_POST-INV_DOC_NO FISCALYEAR = X_REV_POST-FISC_YEAR REASONREVERSAL = X_REV_POST-REASON_REV POSTINGDATE = X_REV_POST-PSTNG_DATE IMPORTING INVOICEDOCNUMBER_REVERSAL = X_REV_POST-INV_DOC_NO FISCALYEAR_REVERSAL = X_REV_POST-FISC_YEAR TABLES RETURN = it_return_mr8m. Commit work IF NOT X_REV_POST-INV_DOC_NO IS INITIAL. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. Move the data to Success Internal Table wa_success_mr8m-crmemo = wa_crmemo-vbeln. wa_success_mr8m-cmemo = wa_cmemo-vbeln. wa_success_mr8m-orders = wa_orders-vbelv. wa_success_mr8m-po = wa_po-vbeln. wa_success_mr8m-text = 'MR8M Done'. append wa_success_mr8m TO it_success_mr8m. clear wa_success_mr8m. ENDIF. Read the values in the table IT_RETURN. CLEAR it_return_mr8m. READ TABLE it_return_mr8m INDEX 1. If message type is error then roll back the work IF it_return_mr8m-type = 'E' . Roll back CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. Move the data to Error Internal Table wa_error_mr8m-crmemo = wa_crmemo-vbeln. wa_error_mr8m-cmemo = wa_cmemo-vbeln. wa_error_mr8m-orders = wa_orders-vbelv. wa_error_mr8m-po = wa_po-vbeln. wa_error_mr8m-text = it_return_mr8m-MESSAGE. append wa_error_mr8m TO it_error_mr8m. clear wa_error_mr8m. ENDIF. ENDIF. ENDIF. ENDIF.
*
ENDIF. ENDIF. ENDIF. ENDIF. ENDIF. CLEAR: wa_error_mr8m, wa_SUCCESS_mr8m, X_REV_POST, wa_CRMEMO_MR8M, wa_orders, wa_po, wa_ekpo, wa_ekbe, wa_rbkp, wa_cmemo. REFRESH: IT_RETURN_MR8M. endloop.
ENDFORM.
" F_PROCESS_MR8M
*&---------------------------------------------------------------------* *& Form F_spool_id *&---------------------------------------------------------------------* * Create a new spool ID *----------------------------------------------------------------------* FORM F_spool_id . *-- Create a new spool from the data in data_tab *-- new-page line-size sy-linsz print on new-page line-size 255 print on destination 'LOCL'(p19) "<<== my printer *-- receiver 'TJOSVOB' cover text 'Spool title'(p20) " <<== Title or Spool req. name list name 'List name'(p21) list dataset 'List Dataset'(p22) immediately ' ' " X means print now keep in spool c_x " X means keep spool new list identification c_x no dialog . *-- Writing Report *-- In this perform you will write the code perform f_display_report.
for displaying
the list
*-- new-page print off new-page print off. ENDFORM.
" F_spool_id
*&---------------------------------------------------------------------* *& Form F_MAIL_LOGIC *&---------------------------------------------------------------------* * Logic to send a mail for the given Email ID's *----------------------------------------------------------------------* FORM F_MAIL_LOGIC . data : wa_tsp01sys type tsp01sys. clear v_spono. v_spono = sy-spono. * v_attach_desc = sy-title. *-- Convert the Spool ID into PDF format perform f_pdf_conversion tables it_compressed_list it_pdf_list
using
v_spono.
*-- Send the Mail in PDF Format perform f_send_mail. ENDFORM.
" F_MAIL_LOGIC
*&---------------------------------------------------------------------* *& Form f_pdf_conversion *&---------------------------------------------------------------------* * Read the spool and convert into PDF format *----------------------------------------------------------------------* * -->P_ITAB_COMPRESSED_LIST text * -->P_ITAB_PDF_LIST text * -->P_V_SPONO text *----------------------------------------------------------------------* FORM f_pdf_conversion TABLES it_compressed_list structure soli it_pdf_list structure tline using lv_spooln type tsp01-rqident. clear: it_compressed_list, it_pdf_list. refresh: it_compressed_list, it_pdf_list. *-- Call the FM "RSPO_RETURN_SPOOLJOB" "RSPO_RETURN_SPOOLJOB" to to read the spool by passing the spool number call function 'RSPO_RETURN_SPOOLJOB' exporting rqident = lv_spooln desired_type = v_type importing real_type = v_type tables buffer = it_compressed_list buffer_pdf = it_pdf_list exceptions no_such_job = 1 job_contains_no_data = 2 selection_empty = 3 no_permission = 4 can_not_access = 5 read_error = 6 type_no_match = 7 others = 8. if sy-subrc <> 0. message I000 with 'Error in spool reading'(017). reading'(017). endif. *-- If the List type is ALI call the FM "CONVERT_ABAPSPOOLJOB_2_PDF" * to convert list into PDF Format v_type = c_ali. " for List it is ALI if call function 'CONVERT_ABAPSPOOLJOB_2_PDF' exporting src_spoolid = lv_spooln tables pdf = it_pdf exceptions err_no_abap_spooljob = 1
err_no_spooljob err_no_permission err_conv_not_possible err_bad_destdevice user_cancelled err_spoolerror err_temseerror err_btcjob_open_failed err_btcjob_submit_failed err_btcjob_close_failed others if sy-subrc <> 0. message e000 with'Error with'Error in convertion endif.
= = = = = = = = = = =
2 3 4 5 6 7 8 9 10 11 12.
of list into PDF format'(018). format'(018).
*-- Logic to convert the given format into 255 character format in IT_PDF describe table it_pdf lines v_lines. describe field it_pdf length v_lineslen IN character MODE. describe field it_temp length v_mimelen IN character MODE. LOOP AT it_pdf. v_tabix = sy-tabix. MOVE it_pdf TO v_temp+v_offset. IF v_tabix = v_lines. v_lineslen = strlen( it_pdf ). ENDIF. v_offset = v_offset + v_lineslen. IF v_offset GE v_mimelen. CLEAR it_temp. it_temp = v_temp(v_mimelen). APPEND it_temp. SHIFT v_temp BY v_mimelen PLACES. v_offset = v_offset - v_mimelen. ENDIF. IF v_tabix = v_lines. IF v_offset GT 0. CLEAR it_temp. it_temp = v_temp(v_offset). APPEND it_temp. ENDIF. ENDIF. ENDLOOP.
LOOP AT it_temp. it_mime(255) = it_temp-line. APPEND it_mime. ENDLOOP. *-- Final Data to send to EMail it_html[] = it_mime[]. endif. ENDFORM.
" f_pdf_conversion
*&---------------------------------------------------------------------* *& Form f_send_mail *&---------------------------------------------------------------------* * Send EMail Logic *----------------------------------------------------------------------* FORM f_send_mail . data : wa_tsp01sys type tsp01sys. *-- Populate Mail Id's Id's WA_RECLIST-REC_TYPE = P_MODE. WA_RECLIST-EXPRESS = C_X. *-- Loop through the Emial select options LOOP AT S_EMAIL INTO X_EMAIL. WA_RECLIST-RECEIVER = X_EMAIL-LOW. *-- append receiver table APPEND WA_RECLIST TO IT_RECLIST. ENDLOOP. *-- If receiver table is not initial CHECK IT_RECLIST[] IS NOT INITIAL. *--To check Mail ID's ID's internal table should not be Empty. *--populate document attributes *-- Mail subject line CLEAR: X_DOC_CHNG. X_DOC_CHNG-OBJ_NAME = 'HEADING' 'HEADING'. . CONCATENATE 'Automatic Vendor Dedit Postings'(005) Postings'(005) SY-DATUM INTO X_DOC_CHNG-OBJ_DESCR SEPARATED BY SPACE. *-- Mail body CONCATENATE 'Automatic Vendor Dedit Postings'(005) Postings'(005) ' is attached.'(006) attached.'(006) INTO WA_OBJTXT SEPARATED BY SPACE. APPEND WA_OBJTXT TO IT_OBJTXT. *-- Append Date and Time into Body of email. MOVE 'File is generated on'(007) on'(007) TO V_INFO. V_TIME = SY-UZEIT. CONCATENATE V_TIME+0(2) V_TIME+2(2) V_TIME+4(2) INTO V_TIME2 SEPARATED BY ':' ':'. . CONCATENATE SY-DATUM+4(2) '/' SY-DATUM+6(2) '/' SY-DATUM+0(4) INTO V_DATE2. CONCATENATE V_INFO V_DATE2 'At' V_TIME2
INTO V_INFO SEPARATED BY SPACE. WA_OBJTXT = V_INFO. APPEND WA_OBJTXT TO IT_OBJTXT. *-- Document size CLEAR : V_TABLE_LINES. DESCRIBE TABLE IT_OBJTXT LINES V_TABLE_LINES. READ TABLE IT_OBJTXT INTO WA_OBJTXT INDEX V_TABLE_LINES . X_DOC_CHNG-DOC_SIZE = ( V_TABLE_LINES - 1 ) * 255 + STRLEN( WA_OBJTXT ). *-- Populate packing list for body text WA_OBJPACK-HEAD_START = 1. WA_OBJPACK-HEAD_NUM = 0. WA_OBJPACK-BODY_START = 1. WA_OBJPACK-BODY_NUM = V_TABLE_LINES. WA_OBJPACK-DOC_TYPE = 'RAW'. APPEND WA_OBJPACK TO IT_OBJPACK. CLEAR WA_OBJPACK. if v_type
= c_ali. loop at it_html. wa_objbin-line = it_html-line. APPEND WA_OBJBIN TO IT_OBJBIN. CLEAR WA_OBJBIN. endloop. v_type = 'PDF'. endif. *-- Get total no.of lines of Object table(attachment) CLEAR : V_TABLE_LINES. DESCRIBE TABLE IT_OBJBIN LINES V_TABLE_LINES. *-- Populate object header(attachment name) WA_OBJHEAD = 'Automatic Vendor Dedit Postings log'(014). APPEND WA_OBJHEAD TO IT_OBJHEAD. CLEAR WA_OBJHEAD. *-- Packing list for attachment WA_OBJPACK-TRANSF_BIN = C_X. WA_OBJPACK-HEAD_START = 1. WA_OBJPACK-HEAD_NUM = 1. WA_OBJPACK-BODY_START = 1. WA_OBJPACK-BODY_NUM = V_TABLE_LINES . WA_OBJPACK-DOC_TYPE = v_type . WA_OBJPACK-OBJ_NAME = 'Auto_Debit'. WA_OBJPACK-OBJ_DESCR = 'attachment'. WA_OBJPACK-DOC_SIZE = V_TABLE_LINES * 255. APPEND WA_OBJPACK TO IT_OBJPACK. CLEAR WA_OBJPACK. *-- Sending the EMail document in given format CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' EXPORTING DOCUMENT_DATA = X_DOC_CHNG
PUT_IN_OUTBOX COMMIT_WORK TABLES PACKING_LIST OBJECT_HEADER CONTENTS_BIN CONTENTS_TXT RECEIVERS EXCEPTIONS TOO_MANY_RECEIVERS DOCUMENT_NOT_SENT DOCUMENT_TYPE_NOT_EXIST OPERATION_NO_AUTHORIZATION PARAMETER_ERROR X_ERROR ENQUEUE_ERROR OTHERS
= C_X = C_X = = = = =
IT_OBJPACK IT_OBJHEAD IT_OBJBIN IT_OBJTXT IT_RECLIST
= = = = = = = =
1 2 3 4 5 6 7 8.
IF SY-SUBRC <> 0. MESSAGE E000 WITH 'Error occurred in sending mail'(015). ELSE. *-- Wait upto 2 secounds wait up to 2 seconds. *-- Call the RSCONN01 (SAPconnect: Start Send Process) SUBMIT RSCONN01 WITH MODE = 'INT' * WITH output = 'X' AND RETURN. PERFORM F_DISPLAY_SENT_MAIL_STATUS. ENDIF. * To delete the spool request wa_tsp01sys-rqident = sy-spono. call function 'RSPO_IDELETE_SPOOLREQ' exporting spoolreq = wa_tsp01sys. if sy-subrc <> 0. endif. ENDFORM.
" f_send_mail
*&---------------------------------------------------------------------* *& Form F_DISPLAY_SENT_MAIL_STATUS *&---------------------------------------------------------------------* * Display Mail Sent status to the user *----------------------------------------------------------------------* FORM F_DISPLAY_SENT_MAIL_STATUS . ULINE AT /1(60). WRITE :/ SY-VLINE, 'Mail sent Successfully to'(016) COLOR 5 INTENSIFIED ON, 60 SY-VLINE. LOOP AT S_EMAIL INTO X_EMAIL. V_TABLE_LINES = STRLEN( X_EMAIL-LOW ).
MOVE X_EMAIL-LOW+0(V_TABLE_LINE S) TO V_TEXT2. WRITE :/ SY-VLINE, 2 SY-TABIX LEFT-JUSTIFIED, 5 ')', V_TEXT2, 60 SY-VLINE. ENDLOOP. ULINE AT /1(60). ENDFORM.
" F_DISPLAY_SENT_MAIL_STATUS
Standard BAPI Change / Function group enhancement Procedure All application function modules (BAPI) can be enhanced by adding parameters to the standard function module interface. These parameters must be ‘optional’ in nature, since adding a mandatory parameter will require all calls to be changed. Additionally any function module that is part of the Central Basis cannot be enhanced You can do the follow ing things for a BAPI (Compat ible enhancement) are: • •
New optional fields in structures, if these are added to the end of a structure New optional parameters
You can do the below modi fication for a BAPI (I ncompatible enhancements) are: • • • • •
Changes to the field length Changes to the field type Inserting a field within a structure Deleting parameters and fields Adding new mandatory fields or mandatory parameters
Example: function module ‘BAPI_SALESORDER_CREATEFROMDAT2” . From the menu, choose ‘Function module -> Enhance interface’ to add optional parameters to a function module.
Once you click the Enhance Interface. You will be finding the screen as below:
Click on create at bottom left. Pass the enhancement implementation name and short text. Once you done creation you will find the enhancement in the above screen. Ex : Sales_order_add_fields Select that enhancement and click enter.
Press Yes. Then you will be finding the screen as shown below. You can add new fields as per the requirement.
After adding the required fields, we need to add the custom code for processing. To add the code into the BAPI / Function module:
Place the cursor on the enhancement spot where we need to add the code and right click and implement the enhancement by selection the create. Following screen would appear:
Select the enhancement and press enter.
In the above screen you can find the red colored line (Begin Enhancement … End enhancement). In between that you can add you own code.
Create Customer Quotation (BAPI_QUOTATION_CREATEFROMDATA2) with BAPI Extension Our client had a requirement to create customer quotation using BAPI function moduleBAPI_QUOTATION_CREATEFROMDATA2 with reference to Container process by using custom dialog program . This BAPI is mapped with standard SAP Transaction VA21. The client requirement is to map all standards quotation fields and need to add 4 customize fields Plug in Date(DATS) , Plugin Time(TIMS) , Plug out Date(DATS), Plug out time (TIMS) which are added at line item level by creating extra tab Additional data B.
Standard quotation field we can easily mapped using BAPI but as per requirement we need to do extension of BAPI BAPI_QUOTATION_CREATEFROMDATA2 for 4 customize fields. Steps for BAPI Extension: 1.
Append Z structure in VBAP table with 4 customize fields.
2.
In BAPI BAPI_QUOTATION_CREATEFROMDATA2 we need to use EXTENSIONIN parameter for BAPI extension
3.
Because the BAPIs work with checkboxes, you must also define a customize structure (ZZDATES_TIMES1X) checkbox for the customer structure. It should be structured like this:
4.
Define these customer structures in the structures VBAPKOZ und VBAPKOZX using INCLUDE or APPEND.
5.
Add the customer structures to the BAPE_VBAP and BAPE_VBAPX BAPI structures using the APPEND technique.
6.
Adjust the following structures f or customer enhancements to table VBAP: ��
VBAPKOZ
��
VBAPKOZX
��
BAPE_VBAP
��
BAPE_VBAPX
7.
Generally, the data should be added to the BAPI interface in the internal communication structures (VBAKKOM, and so on). There, you can process the data in the SD tables (VBAK, and so on).
8.
If the EXTENSIONIN parameter contains customer data, you must complete at least the key fields in the relevant standard parameters.
9.
Added logic for BAPI extension in custom ized dialog program as below
Value Proposition to Customer : We can create customer quotation using BAPI and additional we can extend standard Customer Quotation Create BAPI for the additional customized fields as per customer’s requirement. Sales order: Create Sales Order (BAPI_SALESORDER_CREATEFROMDAT2 ) with BAPI Extension Our client has requirement to create Sales Order using BAPI function module BAPIBAPI_SALESORDER_CREATEFROMDAT2 with reference to Container process by using customized dialog program . This BAPI is mapped with standard SAP Transaction VA01. The client requirement is to map all standards sales order fields and need to add 4 customize fields Plug in Date(DATS) , Plugin Time(TIMS) , Plug out Date(DATS), Plug out time (TIMS) which are added at line item level by creating extra tab Additional data B.
Repeat all steps for BAPI Extension used in Create Customer Quotation as above. The same steps for extending sales order, can be done as we have done it for create quotation BAPI.
Parking a Document in FI using BAPI In FI Module, Sometimes we need to park a document instead of Posting. Through BDC, direct input method we can do this. But in this post, I am explaining the Parking using BAPI. There is no Direct BAPI for parking the FI Document. So using the Document posting BAPI, ‘BAPI_ACC_GL_POSTING_POST’ we can achieve this easily. Header-username = sy-uname. Header-comp Header-fisc_year = sy-datum+0(4). Header-doc_date = sy-datum. Header-pstng_date = sy-datum. Header-fis_period = sy-datum+4(2). Header-doc_type = 'SA'. accountgl-itemno_acc = '1'. accountgl-gl_account = '0000115510' . accountgl-comp_code = '1000'. accountgl-pstng_date = sy-datum. accountgl-doc_type = 'SA'. accountgl-fisc_year = sy-datum+0(4). accountgl-fis_period = sy-datum+4(2). APPEND accountgl. CLEAR accountgl. loc_cnt = 1. loc_cnt = loc_cnt + 1. accountgl-itemno_acc = loc_cnt. accountgl-gl_account = '0000601900' . accountgl-comp_code = '1000'.
code = '1000'.
accountgl-pstng_date = sy-datum. accountgl-doc_type = 'SA'. accountgl-fisc_year = sy-datum+0(4). accountgl-fis_period = sy-datum+4(2). accountgl-costcenter = ‘13100’. APPEND accountgl. CLEAR accountgl. currencyamount-itemno_acc = ‘1’. currencyamount-currency = 'USD'. currencyamount-amt_doccur = loc_amt. APPEND currencyamount.
(
For Debit
/
‘S’)
CLEAR currencyamount. currencyamount-itemno_acc = ‘2’. currencyamount-currency = 'USD'. currencyamount-amt_doccur = ( -1 ) * ( loc_amt ). ( APPEND currencyamount.
For
credit
/‘H’)
CLEAR currencyamount. extension1-field1 = 'BAPI-PARK'. extension1-field2 = '1'. APPEND extension1. CALL FUNCTION 'BAPI_ACC_GL_POSTING_POST' EXPORTING documentheader = header IMPORTING obj_key = obj_key TABLES accountgl = accountgl currencyamount = currencyamount return = return extension1 = extension1 CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. Implement Customer Exit (CMOD) ACBAPI01, with Include Program ZXACCU15 of Component EXIT_SAPLACC4_001.
READ TABLE extension INDEX 1. IF sy-subrc = 0 AND extension-field1 = 'BAPI-PARK'. ( to confirm for our parking, because it should not work for all the BAPI posting by the standards.) MOVE 2 TO t_acchd-status_new. " Park Document Endif. To check the parked Document, go to table BKPF and we can check the Document status as ‘V’.
We can check the parked document in FBV0. One more important thing after implementing all the above steps, if you are trying to post a parked document which is created by the above process, you may be getting some error. We have to follow steps of the OSS notes (Note 1020574) to avoid that error.
Creating Sales Scheduling Agreement with Extensions using BAPI Introduction: A Scheduling Agreement is an outline agreement created between a Vendor and a Customer. A Sales Scheduling Agreement (not to be confused with the Purchasing Scheduling Agreement) is a long term Sales Agreement which contains the details of the delivery schedule and the deliveries are made to the customer as per the schedule entered in the document. The Transaction code for Creating a Sales Scheduling Agreement is VA31.
The objective of this document is to illustrate creation of Sales Scheduling Agreements with Extensions (Z fields) using BAPI. In this scenario, a Z field (ZZMTLIFNR) that exist in VBAP (Sales order Line Item Table) needs to be mapped using the BAPI for creation of Scheduling Agreement. BAPI: The BAPI used for Creation of Scheduling Agreements is SD_SALESDOCUMENT_CREATE. The following parameters/tables need to be populated before the BAPI is called: 1. SALES_HEADER_IN This structure needs to be populated with header level data of the scheduling agreement that needs to be created. Some of the parameters that are passed to this structure are: 1. 2. 3. 4. 5. 6. 7. 8. 9.
DOC_DATE – Document date VBAK-AUDAT DOC_TYPE – Document Type VBAK-AUART SALES_ORG – Sales Organization VBAK-VKORG DISTR_CHAN – Distribution Channel VBAK-VTWEG DIVISION – Division VBAK-SPART PURCH_DATE - Purchase Date VBAK-BSTDK CT_VALID_F – Valid from date VBAK-GUEBG CT_VALID_T – Valid to date VBAK-GUEEN PRICE_DATE – Pricing Date VBAK-PRSDT
2. SALES_HEADER_INX The fields in this structure needs to be populated with ‘X’, if they are being populated in the structure SALES_HEADER_IN. For e.g. if SALES_HEADER_IN-DOC_DATE is getting populated with a value say 11/01/2010, then SALES_HEADER_INX-DOC_DATE should be set to ‘X’. Code snippet for populating these two structures is given below:
Also the field SALES_HEADER_INX -UPDATEFLAG needs to be set to ‘I ’.
3. SALES_ITEMS_IN This table needs to be populated with item level data of the scheduling agreement that needs to be created. The parameters that are passed to this table are: 1. 2. 3. 4. 5. 6. 7.
ITM_NUMBER - Item Number MATERIAL - Material Number PLANT - Plant ITEM_CAT - Item Category TARGET_QTY - Target Quantity CUST_MAT35 - Customer Material REASON_REJ - Reason for Rejection
4. SALES_ITEMS_INX
VBAP-POSNR VBAP-MATNR VBAP-WERKS VBAP-PSTYV VBAP-ZMENG VBAP-KDMAT VBAP-ABGRU
The fields in this structure needs to be populated with ‘X’, if they are being populated in the structure SALES_ITEMS_IN. Code snippet for populating these two structures is given below:
The UPDATEFLAG in SALES_ITEMS_INX table needs to be set to ‘I’ (for insert).
The workareas are appended to the internal table.
5. SALES_PARTNER This table should be populated with Partner Level Information (Sold to Party, Ship to Party Etc) of the document. The parameters that need to be passed are: 1. PARTN_ROLE
- Partner Role - VBPA-PARVW
2. PARTN_NUMBER – Partner Number – VBPA-KUNNR
3. ITM_NUMBER - Item Number - VBPA-POSNR – This field needs to be populated with ‘000000’ for Header Level Partner and VBAP-POSNR for Item Level Partner.
6. SALES_SCHEDULES_IN This table is populated with Schedule Line Level Data of the Document. The following parameters need to be populated: 1.
ITM_NUMBER 2. SCHED_LINE 3. REQ_DATE 4. REQ_DATE
- Item Number - VBEP-POSNR - Schedule Line VBEP-ETENR - Required Quantity VBEP-WMENG - Required Date VBEP-EDATU
7. SALES_SCHEDULES_INX The fields in this structure needs to be populated with ‘X’, if they are being populated in the structure SALES_SCHEDULES_IN.
Work area is appended to the Internal Table
8. SALES_TEXT The parameters in this table are populated with header level and item level texts which need to be populated for the Scheduling Agreement. The parameters of this table which need to be filled are: 1.
ITM_NUMBER Item Number Number - VBAP-POSNR for Item Level Text and ‘000000’ for Header Level Text 2. TEXT_ID Text Id 3. TEXT_LINE Text Line -The actual text which needs to be populated 4. FUNCTION Function - needs to be populated with ‘009’ (Insert)
9. EXTENSIONIN This table needs to be populated for populating the Z fields at Header or Item Level of the Schedule Line agreement. In this scenario, we will see how a Z field at the item level (VBAP-ZZMTLIFNR) is populated using the BAPI. The following procedure needs to be followed: •
•
The field ZZMTLIFNR already exists in VBAP table through customer append of structure ZMOTC_LIFNR.
Enhance Structure BAPE_VBAP by appending a structure (ZMSOTC_LIFNR) which contains the two fields – ZZMTLIFNR and
ZZMTIDNEX.
•
Enhance BAPE_VBAPX by appending structure (ZMSOTC_LIFNRX) containing two flag fields – ZZMTLIFNR (single character) and ZZMTIDNEX (single character). This structure has to be enhanced because the BAPIs work with checkboxes.
•
Enhance Structure VBAPKOZ by appending a structure (ZMSOTC_LIFNR_COM) which contains the two fields – ZZMTLIFNR and ZZMTIDNEX.
•
•
Enhance VBAPKOZX by appending structure (ZMSOTC_LIFNR_COMX) containing two flag fields – ZZMTLIFNR (single character) and ZZMTIDNEX (single character). This structure has to be enhanced because the BAPIs work with checkboxes.
Adjust the following structures for customer enhancements to table VBAK: a) VBAKKOZ b) VBAKKOZX c) BAPE_VBAK d) BAPE_VBAKX
•
Adjust the following structures for customer enhancements to table VBEK: a) VBEPKOZ b) VBEPKOZX c) BAPE_VBEP d) BAPE_VBEPX
•
Now the structural enhancements have been done and the coding part has to be done in this manner : Populate EXTENSIONIN-STRUCTURE with ‘BAPE_VBAP’. th
th
Populate 11 to 26 character EXTENSIONIN-VALUEPART1 concatenation of fields VBAP-POSNR and VBAP-ZZMTLIFNR.
with
string
which
is
a
The first 10 characters of EXTENSION-VALUEPART1 are reserved for VBAP-VBELN and the Z fields to be populated are passed to VALUEPART1 as subsequent characters of this field. Append this record to table EXT ENSIONIN. Populate EXTENSION-STRUCTURE with BAPE_VBAPX. th
th
Populate 11 to 17 characters of EXTENSIONIN-VALUEPART1 with a string which is a concatenation of fields VBAP-POSNR and ‘X’.
The first 10 characters of EXTENSION-VALUEPART1 are reserved for VBAP-VBELN and the Z fields to be populated are passed to VALUEPART1 as subsequent characters of this field. Append this record also to table EXT ENSIONIN.
Now all the parameters that need to be passed to the BAPI have been populated. The BAPI will be called now:
The scheduling agreement that is created is returned in the importing parameter SALESDOCUMENT_EX and if the RETURN table does not have any error messages or abort messages then BAPI_TRANSACTION_COMMIT is called.
In this manner a Sales Scheduling Agreement with Extensions is created using BAPI.
Post FI document using BAPI INTRODUCTION: The accounting documents record changes in values in a company code arising from accounting transactions. They consist of one or more line items (postings), each of which represents an individual transaction posted to an account. When posting an accounting document, the SAP system updates the transaction figures in the accounts to which the document is posted.An accounting document is a representation within the SAP System of the document (for example, an invoice) that triggered the posting. BAPI ‘BAPI_ACC_DOCUMENT_POST’ acts as the interface between your code and standard transactions This
document
FB01/FB50. explains
the
various
steps
involved
to
post
a
FI
document
using
BAPI
‘BAPI_ACC_DOCUMENT_POST’ from an excel file.This is helpful to SAP Technical consultants as it gives the details of all the required parameters and data in document posting and validation to check if the GL account in which we are posting amounts is present in SAP database. Transaction code for FI documents are FB01/FB50(Create), FB02(Change) and FB03(Display). In one document, more than 999 line item cannot be posted because BAPI can post up to 999 lines in one document. The file should be balanced for the document to be posted i.e. credit amount and debit amount should be equal. In FI, header table for document data is BKPF and line item table is BSEG. STEPS: Various steps involved in posting a document from a file are: 1. Upload data from excel file into an internal table using FM ‘GUI_UPLOAD’ as explained in subroutine UPLOAD_FORM_EXCEL. 2. After we upload excel data into an internal table we need split the data into field patterns into header data and line item data as explained in subroutine ‘SPLIT_INTO_RECORDS’. This splitting is done according to my file format. In header data in the file in line no.2, posting date and header company code. That is why I have used case when 2. And all the other information is present at the lines as described by CASE statement. Line item details start from line number 14, that’s why LOOP AT t_upload INTO w_upload FROM 14is used. User can split the data according to their file format. 3. After the splitting, loop at the line item table and validate the GL account numbers. If the GL accounts are valid, Populate the line item data into BAPI GL and currency tables otherwise append that GL account number into error message table. This is explained in subroutines ‘VALIDATE_GL’ and ‘POPULATE_BAPI’. 4. In case of intercompany postings, in which line item and Header Company codes are different. Find out line item company code currency key and compare if the line item currency key and header currency key given is same or different. If these are different then we need to populate ‘EXCHANGE_RATE’ field of currency table if it is given in the file. Otherwise BAPI itself will fetch the exchange rate between 2 currencies and do the conversion.
5. Above step is required because a document is posted in 3 currencies document currency, company code currency and group currency. In case of the intra company code postings all the currencies are same so the amounts are same In all3 currencies. But in case of inter company code postings the currencies are different so the amounts are also different. Due to this we need to give and exchange rate base on which the BAPI will convert the amounts. 6. Next step in document posting is to check the data using BAPI ‘BAPI_ACC_DOCUMEN_CHECK’. If this BAPI do not return any error message, go to next step otherwise display error message given by BAPI. 7. Post the document using BAPI ‘BAPI_ACC_DOCUMEN_POST’. Check for sy-subrc, if initial; commit the
work
using
‘BAPI_TRANSACTION_COMMIT’.
This
step
is
explained
in
subroutine
‘POST_DOCUMENT’. While providing header data in W_HEADER work area do not provide OBJ_KEY, OBJ_TYPE and OBJ_SYS, these are automatically populated by BAPI. SUMMARY: This document explains the various steps involved in FI document posting from excel file. Main steps involved are uploading and splitting data into internal table, Populate data into BAPI tables and call BAPI to post the documents and committing the transaction. A sample source code is given for the references. Note:In case of intercompany code postings, if the exchange rate between 2 currencies is given in the file, programmer should pass only the exchange rate given. Currency value conversions will be done by the BAPI. Also Field status group configurations of GL accounts are not considered by BAPI as it is bypassed by BAPI, but these are considered when doing posting using FB50. SOURCE CODE:
*Global DATA declarations TYPES: BEGIN OF type_upload, "Work table used for upload rec(500) TYPE c, END OF type_upload. *Global work area declarations DATA: w_postingsTYPE , w_header TYPE bapiache09, w_bukrs TYPE type_bukrs, w_upload TYPE type_upload, w_item_gl TYPE bapiacgl09, w_dummy(1) TYPE c, "#EC NEEDED w_item_curr TYPE bapiaccr09. DATA:t_posting TYPE STANDARD TABLE OF , t_return TYPE STANDARD TABLE OF bapiret2, t_bukrs TYPE STANDARD TABLE OF type_bukrs, t_item_gl TYPE STANDARD TABLE OF bapiacgl09, t_upload TYPE STANDARD TABLE OF type_upload, t_filename TYPE STANDARD TABLE OF type_filename, t_item_curr TYPE STANDARD TABLE OF bapiaccr09. *&--------------------------------------------------------------------* *&Form UPLOAD_FROM_EXCEL *&--------------------------------------------------------------------* FORM upload_from_excel. REFRESH t_upload. *--upload data from excel to internal table
DATA: lw_filename TYPE string. lw_filename = p_file. CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = lw_filename filetype = 'ASC' TABLES data_tab = t_upload 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. *- End of change @ECC ENDFORM." UPLOAD_FROM_EXCEL *&--------------------------------------------------------------------* *&Form SPLIT_INTO_RECORDS *&--------------------------------------------------------------------* FORM split_into_records. DATA: lv_doc_date TYPE sy-datum, lv_account TYPE char10, lv_product TYPE char20, lv_period TYPE char2, lv_kursf TYPE char9, lv_aufnr TYPE char12, lv_plant TYPE char10, lv_customer TYPE kunnr, lv_cust_hie2 TYPE hiezu02, lv_pstng_date TYPE sy-datum, lv_amount TYPE char30. CLEAR:lv_doc_date, lv_account, lv_product, lv_period, lv_kursf, lv_plant, lv_customer, lv_cust_hie2, lv_pstng_date, lv_amount. *Split header data LOOP AT t_upload INTO w_upload. CASE sy-tabix. * start of insert @007
WHEN 2. SPLIT w_upload w_dummyw_dummyw_dummy w_dummyw_dummyw_dummy w_dummyw_dummyw_dummy w_header-ac_doc_no w_dummy. * end of insert @007 WHEN 4. SPLIT w_upload w_dummyw_dummy w_dummyw_dummy lv_doc_date w_dummyw_dummy w_header-header_txt w_dummyw_dummy w_header-comp_code w_dummy. WHEN 6. SPLIT w_upload w_dummyw_dummy w_dummyw_dummy lv_pstng_date w_dummyw_dummy w_header-ref_doc_no w_dummyw_dummy w_header-doc_type w_dummy. WHEN 8. SPLIT w_upload w_dummyw_dummy w_dummyw_dummy lv_period w_dummyw_dummy g_jtype w_dummyw_dummy"@008
AT c_tab INTO
AT c_tab INTO
AT c_tab INTO
AT c_tab INTO
w_header1-curr w_dummy. "@008 WHEN 10. SPLIT w_upload AT c_tab INTO w_dummyw_dummy w_dummyw_dummy g_rdate w_dummyw_dummy w_header-reason_rev w_dummyw_dummy "@008 lv_kursf "@008 w_dummy. "@008 EXIT. ENDCASE. *Split line item data LOOP AT t_upload INTO w_upload FROM 14. CLEAR lv_account. SPLIT w_upload AT c_tab INTO w_postings-company lv_account w_postings-ttype w_postings-pkey
"@008
lv_amount w_postings-ccent w_postings-prcent w_postings-description w_postings-assignment w_postings-tax_code w_postings-juridiction_code lv_aufnr w_postings-psa lv_plant w_postings-wbs_element lv_product w_postings-product_hie w_postings-meins w_postings-quantity w_postings-asgm w_postings-asgc w_postings-billing_type lv_customer lv_cust_hie2 w_postings-order_reason w_postings-dsd_return_rsn w_postings-reson1 w_postings-item_sales w_postings-sales_order w_postings-dist_channel w_postings-sales_org w_postings-division w_postings-shipping_type w_postings-partner_pc w_postings-item_category w_postings-trading_partner w_postings-market w_postings-sales_office w_postings-country w_postings-resp w_postings-value_date w_postings-trade_type w_postings-controlling_area w_postings-cost_object w_postings-cust_hie1 w_postings-cust_hie3 w_postings-cust_hie4 w_postings-cust_hie5 w_postings-cust_hie6 w_postings-cust_hie7 w_postings-ship_to_party w_postings-fin_plning_lvl w_postings-ext_ic_mod_std w_postings-vv160_intrprtation w_postings-vv161_intrprtation w_postings-crm_marketing w_dummy. w_postings-amount = lv_amount. * END OF @007 IF NOT ( w_postings-pkey IS INITIAL ). APPEND w_postings TO t_posting.
ENDIF. CLEAR: w_postings, lv_aufnr,lv_cust_hie2, lv_customer, lv_product, lv_plant, lv_account, lv_amount. ENDLOOP. IF t_bukrs IS NOT INITIAL. DELETE ADJACENT DUPLICATES FROM t_bukrs COMPARING bukrs. *Fecth the currency for company codes SELECT bukrs waers FROM t001 INTO TABLE t_curr FOR ALL ENTRIES IN t_bukrs WHERE bukrs EQ t_bukrs-bukrs. ENDIF. IF t_curr IS NOT INITIAL. SORT t_curr BY bukrs. ENDIF. ENDFORM." SPLIT_INTO_RECORDS *&---------------------------------------------------------------------* *& Form VALIDATE_GL *&---------------------------------------------------------------------* FORM validate_gl. TYPES: BEGIN OF ltype_gl, bukrs TYPE bukrs, saknr TYPE saknr, xintb TYPE xintb, END OF ltype_gl. DATA: lt_postings_temp TYPE STANDARD TABLE OF type_item, lt_gl_flag TYPE STANDARD TABLE OF ltype_gl, lw_gl_flag TYPE ltype_gl, lv_msg TYPE char100, lw_message TYPE type_message, lv_counter TYPE posnr_acc. REFRESH: lt_postings_temp, lt_gl_flag. CLEAR: lw_gl_flag, lv_counter. lt_postings_temp = t_posting. * lt_postings_temp = t_posting. IF lt_postings_temp IS NOT INITIAL. SORT lt_postings_tempBY company account. DELETE ADJACENT DUPLICATES FROM lt_postings_temp COMPARING company account. SELECT "#EC CI_SGLSELECT bukrs saknr xintb FROM skb1 INTO TABLE lt_gl_flag FOR ALL ENTRIES IN lt_postings_temp WHERE bukrs = lt_postings_temp-company AND saknr = lt_postings_temp-account. ENDIF. **Intialize counter lv_counter = 1. LOOP AT t_posting INTO w_postings.
READ TABLE lt_gl_flag INTO lw_gl_flag WITH KEY saknr = w_postings-account bukrs = w_postings-company. IF sy-subrc IS INITIAL. IF lw_gl_flag-xintb = c_x. CONCATENATE text-004 w_postings-account text-006 INTO lv_msg SEPARATED BY space. * Else display the error message for GL account. lw_message-mes_type = c_e. lw_message-text = lv_msg. APPEND lw_message TO t_message. CLEAR lw_message. g_error_flag = c_x. ELSE. PERFORM populate_bapi USING lv_counter. * Increase counter by one lv_counter = lv_counter + 1. ENDIF. ELSE. "error message lv_msg * Else display the error message for GL account. lw_message-mes_type = c_e. lw_message-text = lv_msg. APPEND lw_message TO t_message. CLEAR lw_message. g_error_flag = c_x. ENDIF. ENDLOOP. ENDFORM." VALIDATE_GL *&---------------------------------------------------------------------* *& Form POPULATE_BAPI *&---------------------------------------------------------------------* FORM populate_bapi USING rlv_counter TYPE posnr_acc. **Populate line item details IF w_postings-company NE w_header-comp_code. g_cross_flag = c_x. ENDIF. *Fill Credit line item data IF w_postings-pkey=50. w_item_gl-itemno_acc = rlv_counter. w_item_gl-comp_code = w_postings-company. w_item_gl-gl_account = w_postings-account. w_item_gl-pstng_date = w_header-pstng_date. w_item_gl-cs_trans_t = w_postings-ttype. w_item_gl-costcenter = w_postings-ccent. w_item_gl-profit_ctr = w_postings-prcent. w_item_gl-wbs_element = w_postings-wbs_element. w_item_gl-orderid = w_postings-internal_order . w_item_gl-item_text = w_postings-description. w_item_gl-alloc_nmbr = w_postings-assignment. w_item_gl-tax_code = w_postings-tax_code. w_item_gl-taxjurcode = w_postings-juridiction_co de. w_item_gl-orderid = w_postings-internal_order . w_item_gl-base_uom = w_postings-meins. w_item_gl-quantity = w_postings-quantity. w_item_gl-trade_id = w_postings-trading_partne r. w_item_gl-value_date = w_postings-value_date.
APPEND w_item_gl TO t_item_gl. IF w_header2-kursf IS NOT INITIAL. * To fetch company code currency READ TABLE t_curr INTO w_curr WITH KEY bukrs = w_postings-company BINARY SEARCH. IF w_curr-waers NE w_header1-curr. w_item_curr-exch_rate = w_header2-kursf. ENDIF. ENDIF. w_item_curr-itemno_acc =rlv_counter. w_item_curr-curr_type = c_doc_curr. "Document Currency w_item_curr-currency = w_header1-curr. w_item_curr-amt_doccur =w_postings-amount * ( -1 ). APPEND w_item_curr TO t_item_curr. CLEAR w_item_curr. g_credit_total = g_credit_total + w_postings-amount. * Clear variables CLEAR: w_postings, w_item_curr, w_curr, w_item_gl. *Fill Debit line item data ELSEIF w_postings-pkey= 40. w_item_gl-itemno_acc = rlv_counter. w_item_gl-comp_code = w_postings-company. w_item_gl-gl_account = w_postings-account. w_item_gl-pstng_date = w_header-pstng_date. w_item_gl-cs_trans_t = w_postings-ttype. w_item_gl-costcenter = w_postings-ccent. w_item_gl-profit_ctr = w_postings-prcent. w_item_gl-wbs_element = w_postings-wbs_element. w_item_gl-orderid = w_postings-internal_order . w_item_gl-item_text = w_postings-description. w_item_gl-alloc_nmbr = w_postings-assignment. w_item_gl-tax_code = w_postings-tax_code. w_item_gl-taxjurcode = w_postings-juridiction_co de. w_item_gl-orderid = w_postings-internal_order . w_item_gl-base_uom = w_postings-meins. w_item_gl-quantity = w_postings-quantity. w_item_gl-part_prctr = w_postings-partner_pc. w_item_gl-trade_id = w_postings-trading_partne r. w_item_gl-value_date = w_postings-value_date. APPEND w_item_gl TO t_item_gl. IF w_header2-kursf IS NOT INITIAL. * To fetch company code currency READ TABLE t_curr INTO w_curr WITH KEY bukrs = w_postings-company BINARY SEARCH. IF w_curr-waers NE w_header1-curr. w_item_curr-exch_rate = w_header2-kursf. ENDIF. ENDIF. w_item_curr-itemno_acc =rlv_counter. w_item_curr-curr_type = c_doc_curr. "Document Currency w_item_curr-currency = w_header1-curr. w_item_curr-amt_doccur =w_postings-amount .
APPEND w_item_curr TO t_item_curr. CLEAR w_item_curr. g_debit_total = g_debit_total + w_postings-amount. * Clear variables CLEAR: w_postings, w_item_curr, w_curr, w_item_gl. ENDIF. ENDFORM." POPULATE_BAPI *&---------------------------------------------------------------------* *&Form POST_DOCUMENT *&---------------------------------------------------------------------* FORM post_document. TYPES:BEGIN OF type_bkpf, bukrs TYPE bkpf-bukrs, belnr TYPE bkpf-belnr, gjahr TYPE bkpf-gjahr, END OF type_bkpf. *Local variable declarations DATA: lv_belnr TYPE belnr_d, lv_bukrs TYPE bukrs, lv_gjahr TYPE gjahr, lw_message TYPE type_message, lv_msg TYPE symsgv, lv_rdate TYPE sy-datum, lt_return TYPE STANDARD TABLE OF bapiret2, lw_return TYPE bapiret2, "#EC NEEDED lt_bkpf TYPE STANDARD TABLE OF type_bkpf, lw_bkpf TYPE type_bkpf, lv_bvorg TYPE bvor-bvorg. *Clear local work area and variables CLEAR: lw_message, lv_msg, lw_return, lw_bkpf. CLEAR: w_header-obj_type, w_header-obj_key, w_header-obj_sys. IF g_error_flag NE c_x. IF g_credit_total<> 0 AND g_debit_total<> 0. CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK' EXPORTING documentheader= w_header TABLES accountgl = t_item_gl currencyamount = t_item_curr return = t_return. If t_return is not initial. * Posting journal entries CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST' EXPORTING documentheader = w_header IMPORTING obj_type = g_obj_type obj_key = g_obj_key obj_sys = g_obj_sys
TABLES accountgl = t_item_gl currencyamount = t_item_curr return = t_return. IF sy-subrc<> 0. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = c_x. ENDIF. Endif. ENDIF.
Custom BAPI creation - Step-by-step Procedure To create the custom BAPI, we need to follow this step-by-step procedure. The steps are 1.
Create the BAPI structure. 2. Create Remote Function Module. 3. Place the Remote Function Module into BOR (Business Object Repository). 4. Implement, Generate and Release the BAPI. Step 1: To create BAPI structure, the structure name should start with the keyword ZBAPI. Here, Two BAPI structures are created in this scenario .The name of these BAPI structures areZBAPI_IMPORT and ZBAPI_TABLE. Step 2: ZBAPI_IMPORT structure contains the following fields.
Step 3: ZBAPI_TABLE structure contains the following fields.
Step 4: Now, define the Remote Enabled Function Module using T-Code SE37 starting with theZBAPI . Provide the name of the function module as ZBAPI_DEMO_SD. We must pass the Importand export parameters as value parameters. Attribute Parameters:
Import Parameters:
Export Parameters:
Return parameter in the Export parameter contains the error messages. Here, we must create a parameter of type structure BAPIRETURN either in Export Parameter or Table Parameter. Table Parameters:
Source Parameter:
Step5: Place this Function Module in to BOR using the T-code SWO1. Provide the name of the object and press create button.
Provide the following details.
Step 6: Add our Custom BAPI using Ut ilities->API Method->Add M ethod
Now, Provide the created Custom Function Module name and click enter.
Press the Next Step Button.
Press the Next Step Button.
Click the YES button.
Now, Method is added in the BOR.
And Press Save button. Step 7: Now Implement the BAPI.
Now, Generate the BAPI by clicking symbol ‘ball’.
Lastly, Release the BAPI.
Step 8: Now test the BAPI by clicking Execute Button.
The following screen appears and click execute button.
Provide the sales document number and press execute button.
Now check the output as follows…
Custom BAPI creation - Step-by-step Procedure Scenario: Step-by-step creation of a BAPI to retrieve fields from table T001. Procedure: Go to transaction SE11 and create a structure as shown or as per your requirement. Give the name in the Data type field and click create.
In the pop-up that comes up, select the radio button “ structure”.
In the components tab of the structure, give the different fields and their corresponding field types and press enter to check the compatibility and corrective ness.
Do not forget to save it in a package. You can even save it as a local object. For my example, I save it in a package.
Check the structure (ctrl + F2) and activate (ctrl + F3) the structure. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Now we are done with the creation of a Structure. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Go to transaction SE37 where you create function modules. Click on create after you enter the name of the Function module.
A screen as shown above would pop-up where you mention the function group to save the function module and also provide some short text describing your function module.
In the next pop-up that follows, click on continue as shown above.
The function module screen would look like the one above.
Go to the Attributes tab and select the radio button reading “remote-enabled module”. Come back to the imports tab and provide the import parameters as shown or as per your requirement.
Now in the Export tab, provide the export parameters as shown or as per your requirement.
In the tables tab, provide the information as shown or as per your requirement.
The next screen you visit is the source code. It would look like this.
In the source code tab, write the following code in order to pick the data based on the input you provide.
Now, save and check the code and activate the function module. After successful activation, G o to the attributes tab. G o to Function moduleReleaseRelease.
+++++++++++++++++++++++++++++++++++++++++++++++ Now we are done with the creation of a Function Module. +++++++++++++++++++++++++++++++++++++++++++++++ Go to transaction SWO1 and enter the name of the BAPI you would like to create or as shown in the screen and click the create button.
Give the name of the BAPI as above and click on create.
Give the above-mentioned details and click on the continue icon.
Save in a package. The resulting screen is as follows.
Now click on the methods to drop down and see what methods are provided by default. There would be two methods, showing in red color which come by default while creating the BAPI.
Click or select the method as shown above and go to the path “Utilities API methods Add methods”.
On the screen that follows, provide the function module name and click on the continue icon.
In the ultimate pop-up, click the next step icon. We observe that the information is predefined in the fields. This is the next screen where you would just click on the “next” icon.
Click on Yes. You can see an information message reading “ ZBAPIFMT001” inserted. Now save after you add the method. Select & Double click on the API method. Go to Tab: ABAP Check 'API Function'.
The above screen is displayed. Go to the ABAP tab as shown below.
Select the Radio button reading “API Function” as already said above.
click on the continue icon to proceed further. Now select the Object “ZBAPI_T001” as shown below.
Go to : EditChange Release StatusObject type
To
Modeled.
The above shown screen will be displayed. Click on yes. The message shows, The object type status set to modeled. (or already modeled) Go to : EditChange Release StatusObject type
To
Implemented.
You can see a message reading “Object type status set to implemented” Now, go to: EditChange Release Status ObjectTo Released.
There would be two pop ups coming up. Click continue on the Pop Ups. Keep the cursor on the 'Method'. Go to: EditChange Release StatusObject type component TO Modeled.
You can see the message reading “ status for method ‘zbapifmt001’ set to modeled”. Now, go to: EditChange Release Status Object type component TO Implemented
You can see the message reading “ status for method ‘zbapifmt001’ set to implemented”. Now go to: EditChange Releasse Status
Object
type component
To
Released
You can see the message reading “ status for method ‘zbapifmt001’ set to Released”. Click on Generate Button. (the red ball kind of button is the Generate button)
After clicking on the generate button, you can see the message reading “Object type 'ZBAPI_T001' generated successfully”. Now go to BAPI Tcode (BOR) there we can find the BAPI (our BAPI) The BAPI browser would look like the screen below.
You can click on the Alphabetical tab so that you can browse the BAPI’s in an alphabetical order. Find your BAPI as shown.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Now we are done with the creation of a BAPI.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Test Your BAPI.
Enter the name of your BAPI in the transaction SWO1 and click on ‘Test’.
The above screen is displayed. Click on the Execute icon against the BAPI as shown.
The above screen is displayed where you would require entering the data against the empty input fields.
We have entered some data in the Field. After entering the data, click on the execute icon as shown below.
The following screen is displayed which has some values as is indicated by the ITEMTAB.
Click on the Edit table icon as shown below.
The results as per our input are as shown below.
By this, we would get it confirmed that our BAPI is working properly. We can even check it by passing different values again. Come back to the input and execution screen.
After executing the BAPI based on t he input provided, we get the f ollowing screen.
Hit on the execute icon.