Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

BAPI

Former Member
0 Kudos
157

Hi Friends,

I have to create sales order, delivery, and invoice using BAPI. First I have to create sales order based on material number and order quantity …. Based on the sales order created I have to create delivery (I have to enter batch number also in second screen of vl01n).based on the delivery number created I have to create invoice.

How to do all the above steps using BAPI’S if possible please send me sample code.

Thanks for your help.

2 REPLIES 2
Read only

Former Member
0 Kudos
106

Hi

See the sample code

Check this example of creating a sales order and for creating delivery...and PGI

************SALES ORDER INPUT CREATION.

PARAMETERS: p_auart TYPE auart OBLIGATORY.

PARAMETERS: p_vkorg TYPE vkorg OBLIGATORY.

PARAMETERS: p_vtweg TYPE vtweg OBLIGATORY.

PARAMETERS: p_spart TYPE vtweg OBLIGATORY.

PARAMETERS: p_sold TYPE kunnr OBLIGATORY.

PARAMETERS: p_ship TYPE kunnr OBLIGATORY.

*ITEM

PARAMETERS: p_matnr TYPE matnr OBLIGATORY.

PARAMETERS: p_menge TYPE kwmeng OBLIGATORY.

PARAMETERS: p_plant TYPE werks_d OBLIGATORY.

PARAMETERS: p_itcat TYPE pstyv OBLIGATORY.

  • DATA DECLARATIONS.

DATA: v_vbeln LIKE vbak-vbeln.

DATA: header LIKE bapisdhead1.

DATA: headerx LIKE bapisdhead1x.

DATA: item LIKE bapisditem OCCURS 0 WITH HEADER LINE.

DATA: itemx LIKE bapisditemx OCCURS 0 WITH HEADER LINE.

DATA: partner LIKE bapipartnr OCCURS 0 WITH HEADER LINE.

DATA: return LIKE bapiret2 OCCURS 0 WITH HEADER LINE.

DATA: lt_schedules_inx TYPE STANDARD TABLE OF bapischdlx

WITH HEADER LINE.

DATA: lt_schedules_in TYPE STANDARD TABLE OF bapischdl

WITH HEADER LINE.

  • HEADER DATA

header-doc_type = p_auart.

headerx-doc_type = 'X'.

header-sales_org = p_vkorg.

headerx-sales_org = 'X'.

header-distr_chan = p_vtweg.

headerx-distr_chan = 'X'.

header-division = p_spart.

headerx-division = 'X'.

headerx-updateflag = 'I'.

  • PARTNER DATA

partner-partn_role = 'AG'.

partner-partn_numb = p_sold.

APPEND partner.

partner-partn_role = 'WE'.

partner-partn_numb = p_ship.

APPEND partner.

  • ITEM DATA

itemx-updateflag = 'I'.

item-itm_number = '000010'.

itemx-itm_number = 'X'.

item-material = p_matnr.

itemx-material = 'X'.

item-plant = p_plant.

itemx-plant = 'X'.

item-target_qty = p_menge.

itemx-target_qty = 'X'.

item-target_qu = 'EA'.

itemx-target_qu = 'X'.

item-item_categ = p_itcat.

itemx-item_categ = 'X'.

APPEND item.

APPEND itemx.

  • Fill schedule lines

lt_schedules_in-itm_number = '000010'.

lt_schedules_in-sched_line = '0001'.

lt_schedules_in-req_qty = p_menge.

APPEND lt_schedules_in.

  • Fill schedule line flags

lt_schedules_inx-itm_number = '000010'.

lt_schedules_inx-sched_line = '0001'.

lt_schedules_inx-updateflag = 'X'.

lt_schedules_inx-req_qty = 'X'.

APPEND lt_schedules_inx.

  • Call the BAPI

CALL FUNCTION 'BAPI_SALESDOCU_CREATEFROMDATA1'

EXPORTING

sales_header_in = header

sales_header_inx = headerx

IMPORTING

salesdocument_ex = v_vbeln

TABLES

return = return

sales_items_in = item

sales_items_inx = itemx

sales_schedules_in = lt_schedules_in

sales_schedules_inx = lt_schedules_inx

sales_partners = partner.

  • Check the return table.

LOOP AT return WHERE type = 'E' OR type = 'A'.

EXIT.

ENDLOOP.

IF sy-subrc = 0.

WRITE: / 'Error in creating document'.

ELSE.

COMMIT WORK AND WAIT.

WRITE: / 'Document ', v_vbeln, ' created'.

ENDIF.

************DELIVERY CREATION.

DATA: BEGIN OF t_vbap OCCURS 0,

vbeln LIKE vbap-vbeln,

posnr LIKE vbap-posnr,

