Application Development 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: 

Bapi Usage in RFC Function Modules

0 Kudos

Hie there,

Can someone sent me sample code for the usage of a BAPI in a Function.

I want ot develop an RFC whereby i pass an Account number to it and get the BP addressess.

The structures which i intend to use for this functionality is

1. Tablble say T_XXXX (this contains the Accnt Number )

2. from this Account number i intend to intend to pass it into a BAPI say BAPI_BCA_XXXXXX1

Import

Accnt Number

Export

Accnt number

BP Number

from this result of this Bapi i intend to use the result above

i.e BP #

in another BApi BAPI _ADRESS

Import

BP #

Export

AddressData.

This structure ADDRESSDATA is the overall structure which should come out from the RFC Function Module.

Sample code for similar scenarios will be appreciated.

Points rewarded

2 REPLIES 2

Former Member
0 Kudos

function zvp_bapi_goodsmvt_create.

*"----


""Local Interface:

*" IMPORTING

*" VALUE(PSTNGDATE) TYPE BUDAT

*" VALUE(DOCDATE) TYPE BUDAT

*" VALUE(GM_CODE) TYPE GM_CODE

*" VALUE(REF_DOC_NO) TYPE XBLNR

*" VALUE(GOODSMVT_HEADER) LIKE BAPI2017_GM_HEAD_01 STRUCTURE

*" BAPI2017_GM_HEAD_01

*" VALUE(GOODSMVT_CODE) TYPE BAPI2017_GM_CODE

*" VALUE(TESTRUN) LIKE BAPI2017_GM_GEN-TESTRUN DEFAULT SPACE

*" VALUE(GOODSMVT_REF_EWM) LIKE /SPE/BAPI2017_GM_REF_EWM STRUCTURE

*" /SPE/BAPI2017_GM_REF_EWM OPTIONAL

*" EXPORTING

*" VALUE(GOODSMVT_HEADRET) LIKE BAPI2017_GM_HEAD_RET STRUCTURE

*" BAPI2017_GM_HEAD_RET

*" VALUE(MATERIALDOCUMENT) TYPE BAPI2017_GM_HEAD_RET-MAT_DOC

*" VALUE(MATDOCUMENTYEAR) TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR

*" TABLES

*" GOODSMVT_ITEM STRUCTURE BAPI2017_GM_ITEM_CREATE

*" GOODSMVT_SERIALNUMBER STRUCTURE BAPI2017_GM_SERIALNUMBER

*" OPTIONAL

*" RETURN STRUCTURE BAPIRET2

*" GOODSMVT_SERV_PART_DATA STRUCTURE

*" /SPE/BAPI2017_SERVICEPART_DATA OPTIONAL

*" EXTENSIONIN STRUCTURE BAPIPAREX OPTIONAL

*" GOODSMVT_ITEM_CWM STRUCTURE /CWM/BAPI2017_GM_ITEM_CREATE

*" OPTIONAL

*"----


goodsmvt_header-pstng_date = pstngdate.

goodsmvt_header-doc_date = docdate.

"APPEND goodsmvt_header.

goodsmvt_header-ref_doc_no = ref_doc_no.

goodsmvt_code-gm_code = gm_code.

"APPEND goodsmvt_code.

call function 'BAPI_GOODSMVT_CREATE'

exporting

goodsmvt_header = goodsmvt_header

goodsmvt_code = goodsmvt_code

testrun = testrun

goodsmvt_ref_ewm = goodsmvt_ref_ewm

importing

goodsmvt_headret = goodsmvt_headret

materialdocument = materialdocument

matdocumentyear = matdocumentyear

tables

goodsmvt_item = goodsmvt_item

goodsmvt_serialnumber = goodsmvt_serialnumber

return = return

goodsmvt_serv_part_data = goodsmvt_serv_part_data

extensionin = extensionin.

commit work.

if not return-type = 'E' .

update zvp_vendespatch set vdflag = 'X'

where ebeln = goodsmvt_item-po_number

and xblnr = goodsmvt_header-ref_doc_no.

if sy-subrc = 0.

return-type = 'I'.

return-message = 'Record updated Succesfully'.

append return.

clear return.

else.

return-type = 'E'.

return-message = 'Record not updated '.

append return.

endif.

else.

if return-type = 'E'.

return-type = 'I'.

return-message = 'Document not posted'.

append return.

endif.

endif.

endfunction.

