Data uploading in MD61 using Bapi in SAP ABAP
DATA UPloading in SAP ABAP
Input Screen
ABAP Code Here :
REPORT zpp_md61_upload.
TYPE-POOLS : truxs .
TYPES : BEGIN OF t_excel_data , " structure for excel
planno TYPE pbdnr , " req Plan no
werks TYPE werks_d , " Plant
date_type TYPE entlu , " Period(date type)
matnr TYPE matnr , " material
pquan TYPE plnmg , " planned quantity
req_type TYPE bedae , " Requirement type
date TYPE edatu , " date
version TYPE versb , " vrsion
END OF t_excel_data .
DATA : gs_req_item TYPE bapisitemr,
gs_items TYPE bapisshdin,
gt_req_item TYPE TABLE OF bapisitemr,
gt_items TYPE TABLE OF bapisshdin.
DATA : gt_return LIKE bapiret1 OCCURS 0 WITH HEADER LINE.
DATA: gs_excel_data TYPE t_excel_data, " work area for excel data
gt_excel_data TYPE TABLE OF t_excel_data, " internal dataa for excel data
gt_raw_data TYPE truxs_t_text_data . " raw data .
DATA: gs_excel_data1 TYPE t_excel_data, " work area for excel data
gt_excel_data1 TYPE TABLE OF t_excel_data. " internal dataa for excel data
"SELECTION-SCREEN
SELECTION-SCREEN : BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001 .
PARAMETERS : p_file TYPE rlgrap-filename OBLIGATORY. " file name
SELECTION-SCREEN : END OF BLOCK b1 .
" f4 help event
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file .
PERFORM f4_filename . "subroutine to perform f4 help
START-OF-SELECTION .
PERFORM upload_excel . "subroutine to uplaod excel file .
PERFORM req_create . " subroutine for using bapi to update md61 .
*&---------------------------------------------------------------------*
*& Form F4_FILENAME
*&---------------------------------------------------------------------*
FORM f4_filename .
CALL FUNCTION 'F4_FILENAME' " function module to perform f4 help
IMPORTING
file_name = p_file.
.
ENDFORM. " F4_FILENAME
*&---------------------------------------------------------------------*
*& Form UPLOAD_EXCEL
*&---------------------------------------------------------------------*
FORM upload_excel .
REFRESH gt_excel_data .
IF NOT p_file IS INITIAL .
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP' " Function module to upload excel data into internal table
EXPORTING
* I_FIELD_SEPERATOR =
i_line_header = 'X'
i_tab_raw_data = gt_raw_data
i_filename = p_file
TABLES
i_tab_converted_data = gt_excel_data
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
ENDFORM. " upload_excel
*&---------------------------------------------------------------------*
*& Form REQ_CREATE
*&---------------------------------------------------------------------*
FORM req_create .
* Constants for period indicator
CONSTANTS : c_c TYPE c VALUE 'C', " Calender Format
c_k TYPE c VALUE 'K', " Calender Format
c_p TYPE c VALUE 'P', " Period Format
c_d TYPE c VALUE 'D', " day
c_m TYPE c VALUE 'M' . " month
CONSTANTS : c_1 TYPE c VALUE '1' , " Day
c_2 TYPE c VALUE '2' , " Week
c_3 TYPE c VALUE '3' , " Month
c_4 TYPE c VALUE '4' , " Posting period
c_5 TYPE c VALUE '5' . " Planning Calender Period
REFRESH : gt_items , gt_excel_data1 .
gt_excel_data1[] = gt_excel_data[].
SORT gt_excel_data1 BY planno werks date_type matnr.
DELETE ADJACENT DUPLICATES FROM gt_excel_data1 COMPARING planno werks date_type matnr .
* LOOP AT gt_excel_data1 INTO gs_excel_data1.
LOOP AT gt_excel_data INTO gs_excel_data
.
* CASE gs_excel_data-date_type.
*
* WHEN c_c OR c_k OR c_p.
* gs_excel_data-date_type = c_5.
* WHEN c_d .
* gs_excel_data-date_type = c_1.
* WHEN c_m.
* gs_excel_data-date_type = c_3.
* WHEN OTHERS.
* gs_excel_data-date_type = c_2.
* ENDCASE.
* Req Items
gs_req_item-material = gs_excel_data-matnr . " Passing MAterial to req items in Bapi from excel IT
gs_req_item-plant = gs_excel_data-werks . " Passing MAterial to req items in Bapi from excel IT
* gs_req_item-version = gs_excel_data-version .
gs_req_item-req_number = gs_excel_data-planno . " Passing requirement Plan no to req items in Bapi from excel IT
gs_req_item-requ_type = gs_excel_data-req_type . " Passing requirement type to req items in Bapi from excel IT
gs_req_item-version = '00' . " Version is always 00 .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = gs_req_item-material
IMPORTING
output = gs_req_item-material.
* line items
gs_items-date_type = '3'."gs_excel_data-date_type . " Passing date type to requirements_schedule_in in Bapi from excel IT
* gs_items-req_date = '20150113' .
* gs_items-unit = gs_excel_data-meins .
gs_items-req_qty = gs_excel_data-pquan . " Passing quantity to requirements_schedule_in in Bapi from excel IT
gs_items-req_date = gs_excel_data-date . " Passing date to requirements_schedule_in in Bapi from excel IT
APPEND gs_items TO gt_items .
PERFORM create_change . " Perform to create and change data in MD61 & MD62
CLEAR : gs_items , gs_excel_data , gt_items .
CLEAR : gs_req_item , gt_items .
ENDLOOP .
* CLEAR : gs_excel_data1 .
* ENDLOOP.
PERFORM messages . " Perform to display messages
ENDFORM. " REQ_CREATE
*&---------------------------------------------------------------------*
*& Form CREATE_CHANGE
*&---------------------------------------------------------------------*
FORM create_change .
BREAK exzaabap1.
CALL FUNCTION 'BAPI_REQUIREMENTS_CREATE' " Bapi to Create Planned Independent Reqmts
EXPORTING
requirements_item = gs_req_item
* REQUIREMENT_PARAM =
do_commit = 'X'
update_mode = 'X'
* REFER_TYPE = ' '
* PROFILID = ' '
* IMPORTING
* MATERIAL =
* PLANT =
* REQUIREMENTSTYPE =
* VERSION =
* REQMTSPLANNUMBER =
* MRP_AREA =
* MATERIAL_EVG =
* REQ_PLAN_ID_EXT =
* REQ_SEGMENT =
* IMPORTING
* MATERIAL = gs_req_item-material
TABLES
requirements_schedule_in = gt_items
* REQUIREMENTS_CHAR_IN =
return = gt_return.
* BREAK-POINT.
READ TABLE gt_return INTO gt_return WITH KEY type = 'E' id = '6P' number = '011'. " in case if the data already
"exists
IF sy-subrc IS INITIAL.
CALL FUNCTION 'BAPI_REQUIREMENTS_CHANGE'
EXPORTING
material = gs_req_item-material
plant = gs_req_item-plant
requirementstype = gs_req_item-requ_type
version = gs_req_item-version
reqmtsplannumber = gs_req_item-req_number
vers_activ = 'X'
delete_old = ' '
TABLES
requirements_schedule_in = gt_items
return = gt_return.
READ TABLE gt_return INTO gt_return WITH KEY type = 'E' id = '6P' number = '012'. " in case if the requirement is blocked
IF sy-subrc IS INITIAL.
WAIT UP TO 5 SECONDS.
CALL FUNCTION 'BAPI_REQUIREMENTS_CHANGE'
EXPORTING
material = gs_req_item-material
plant = gs_req_item-plant
requirementstype = gs_req_item-requ_type
version = gs_req_item-version
reqmtsplannumber = gs_req_item-req_number
vers_activ = 'X'
delete_old = ' '
TABLES
requirements_schedule_in = gt_items
return = gt_return.
ENDIF.
ENDIF .
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ENDFORM. " CREATE_CHANGE
*&---------------------------------------------------------------------*
*& Form MESSAGES
*&---------------------------------------------------------------------*
FORM messages .
IF gt_return IS INITIAL . " For Success message
MESSAGE 'Forecast is created or updated Successfully' TYPE 'S' .
ENDIF .
IF gt_return IS NOT INITIAL .
LOOP AT gt_return . " For error message to be displayed
WRITE : / gt_return-id ,
10 gt_return-number ,
20 gt_return-type ,
30 gt_return-message .
ENDLOOP .
ENDIF.
ENDFORM. " MESSAGES
Note : If you upload data in MD61 Tcode monthly, your date field value
must be first day of this month. Otherwise this month will be twice.
Comments
Post a Comment