0% found this document useful (0 votes)
334 views

Bapi

This document defines data structures and variables used to upload a flat file into internal tables, populate BAPI structures, and call BAPI functions to create sales documents in SAP. Key points: - It uploads a flat file into an internal table using a function module. - It copies data from the internal table to another table to translate fields to uppercase. - It populates header and item BAPI structures by looping through the internal table and clearing errors and BAPI tables between each loop iteration. - It calls BAPI functions to create sales documents for each record in the internal table.

Uploaded by

dhivya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
334 views

Bapi

This document defines data structures and variables used to upload a flat file into internal tables, populate BAPI structures, and call BAPI functions to create sales documents in SAP. Key points: - It uploads a flat file into an internal table using a function module. - It copies data from the internal table to another table to translate fields to uppercase. - It populates header and item BAPI structures by looping through the internal table and clearing errors and BAPI tables between each loop iteration. - It calls BAPI functions to create sales documents for each record in the internal table.

Uploaded by

dhivya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

ZABAP_MATERIAL20

*&---------------------------------------------------------------------*
*& Report ZABAP_MATERIAL20
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
report

zabap_material20.

data: begin of t_flatfile occurs 0,


*
bstkd TYPE char35, "purchase order " at new statement is used on
*this field so plesae don't change 1st field order
auart type char4, "Order type
vkorg type char4, "Sales organization
vtweg type char2, "Distri.Channel
spart type char2, "Division
partn_role type char2, "Partner Role
sold_to_party type char10, " sold_to_party
*

*
*
*

ship_to_party TYPE char10,0


bstkd type char35, "purchase order
bstdk type vbak-bstdk,
" PO DATE
ketdat type vbkd-prsdt, " Req.Del Date
prsdt type vbkd-prsdt,
"Pricing Date
lifsk type vbak-lifsk, "Delivery block (document header)
faksk type vbak-faksk, "Billing block in SD document
bsark type vbak-bsark, "Customer purchase order type
zterm type vbkd-zterm,"Sales Document: Business Data
INCO1 type VBKD-INCO1,"Incoterms (part 1)
INCO2 type VBKD-INCO2,"Incoterms (part 2)
itm_number type char6, "item number
material type char18, "material
plant type char4,
"plant
store_loc type char4, "Storage location
target_qty type char13, "quantity
batch type char10,
"batch
prc_group2 type char3, "material group2
mvgr3 type vbap-mvgr3, "Material group 3
item_categ type char4, "item category

mvgr2 TYPE vbap-mvgr2, "Material group 2


etdat type rv45a-etdat, "Schedule line date
lifsp type lifsp_ep, "Schedule line blocked for delivery
abgru type abgru, "Reason For Rejection.
end of t_flatfile.

*DATA: t_flatfile1 LIKE t_flatfile OCCURS 0 WITH HEADER LINE.

data:begin of t_flatfile1 occurs 0,


bstkd type char35, "purchase order " at new statement is used on
*this field so plesae don't change 1st field order
auart type char4, "Order type
vkorg type char4, "Sales organization
vtweg type char2, "Distri.Channel
spart type char2, "Division
partn_role type char2, "Partner Role
sold_to_party type char10, " sold_to_party
*
*

*
*
*

ship_to_party TYPE char10,


bstkd TYPE char35, "purchase order
bstdk type vbak-bstdk,
" PO DATE
ketdat type vbkd-prsdt, " Req.Del Date
prsdt type vbkd-prsdt,
"Pricing Date
lifsk type vbak-lifsk, "Delivery block (document header)
faksk type vbak-faksk, "Billing block in SD document
bsark type vbak-bsark, "Customer purchase order type
zterm type vbkd-zterm,"Sales Document: Business Data
INCO1 type VBKD-INCO1,"Incoterms (part 1)
INCO2 type VBKD-INCO2,"Incoterms (part 2)
itm_number type char6,
material type char18,
plant type char4,
store_loc type char4,
target_qty type char13,
batch type char10,
prc_group2 type char3, "material group2
mvgr3 type vbap-mvgr3, "Material group 3
item_categ type char4,

