BAPI_AGREEMENT_CONTRACT - ME31K Create Vendor Contract

How to used BAPI_AGREEMENT_CONTRACT to create vendor contract for tcode ME31K?

REPORT zbapi_contract_create NO STANDARD PAGE HEADING

LINE-SIZE 120.
 

*&---------------------------------------------------------------------*
*& I N C L U D E S
*&---------------------------------------------------------------------*

*& TOP INCLUDE

INCLUDE zcontract_create_top.

*& FORMS INCLUDE

INCLUDE zcontract_create_forms.

*&---------------------------------------------------------------------*
*& A T - S E L E C T I O N - S C R E E N
*&---------------------------------------------------------------------*

*& Providing f4 help path to filmanager on presentation server

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

PERFORM get_file_name USING p_file.

*&---------------------------------------------------------------------*
*& S T A R T - O F - S E L E C T I O N
*&---------------------------------------------------------------------*

START-OF-SELECTION.

*& Getting xls file from presentation server and converting it into int tab

PERFORM convert_xls_to_itab USING it_type

CHANGING it_contract.

*&---------------------------------------------------------------------*
*& E N D - O F - S E L E C T I O N
*&---------------------------------------------------------------------*

END-OF-SELECTION.

*& Filling up data for bapi fm and processing it for contract creation

PERFORM build_and_processing_bapi USING it_contract.

*& return messages from bapi

PERFORM get_return_messages.

*&---------------------------------------------------------------------*
*& T O P - O F - P A G E
*&---------------------------------------------------------------------*

TOP-OF-PAGE.

PERFORM write_heading.

*&---------------------------------------------------------------------*
*& Include ZCONTRACT_CREATE_TOP
*&---------------------------------------------------------------------*

TYPE-POOLS: truxs.

*& structure same as xls file on presentation server

TYPES: BEGIN OF ty_contract,

vendor(10),
agree_type(2),
agree_date(10),
pur_org(4),
pur_grp(3),
item_cat(1),
plant(4),
sto_loc(4),
val_start(10),
val_end(10),
material(18),
qty(15),
uom(3),
net_price(15),

END OF ty_contract,

*& structure for reporting status for created contracted

BEGIN OF ty_message,

vendor(10),
status(12),
message TYPE bapi_msg,

END OF ty_message.

TYPES: ty_t_contract TYPE STANDARD TABLE OF ty_contract.

*& work area declarations

DATA: wa_contract TYPE ty_contract,

wa_contract_header TYPE bapioahead,
wa_contract_header_x TYPE bapioaheax,
wa_contract_detail TYPE bapioaitem,
wa_contract_detail_x TYPE bapioaitex,
wa_return TYPE bapiret2,
wa_message TYPE ty_message,

*& Internal table declarations

it_contract_detail TYPE STANDARD TABLE OF bapioaitem,
it_contract_detail_x TYPE STANDARD TABLE OF bapioaitex,
it_contract TYPE STANDARD TABLE OF ty_contract,
it_type TYPE truxs_t_text_data,
it_return TYPE STANDARD TABLE OF bapiret2,
it_message TYPE STANDARD TABLE OF ty_message,

*& misc data declarations for programming logic

l_count TYPE evrtp VALUE '00000'.

*& constants used in prog

CONSTANTS: cns_x TYPE char1 VALUE 'X'.

*&----------------- S E L E C T I O N - S C R E E N -----------------*

SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE text-001.

PARAMETERS: p_file TYPE rlgrap-filename OBLIGATORY.

SELECTION-SCREEN END OF BLOCK bl1.

*&---------------------------------------------------------------------*
*& Include ZCONTRACT_CREATE_FORMS
*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*& Form convert_xls_to_itab
*&---------------------------------------------------------------------*

* Conversion of xls file
*----------------------------------------------------------------------*

* -->P_IT_TYPE used as import parameter for conversion
* <--P_IT_CONTRACT converted data from xls to int tab
*----------------------------------------------------------------------*