zmeng LIKE vbap-kwmeng,

matnr LIKE vbap-matnr,

werks LIKE vbap-werks,

END OF t_vbap.

DATA: t_request TYPE STANDARD TABLE OF bapideliciousrequest

WITH HEADER LINE.

DATA: t_created TYPE STANDARD TABLE OF bapideliciouscreateditems

WITH HEADER LINE.

DATA: t_return TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE.

SELECT vbeln posnr zmeng matnr werks

INTO TABLE t_vbap

FROM vbap

WHERE vbeln = v_vbeln.

LOOP AT t_vbap.

t_request-document_numb = t_vbap-vbeln.

t_request-document_item = t_vbap-posnr.

t_request-quantity_sales_uom = t_vbap-zmeng.

t_request-quantity_base__uom = t_vbap-zmeng.

t_request-id = 1.

t_request-document_type = 'A'.

t_request-delivery_date = sy-datum.

t_request-material = t_vbap-matnr.

t_request-plant = t_vbap-werks.

t_request-date = sy-datum.

t_request-goods_issue_date = sy-datum.

t_request-goods_issue_time = sy-uzeit.

APPEND t_request.

ENDLOOP.

CALL FUNCTION 'BAPI_DELIVERYPROCESSING_EXEC'

TABLES

request = t_request

createditems = t_created

return = t_return.

READ TABLE t_return WITH KEY type = 'E'.

IF sy-subrc = 0.

MESSAGE e208(00) WITH 'Delivery creation error'.

ENDIF.

COMMIT WORK.

*

************Post goods issue.

READ TABLE t_created INDEX 1.

DATA: vbkok_wa TYPE vbkok.

vbkok_wa-vbeln_vl = t_created-document_numb.

vbkok_wa-wabuc = 'X'.

DATA: v_error.

CALL FUNCTION 'WS_DELIVERY_UPDATE'

EXPORTING

vbkok_wa = vbkok_wa

delivery = t_created-document_numb

IMPORTING

ef_error_in_goods_issue_0 = v_error.

COMMIT WORK.

see the doc

BAPI-step by step

http://www.sapgenie.com/abap/bapi/example.htm

list of all bapis

http://www.planetsap.com/LIST_ALL_BAPIs.htm

for BAPI's

http://www.sappoint.com/abap/bapiintro.pdf

http://www.sappoint.com/abap/bapiprg.pdf

http://www.sappoint.com/abap/bapiactx.pdf

http://www.sappoint.com/abap/bapilst.pdf

http://www.sappoint.com/abap/bapiexer.pdf

http://service.sap.com/ale

http://service.sap.com/bapi

http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCMIDAPII/CABFAAPIINTRO.pdf

http://help.sap.com/printdocu/core/Print46c/en/data/pdf/CABFABAPIREF/CABFABAPIPG.pdf

http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCFESDE8/BCFESDE8.pdf

http://www.planetsap.com/Bapi_main_page.htm

http://www.topxml.com/sap/sap_idoc_xml.asp

http://www.sapdevelopment.co.uk/

http://www.sapdevelopment.co.uk/java/jco/bapi_jco.pdf

Also refer to the following links..

www.sap-img.com/bapi.htm

www.sap-img.com/abap/bapi-conventions.htm

www.planetsap.com/Bapi_main_page.htm

www.sapgenie.com/abap/bapi/index.htm

Checkout !!

http://searchsap.techtarget.com/originalContent/0,289142,sid21_gci948835,00.html

http://techrepublic.com.com/5100-6329-1051160.html#

Example Code

U need to give the step_nr, item_nr, cond_count and cond_type so the correct conditon will be updated. If no condition exists for the given parameters, a new condition will be created.

U can find these parameters for a particular condition type in table KONV.

&----


*& Form saveTransactionJOCR

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM saveTransactionJOCR .

data: salesdocument like BAPIVBELN-VBELN,

order_header_inx like bapisdh1x,

order_header_in like bapisdh1,

return type standard table of bapiret2 with header line,

conditions_in type standard table of bapicond with header line,

conditions_inx type standard table of bapicondx with header line,

logic_switch like BAPISDLS,

step_nr like conditions_in-cond_st_no,

item_nr like conditions_in-itm_number,

cond_count like conditions_in-cond_count,

cond_type like conditions_in-cond_type.

salesdocument = wa_order_information-VBELN.

LOGIC_SWITCH-COND_HANDL = 'X'.

order_header_inx-updateflag = 'U'.

  • conditions

clear conditions_in[].

clear conditions_inx[].

clear: step_nr,

item_nr,

cond_count,

cond_type.

step_nr = '710'.

item_nr = '000000'.

cond_count = '01'.

cond_type = 'ZCP2'.