mvgr2 TYPE vbap-mvgr2, "Material group 2


etdat type rv45a-etdat, "Schedule line date
lifsp type lifsp_ep, "Schedule line blocked for delivery
abgru type abgru, "Reason For Rejection.
end of t_flatfile1.

data:w_fname1 like ibipparms-path,


w_filename1 type string,
w_flag type char1.
data:w_salesdocument like bapivbeln-vbeln.
data:begin of t_salesdocument occurs 0,
vbeln like vbak-vbeln,
auart type vbak-auart,
vkorg type vbak-vkorg,
vtweg type vbak-vtweg,
spart type vbak-spart,
bstkd type vbak-bstnk,
message(220) type c,

end of t_salesdocument.
data:begin of t_salesdocument_error occurs 0,
vbeln type vbak-vbeln,
auart type vbak-auart,
vkorg type vbak-vkorg,
vtweg type vbak-vtweg,
spart type vbak-spart,
bstkd type vbak-bstnk,
message(220) type c,
end of t_salesdocument_error.
data:t_errormessages like bapiret2 occurs 0 with header line.
*BAPI STRUCTURES DECLERATIONS
data: l_order_header_in like bapisdhd1,
l_order_header_inx like bapisdhd1x.
data:t_return like bapiret2 occurs 0 with header line,
t_order_partners like bapiparnr occurs 0 with header line,
t_order_items_in like bapisditm occurs 0 with header line,
t_order_items_inx like bapisditmx occurs 0 with header line,
t_order_schedules_in like bapischdl occurs 0 with header line,
t_order_schedules_inx like bapischdlx occurs 0 with header line.

selection-screen begin of block b1 with frame.


parameters p_file1 like ibipparms-path." obligatory.
selection-screen end of block b1.
********** search help for upload file***********
at selection-screen on value-request for p_file1.
clear w_fname1.
call function 'F4_FILENAME'
exporting
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name
= 'P_FILE1'
importing
file_name
= w_fname1.
clear p_file1.
p_file1 = w_fname1.
clear w_filename1.
w_filename1 = p_file1.
*validations for upload file name.
if p_file1 is initial.
message 'Make an Entry in required fields' type 'S'.
exit.

endif.

if not p_file1 is initial.


search p_file1 for '.TXT'.
if sy-subrc <> 0.
clear p_file1.
message 'Select Text Tab Delimited file format only!'
type 'S'.
else.
message 'Press Execute Button !' type 'S'.
endif.
endif.

clear t_flatfile.
refresh t_flatfile.

start-of-selection.
*function module for upload into internal table
call function 'GUI_UPLOAD'
exporting
filename
= w_filename1
filetype
= 'ASC'
has_field_separator
= 'X'
read_by_line
= 'X'
dat_mode
= 'X'
tables
data_tab
= t_flatfile
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.
if sy-subrc <> 0.

message id sy-msgid type sy-msgty number sy-msgno


with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
clear: t_salesdocument,t_flatfile1.
refresh: t_salesdocument,t_flatfile1.
if not t_flatfile[] is initial.

loop at t_flatfile.
t_flatfile1-sold_to_party =

t_flatfile-sold_to_party.