hi i have called this function module to update the z table, have a look at this ,

i think u ll get some idea

Former Member
0 Kudos

Hi

See this steps and do accordingly

RFC (Remote Function Call) is similar to the general SAP fun module: except that in the attributes you click the radio button: RFC enabled;

and you will be passing an Import parameter DESTINATION to it.

Other code and usage will be similar to any fun module;

Have a look at any fun module in SE37 to understand better about the different components of Fun modules;

Refer this link:

http://help.sap.com/saphelp_nw04/helpdata/en/22/042518488911d189490000e829fbbd/frameset.htm

BAPI

BAPI Step by step Guidance

BAPI stands for Business Application Programming Interface.

There are 5 different steps in BAPI.

- Create BAPI Structure

- Create BAPI Function Module or API Method.

- Create BAPI object

- Release BAPI Function Module.

- Release BAPI object.

Step1.

Creating BAPI Structure:

- Go to <SE11>.

- Select Data Type & Enter a name.

- Click on Create.

- Note: Always BAPI should be in a development class with request number (Not Local Object).

- Select Structure & hit ENTER.

- Enter the fields from your database. Make sure that the first field is the Primary Key Field.

- Then SAVE & ACTIVATE.

Step 2.

Creating BAPI module:

- Enter TR.CODE <SE37>.

- Before entering any thing, from the present screen that you are in, select the menu

Goto -> Function Groups -> Create Group.

Enter a name (Note: This name Must start with ZBAPI)

Let this screen be as it is and open another window and there, enter TR.CODE <SE80).

Click on the Third ICON that says Inactive Objects.

Select the group that you just created and click on Activate.

Notice that the group you created will disappear from the list of inactive objects.

- Go back to ><SE37> screen and enter a name and hit <ENTER>. Then enter the group name that you just created and activated.

NOTE: When you release a function module the respective group will be attached to that particular application. It cannot be used for any other application. NEVER include an already existing group that is attached to another module.

Now click on the first Tab that says [ATTRIBUTES] and select the radio button that says remote-enabled module since we will be accessing this from any external system.

Then click on the second tab that says [IMPORT].

Enter a PARAMETER NAME, TYPE and the structure you created in the first step. Also select the check box ‘Pa’. All remotely enabled functional modules MUST be Pa enabled, where Pa means ‘Passed by Value’ and if you don’t select ‘Pa’, then that means it will be passed by reference..

Then click on tab that says [EXPORT].

Enter the following as is in the first three fields

RETURN TYPE BAPIRETURN (These 3 field values are always same)

Here also select ‘Pa’ meaning Pass by value.

Note: BAPIRETURN contains structure with message fields.

Then SAVE and ACTIVATE.

Step 3.

Creating BAPI object:

- Enter Tr.Code <SWO1> (Note. It is letter ‘O’ and not Zero).

- Enter a name and then click on create. Enter details.

NOTE: Make sure that that Object Type and Program name are SAME.

- Enter Application ‘M’, if you are using standard table Mara. If you are using your own database then select ‘Z’ at the bottom.

- Then hit <ENTER>.

- Now we have to add ‘Methods’. High light METHODS and then select the following from the menu:

Goto Utilities -> API Methods -> Add Methods.

- Enter function Module name and hit <ENTER>.

- Select the second FORWARD ARROW button (>)to go to next step.

- Check if every thing looks ok and again click on FORWARD ARROW button (>).

- Then select ‘YES’ and click on <SAVE>.

- Now on a different screen goto TR.CODE <SE37>. Enter Function Module name and select from the top menu Function Module -> Release -> Release.

- Goback to TR.CODE <SWO1>.

Here select the menu combination shown below in the same order.

- Edit -> Change Release Status -> Object Type Component -> To Implemented.

- Edit -> Change Release Status -> Object Type Component -> To Released.

- Edit -> Change Release Status -> Object Type -> To Implemented.

- Edit -> Change Release Status -> Object Type -> To Released.

- Then click on <SAVE>.

- Then click on Generate Button (4th button from left hand side looks like spinning wheel).

- Then Click on the button that says ‘PROGRAM’ to see the source code.

To check if this is present in work flow goto TR.CODE <BAPI>.

Here it shows business object repository.

- First click on the middle button and then select “ALL” and hit ENTER.

- Goto tab [ALPHABETICAL] and look for the object that you created. This shows that the BAPI object has been created successfully.

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.

Reward if useful

Regards

Anji