CONDITIONS_IN-ITM_NUMBER = item_nr.

conditions_in-cond_st_no = step_nr.

CONDITIONS_IN-COND_COUNT = cond_count.

CONDITIONS_IN-COND_TYPE = cond_type.

CONDITIONS_IN-COND_VALUE = 666.

CONDITIONS_IN-CURRENCY = 'EUR'.

append conditions_in.

CONDITIONS_INX-ITM_NUMBER = item_nr.

conditions_inx-cond_st_no = step_nr.

CONDITIONS_INX-COND_COUNT = cond_count.

CONDITIONS_INX-COND_TYPE = cond_type.

CONDITIONS_INX-UPDATEFLAG = 'U'.

CONDITIONS_INX-COND_VALUE = 'X'.

CONDITIONS_INX-CURRENCY = 'X'.

append conditions_inx.

CALL FUNCTION 'BAPI_SALESORDER_CHANGE'

EXPORTING

SALESDOCUMENT = salesdocument

ORDER_HEADER_IN = order_header_in

ORDER_HEADER_INX = order_header_inx

LOGIC_SWITCH = logic_switch

TABLES

RETURN = return

CONDITIONS_IN = conditions_in

CONDITIONS_INX = conditions_inx

.

if return-type ne 'E'.

commit work and wait.

endif.

ENDFORM. " saveTransactionJOCR

Bdc to Bapi

The steps to be followed are :

1. Find out the relevant BAPI (BAPI_SALESORDER_CHANGE for VA02).

[for VA01 use BAPI_SALESORDER_CREATEFROMDAT2]

2. Create a Z program and call the BAPi (same as a Funtion module call).

2. Now, if you see this BAPi, it has

-> Importing structures.

eg: SALESDOCUMENT: this will take the Sales order header data as input.

-> Tables parameters:

eg: ORDER_ITEM_IN: this will take the line item data as input.

Note :

Only specify fields that should be changed

Select these fields by entering an X in the checkboxes

Enter a U in the UPDATEFLAG field

Always specify key fields when changing the data, including in the checkboxes

The configuration is an exception here. If this needs to be changed, you need to complete it again fully.

Maintain quantities and dates in the schedule line data

Possible UPDATEFLAGS:

U = change

D = delete

I = add

Example

1. Delete the whole order

2. Delete order items

3. Change the order

4. Change the configuration

Notes

1. Minimum entry:

You must enter the order number in the SALESDOCUMENT structure.

You must always enter key fields for changes.

You must always specify the update indicator in the ORDER_HEADER_INX.

2. Commit control:

The BAPI does not run a database Commit, which means that the application must trigger the Commit so that the changes are read to the database. To do this, use the BAPI_TRANSACTION_COMMIT BAPI.

For further details... refer to the Function Module documentation for the BAPi.

Bapi to VB(Visual Basic)

Long back I had used the following flow structure to acheive the same.

Report -> SM59 RFC destination -> COM4ABAP -> VB.exe

my report uses the rfc destination to create a COM session with com4abap. com4abap calls the vb.exe and manages the flow of data between sap and vb exe.

You need to have com4abap.exe

If com4abap is installed you will find it in sapgui installatin directory , C:\Program Files\SAPpc\sapgui\RFCSDK\com4abap.

else refer OSS note 419822 for installation of com4abap

after making the settings in com4abap to point to the vb program and setting up rfc destination in sm59 to point to com4abap session , you can use the following function modules to call the vb code.

for setting up com4abap and rfc destination please refer to the documentation for com4abap.

  • Invoke NEW DCOM session

call function 'BEGIN_COM_SESSION'

exporting

service_dest = service_dest "(this will be a RFC destination created in SM59)

importing

worker_dest = worker_dest

exceptions

connect_to_dcom_service_failed = 1

connect_to_dcom_worker_failed = 2

others = 3.

call function 'create_com_instance' destination worker_dest

exporting

clsid = g_c_clsid

typelib = g_c_typelib

importing

instid = g_f_oid

exceptions

communication_failure = 1 message g_f_msg

system_failure = 2 message g_f_msg

invalid_instance_id = 3

others = 4.

call function 'com_invoke' destination worker_dest

exporting

%instid = g_f_oid

%method = 'UpdatePDF'

sntemp = g_v_const_filent

snsysid = sy-sysid

snflag = 'N'

tables

rssaptable = g_t_pdfdetail1

%return = g_t_pdfdetail1 "t_test

exceptions

communication_failure = 1 message g_f_msg

system_failure = 2 message g_f_msg

invalid_instance_id = 3

others = 4.

then close the com session , using

FM delete_com_instance

FM END_COM_SESSION

<b>Reward points for useful Answers</b>

Regards

Anji