t_flatfile1-bstkd = t_flatfile-bstkd.
t_flatfile1-auart = t_flatfile-auart.
t_flatfile1-vkorg = t_flatfile-vkorg.
t_flatfile1-vtweg = t_flatfile-vtweg.
t_flatfile1-spart = t_flatfile-spart.
t_flatfile1-partn_role = t_flatfile-partn_role.
t_flatfile1-sold_to_party = t_flatfile-sold_to_party .
t_flatfile1-itm_number = t_flatfile-itm_number.
t_flatfile1-material = t_flatfile-material.
t_flatfile1-plant = t_flatfile-plant.
t_flatfile1-store_loc = t_flatfile-store_loc.
t_flatfile1-target_qty = t_flatfile-target_qty.
t_flatfile1-batch = t_flatfile-batch.
t_flatfile1-prc_group2 = t_flatfile-prc_group2.
t_flatfile1-mvgr3 = t_flatfile-mvgr3.
t_flatfile1-item_categ = t_flatfile-item_categ .
t_flatfile1-bstkd = t_flatfile-bstkd.
t_flatfile1-prsdt = t_flatfile-prsdt.
t_flatfile1-bstdk = t_flatfile-bstdk.
t_flatfile1-ketdat = t_flatfile-ketdat.
t_flatfile1-etdat = t_flatfile-etdat. "Schedule line date
t_flatfile1-lifsp = t_flatfile-lifsp.
t_flatfile1-abgru = t_flatfile-abgru.
t_flatfile1-lifsk = t_flatfile-lifsk.
t_flatfile1-faksk = t_flatfile-faksk.
t_flatfile1-bsark = t_flatfile-bsark.
translate:t_flatfile1-sold_to_party to upper case,
t_flatfile1-bstkd to upper case,
t_flatfile1-auart to upper case,
t_flatfile1-vkorg to upper case,
t_flatfile1-vtweg to upper case,
t_flatfile1-spart to upper case,
t_flatfile1-partn_role to upper case,
t_flatfile1-itm_number to upper case,
t_flatfile1-material to upper case,
t_flatfile1-plant to upper case,
t_flatfile1-store_loc to upper case,
t_flatfile1-target_qty to upper case,
t_flatfile1-batch to upper case,
t_flatfile1-prc_group2 to upper case,

t_flatfile1-item_categ to upper case,


t_flatfile1-lifsp to upper case,
t_flatfile1-abgru to upper case,
t_flatfile1-lifsk to upper case,
t_flatfile1-faksk to upper case,
t_flatfile1-bsark to upper case,
t_flatfile1-mvgr2 TO UPPER CASE,
t_flatfile1-mvgr3 to upper case.

append t_flatfile1.
clear t_flatfile1.
endloop.
*Fill Bapi Structures.
*sort t_flatfile1.
data: l_flatfile1 like t_flatfile1.
clear:t_order_items_in,t_order_items_inx,t_order_partners,
t_errormessages,t_order_schedules_in,t_order_schedules_inx.
refresh:t_order_items_in,t_order_items_inx,t_order_partners,
t_errormessages,t_order_schedules_in,t_order_schedules_inx.

loop at t_flatfile1.
clear l_flatfile1.
l_flatfile1 = t_flatfile1.
at new bstkd.
clear w_flag.
w_flag = 'X'.
*Header Data
clear l_order_header_in.
l_order_header_in-doc_type = l_flatfile1-auart.
l_order_header_in-sales_org = l_flatfile1-vkorg.
l_order_header_in-distr_chan = l_flatfile1-vtweg.
l_order_header_in-division = l_flatfile1-spart.
l_order_header_in-purch_no_c = l_flatfile1-bstkd.
l_order_header_in-purch_date = l_flatfile1-bstdk.
l_order_header_in-req_date_h = l_flatfile1-ketdat.
l_order_header_in-price_date = l_flatfile1-prsdt.
l_order_header_in-po_method = l_flatfile1-bsark.
l_order_header_in-dlv_block = l_flatfile1-lifsk.
l_order_header_in-bill_block = l_flatfile1-faksk.

l_order_header_inx-updateflag = 'I'.
l_order_header_inx-doc_type = 'X'.
l_order_header_inx-sales_org = 'X'.
l_order_header_inx-distr_chan = 'X'.
l_order_header_inx-division = 'X'.
l_order_header_inx-purch_no_c = 'X'.
l_order_header_inx-purch_date = 'X'.
l_order_header_inx-req_date_h = 'X'.
l_order_header_inx-price_date = 'X'.
l_order_header_inx-po_method = 'X'.
l_order_header_inx-dlv_block = 'X'.
l_order_header_inx-bill_block = 'X'.