FORM convert_xls_to_itab USING p_it_type TYPE truxs_t_text_data

CHANGING p_it_contract TYPE ty_t_contract.

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

EXPORTING

i_field_seperator = cns_x
i_line_header = cns_x
i_tab_raw_data = p_it_type
i_filename = p_file
 

TABLES

i_tab_converted_data = p_it_contract

EXCEPTIONS

conversion_failed = 1.
 

IF sy-subrc = 1.

   MESSAGE e021(z12_psv1).

ENDIF.
 

ENDFORM. " convert_xls_to_itab

*&---------------------------------------------------------------------*
*& Form build_and_processing_bapi
*&---------------------------------------------------------------------*
* BAPI Usage
*----------------------------------------------------------------------*
* -->P_IT_CONTRACT converted int table from xls file
*----------------------------------------------------------------------*

FORM build_and_processing_bapi USING p_it_contract TYPE ty_t_contract.

** local variable for splitting entered dates

DATA: l_mm TYPE char2,

l_dd TYPE char2,
l_yyyy TYPE char4,
sep TYPE char1 VALUE '/', " separator for splitting date
l_date TYPE char8, " for setting document date
l_date1 TYPE char8, " for setting Validity start date
l_date2 TYPE char8. " for setting validity end date

LOOP AT p_it_contract INTO wa_contract.

** for agreement item no's

l_count = l_count + 10.

** setting document date for internal usage

CLEAR: l_mm, l_dd, l_yyyy, l_date.

SPLIT wa_contract-agree_date AT sep INTO l_mm l_dd l_yyyy

IN CHARACTER MODE.

CONCATENATE l_yyyy l_mm l_dd INTO l_date.

** setting validity start date for internal usage

CLEAR: l_mm, l_dd, l_yyyy, l_date1.

SPLIT wa_contract-agree_date AT sep INTO l_mm l_dd l_yyyy

IN CHARACTER MODE.

CONCATENATE l_yyyy l_mm l_dd INTO l_date1.

** setting validity end date for internal usage

CLEAR: l_mm, l_dd, l_yyyy, l_date2.

SPLIT wa_contract-agree_date AT sep INTO l_mm l_dd l_yyyy

IN CHARACTER MODE.

CONCATENATE l_yyyy l_mm l_dd INTO l_date2.

** assignment of header and details from it_contract for passing it to bapi fm

CLEAR: wa_contract_header, wa_contract_header_x,

wa_contract_detail, wa_contract_detail_x.
wa_contract_header-vendor = wa_contract-vendor.
wa_contract_header-doc_type = wa_contract-agree_type .
wa_contract_header-creat_date = l_date.
wa_contract_header-purch_org = wa_contract-pur_org.
wa_contract_header-pur_group = wa_contract-pur_grp.
wa_contract_header-vper_start = l_date1.
wa_contract_header-vper_end = l_date2.
wa_contract_header-doc_cat = 'K'.
wa_contract_header_x-vendor = cns_x.
wa_contract_header_x-doc_type = cns_x.
wa_contract_header_x-creat_date = cns_x.
wa_contract_header_x-purch_org = cns_x.
wa_contract_header_x-pur_group = cns_x.
wa_contract_header_x-vper_start = cns_x.
wa_contract_header_x-vper_end = cns_x.
wa_contract_header_x-doc_cat = cns_x.
wa_contract_detail-agrmt_item = l_count.
wa_contract_detail-material = wa_contract-material.
wa_contract_detail-plant = wa_contract-plant.
wa_contract_detail-stge_loc = wa_contract-sto_loc.
wa_contract_detail-po_unit = wa_contract-uom.
wa_contract_detail-net_price = wa_contract-net_price.
wa_contract_detail-item_cat = wa_contract-item_cat.
wa_contract_detail-target_qty = wa_contract-qty.

APPEND wa_contract_detail TO it_contract_detail.