endat.
if w_flag = 'X'.
*Partners Funtctions Data
clear t_order_partners.
t_order_partners-partn_role = l_flatfile1-partn_role.
t_order_partners-partn_numb = l_flatfile1-sold_to_party.
*
t_order_partners-ITM_NUMBER = l_flatfile1-itm_number.
append t_order_partners.
clear t_order_partners.

*Item Data
clear t_order_items_in.
*
REFRESH t_order_items_in.
t_order_items_in-itm_number = l_flatfile1-itm_number.
t_order_items_in-material = l_flatfile1-material.
t_order_items_in-plant
= l_flatfile1-plant.
t_order_items_in-store_loc = l_flatfile1-store_loc.
t_order_items_in-target_qty = l_flatfile1-target_qty.
t_order_items_in-batch = l_flatfile1-batch.
t_order_items_in-prc_group2 = l_flatfile1-prc_group2.
t_order_items_in-item_categ = l_flatfile1-item_categ.
*

t_order_items_in-PRC_GROUP2 = l_flatfile1-mvgr2.
t_order_items_in-prc_group3 = l_flatfile1-mvgr3.
t_order_items_in-reason_rej = l_flatfile1-abgru.
append t_order_items_in.
clear t_order_items_in.
clear t_order_items_inx.
REFRESH t_order_items_inx.
t_order_items_inx-updateflag = 'I'.
t_order_items_inx-itm_number = l_flatfile1-itm_number.
t_order_items_inx-material = 'X'.
t_order_items_inx-plant
= 'X'.
t_order_items_inx-store_loc = 'X'.
t_order_items_inx-target_qty = 'X'.

t_order_items_inx-batch = 'X'.
t_order_items_inx-prc_group2 = 'X'.
if l_flatfile1-item_categ is not initial.
t_order_items_inx-item_categ = 'X'.
endif.
t_order_items_inx-reason_rej = 'X'.
*

t_order_items_inx-PRC_GROUP2 = 'X'.
t_order_items_inx-prc_group3 = 'X'.

append t_order_items_inx.
clear t_order_items_inx.
*

*
*

ORDER_SCHEDULES_IN
t_order_schedules_in-itm_number
t_order_schedules_in-sched_line
t_order_schedules_in-req_qty
t_order_schedules_in-req_date =
t_order_schedules_in-req_dlv_bl
append t_order_schedules_in.
clear t_order_schedules_in.

= l_flatfile1-itm_number.
= l_flatfile1-itm_number+1(4).
= l_flatfile1-target_qty .
l_flatfile1-etdat .
= l_flatfile1-lifsp.

ORDER_SCHEDULES_INX
t_order_schedules_inx-itm_number = l_flatfile1-itm_number.
t_order_schedules_inx-sched_line = l_flatfile1-itm_number+1(4).
T_order_schedules_inX-req_qty
= l_flatfile1-target_qty .
t_order_schedules_inx-updateflag = 'I'.
t_order_schedules_inx-req_date = 'X'.
t_order_schedules_inx-req_qty
= 'X'.
t_order_schedules_inx-req_dlv_bl = 'X'.
append t_order_schedules_inx.
clear t_order_schedules_inx.
clear: w_salesdocument,t_return.
refresh:t_return.
endif.
at end of bstkd.

*
*
*

call function 'BAPI_SALESORDER_CREATEFROMDAT2'


exporting
SALESDOCUMENTIN
=
order_header_in
= l_order_header_in
order_header_inx
= l_order_header_inx
SENDER
=
BINARY_RELATIONSHIPTYPE
=

*
*
*
*

*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*

*
*

int_number_assignment
BEHAVE_WHEN_ERROR
LOGIC_SWITCH
TESTRUN
CONVERT
importing
salesdocument
tables
return
order_items_in
order_items_inx
order_partners
order_schedules_in
order_schedules_inx
ORDER_SCHEDULES_IN
ORDER_SCHEDULES_INX
ORDER_CONDITIONS_IN
ORDER_CONDITIONS_INX
ORDER_CFGS_REF
ORDER_CFGS_INST
ORDER_CFGS_PART_OF
ORDER_CFGS_VALUE
ORDER_CFGS_BLOB
ORDER_CFGS_VK
ORDER_CFGS_REFINST
ORDER_CCARD
ORDER_TEXT
ORDER_KEYS
EXTENSIONIN
PARTNERADDRESSES
.

'X'
=
=
=
= ' '
= w_salesdocument
= t_return
= t_order_items_in[]
= t_order_items_inx[]
= t_order_partners[]
= t_order_schedules_in[]
= t_order_schedules_inx[]
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=

LOOP AT t_return. " WHERE type = 'S'.


if t_return-type = 'S'.
call function 'BAPI_TRANSACTION_COMMIT'
exporting
wait
= 'X'
IMPORTING
RETURN
=
.
t_salesdocument-vbeln = w_salesdocument.
t_salesdocument-auart = l_flatfile1-auart.
t_salesdocument-vkorg = l_flatfile1-vkorg.
t_salesdocument-vtweg = l_flatfile1-vtweg.
t_salesdocument-spart = l_flatfile1-spart.

t_salesdocument-bstkd = l_flatfile1-bstkd.
t_salesdocument-message = t_return-message.
APPEND t_salesdocument_error.
append t_salesdocument.
elseif t_return-type = 'E'.

endif.
loop at t_return where type = 'E'.
t_errormessages-type = t_return-type.
t_errormessages-message = t_return-message.
append t_errormessages.
clear t_errormessages.
t_salesdocument_error-vbeln
t_salesdocument_error-auart
t_salesdocument_error-vkorg
t_salesdocument_error-vtweg
t_salesdocument_error-spart

=
=
=
=
=

w_salesdocument.
l_flatfile1-auart.
l_flatfile1-vkorg.
l_flatfile1-vtweg.
l_flatfile1-spart.

t_salesdocument_error-bstkd = l_flatfile1-bstkd.
t_salesdocument_error-message = t_return-message.
append t_salesdocument_error.
clear t_salesdocument_error.
endloop.
*

ENDLOOP.
clear:t_order_partners,t_order_items_in,w_flag,
t_order_items_inx,t_order_schedules_in,t_order_schedules_inx
.
refresh: t_order_partners,t_order_items_in,t_order_items_inx,
t_order_schedules_in,t_order_schedules_inx.
endat.
endloop.

*BAPI_SALESORDER_CREATEFROMDAT2
endif.
*END-OF-SELECTION.
if not t_salesdocument[] is initial.
write:/ 'Sales Orders are created'.
uline.
write:/ 'Order Number', 16 'Order Type',
26 'S.Org',31 'D.Chan',37 'Division',
45 'Purch.ordno', 57 'Message' .
loop at t_salesdocument.
write:/ t_salesdocument-vbeln,
16 t_salesdocument-auart,
26 t_salesdocument-vkorg,
31 t_salesdocument-vtweg,
37 t_salesdocument-spart,
45 t_salesdocument-bstkd,
57 t_salesdocument-message.
endloop.

endif.
if t_errormessages[] is not initial.
write:/ ' Error Messages'.
uline.
*
LOOP AT t_errormessages.
*
WRITE:/ t_errormessages-message.
*
ENDLOOP.
write:/ 'Order Number', 16 'Order Type',
26 'S.Org',31 'D.Chan',37 'Division',
45 'Purch.ordno', 57 'Message' .
loop at t_salesdocument_error.
write:/ t_salesdocument_error-vbeln,
16 t_salesdocument_error-auart,
26 t_salesdocument_error-vkorg,
31 t_salesdocument_error-vtweg,
37 t_salesdocument_error-spart,
45 t_salesdocument_error-bstkd,
57 t_salesdocument_error-message.
endloop.
endif.

You might also like