wa_contract_detail_x-agrmt_item = cns_x.
wa_contract_detail_x-material = cns_x.
wa_contract_detail_x-plant = cns_x.
wa_contract_detail_x-stge_loc = cns_x.
wa_contract_detail_x-po_unit = cns_x.
wa_contract_detail_x-net_price = cns_x.
wa_contract_detail_x-item_cat = cns_x.
wa_contract_detail_x-target_qty = cns_x.

APPEND wa_contract_detail_x TO it_contract_detail_x.

AT END OF vendor.

PERFORM call_bapi.

ENDAT.

ENDLOOP.

ENDFORM. " build_and_processing_bapi

*&---------------------------------------------------------------------*
*& Form call_bapi
*&---------------------------------------------------------------------*
* calling bapi BAPI_AGREEMENT_CONTRACT
* for batch data transfer in transaction ME31K
*----------------------------------------------------------------------*

FORM call_bapi .

REFRESH it_return.

CALL FUNCTION 'BAPI_AGREEMENT_MAINTAIN'

EXPORTING

im_agreement_header = wa_contract_header
im_agreement_header_x = wa_contract_header_x

TABLES

ext_return = it_return

imt_agreement_items = it_contract_detail
imt_agreement_items_x = it_contract_detail_x.

REFRESH: it_contract_detail, it_contract_detail_x.

CLEAR l_count.

** generating status report for created contract

CLEAR wa_message.

wa_message-vendor = wa_contract_header-vendor.

LOOP AT it_return INTO wa_return.

IF wa_return-type = 'S' or

wa_return-type = 'I'.
wa_message-status = 'Created'.
wa_message-message = 'Contract Created Successfully'.

ELSE.

wa_message-status = 'Not Created'.
wa_message-message = wa_return-message..

ENDIF.

APPEND wa_message TO it_message.

ENDLOOP.

ENDFORM. " call_bapi

*&---------------------------------------------------------------------*
*& Form get_file_name
*&---------------------------------------------------------------------*
* Getting file path on pressing F4 for help
*----------------------------------------------------------------------*
* -->P_P_FILE file path from selection screen
*----------------------------------------------------------------------*

FORM get_file_name USING p_p_file TYPE rlgrap-filename.

CALL FUNCTION 'KD_GET_FILENAME_ON_F4'

EXPORTING

program_name = syst-repid
dynpro_number = syst-dynnr
field_name = p_p_file

* STATIC = ' '
* MASK = ' '

CHANGING

file_name = p_p_file.

ENDFORM. " get_file_name

*&---------------------------------------------------------------------*
*& Form get_return_messages
*&---------------------------------------------------------------------*
* Writting report containing status of contract
*----------------------------------------------------------------------*

FORM get_return_messages .

FORMAT INVERSE ON.

FORMAT COLOR 3 ON.

WRITE:/3(118) sy-uline.

WRITE:/3 sy-vline, 5 'VENDOR', 18 sy-vline, 20 'STATUS',

43 sy-vline, 45 'MESSAGE', 120 sy-vline,

/3(118) sy-uline.

FORMAT COLOR OFF.

FORMAT COLOR 5 ON.

LOOP AT it_message INTO wa_message.

WRITE:/3 sy-vline, 5 wa_message-vendor, 18 sy-vline,

20 wa_message-status, 43 sy-vline, 45 wa_message-message,

120 sy-vline,

/3(118) sy-uline.

ENDLOOP.

FORMAT COLOR OFF.

FORMAT INVERSE OFF.

ENDFORM. " get_return_messages

*&---------------------------------------------------------------------*
*& Form write_heading
*&---------------------------------------------------------------------*
* Writting heading of status report
*----------------------------------------------------------------------*

FORM write_heading .

WRITE:/40 'Status Report for Vendor Contract Creation'.

WRITE:/3(118) sy-uline.

SKIP 2.

ENDFORM. " write_heading

ABAP Tips

 

See Also
Estimate Guidelines for ABAP programs

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Main Index
SAP Basis, ABAP Programming and Other IMG Stuff

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.