You are on page 1of 17

BAPI STEP BY STEP.

1. About the Example


2. STEP 1 - Define a structures for the BAPI
3. STEP 2 - rite !unction "o#ule
$. STEP 3 - %reate the API "etho# &sin' the BAPI I(A)D
*. STEP $ - !inal steps
About the example
This BAPI reads system status for a production order from table JEST
and system status text from table TJ02T
Name ZGetOrderStatus
Function group ZBAPISTATUS
Function module: Z_BAPI_GET_ORDER_STATUS
Import parameters: ORDER_STATUS_IMPORT tpe
!"API_ORDER_STATUS_IMPORT:
AUFNR Order number (Keyfield)
SPRAS Language
ExcludeInactive - Checkbox - Exclude inactive status
Ta#les T_"APISTAT tpe !"APISTAT:
O"$NR like JE!-O"J#$
STAT like JE!-!%!
INACT like JE!-&#%C!
TXT04 like !J'(!-!)!'*
TXT30 like!J'(!-!)!+'
E%port parameters RETURN like BAPIRETURN
STEP 1 - Defne a structures for
the BAPI
In this step a structures for the parameters and tables of the function
module used for the BAPI are defned
!se Data type -> Structure
,efine the follo-ing structures.
/"%0&1O$,E$1!%!21&30O$! -hich contains the follo-ing fields.
O$,E$&, Order number (Keyfield)
0$% Language
Exclude&nactive - Checkbox - Exclude inactive status
/"%0&!%!.
O"J#$ like JE!-O"J#$
!%! like JE!-!%!
&#%C! like JE!-&#%C!
!)!'* like !J'(!-!)!'*
!)!+' like!J'(!-!)!+'
Important note
4ou -ill have to define a structure for every 5arameter in the "%0&6 4ou cannot
use the same structures used in existing a55lications because "%0& structures
are fro7en -hen "%0&s are released and then there are restrictions on changing
them6
!"API#$R%ER#STATUS#I&P$RT
!"APISTAT
STEP ' ( )rite Function &odule
Important note*.
Each "%0& must have its o-n function grou56
2nder the attributes tab remember to select 0rocessing !y5e Remote Enabled module8
other-ise the function module cannot be invoked via $9C and used as a "%0&
&m5ort:Ex5ort 5arameters can only be "4 ;%L2E for an $9C enabled function module
<e are only creating one "%0& in this exam5le8 but you can create related "%0&s in the
same function 5ool8 so they -ill be able to share global data6
Attri+ute*

Import Parameter*
Export Parameter*

Ta+le*

Code
Note*
!he subroutine E!1$E!2$#13E%=E is a standard routine used for "%0&s that use
the "%0&$E!2$# structure
&n form /1"%0&1=E!1O$,E$14!E31!%!2 there is a test IF 1 = 26 &f the test is
true a message is dis5layed6 !he condition -ill obviously never be true8 and -e -ill never
-ant to dis5lay a message in a "%0&6 !he reason -hy it is included is8 that it create a
reference for the message8 so that the <>E$E 2E, functionality can be used for the
message6 !his is the %0 standard -ay to handle it8 co5ied from the Com5any Code
=etList "%0&6
INC,U%E ,!"APISTATUSUXX
*****************************************************************
* THIS FILE IS GENERATED BY THE FUNCTION LIBRARY. *
* NEVER CHANGE IT MANUALLY, PLEASE! *
*****************************************************************
INCLUDE LZBAPISTATUSU02.
"Z_BAPI_GET_ORDER_SYSTEM_STATUS
INCLUDE LZBAPISTATUSTOP - Global a!a
92#C!&O#-0OOL /"%0&!%!26 ?3E%=E-&, /+
!y5es.
begin of !y5e1t@'(t8
istat like t@'(t-istat8
txt'* like t@'(t-txt'*8
txt+' like t@'(t-txt+'8
end of ty5e1t@'(t6
,%!%.
- %eclaration* .or TA",E parameter
!1"%0&!%! like /"%0&!%! occurs '8
=1"%0&!%! like /"%0&!%!8
- Ta+le .or o+/ect text*
t1t@'(t ty5e ty5e1t@'(t occurs '8
g1t@'(t ty5e ty5e1t@'(t6
- Structure .or return me**a0e*
,%!%.
"E=&# O9 3E%=E8
3=!4 L&KE 4-3=!48
3=&, L&KE 4-3=&,8
3=#O L&KE 4-3=#O8
3=;A L&KE 4-3=;A8
3=;( L&KE 4-3=;(8
3=;+ L&KE 4-3=;+8
3=;* L&KE 4-3=;*8
E#, O9 3E%=E6
INCLUDE LZBAPISTATUSF"# - S$b%o$!&'()
---INC,U%E ,!"APISTATUSF01 2
-((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((-
-3(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((-
-3 Form SET#RETURN#&ESSA4E
-3(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((-
- T5i* routine i* u*ed .or *ettin0 t5e "API return me**a0e2
- T5e routine i* a *tandard routine .or "API* t5at 5andle* t5e me**a0e
- *tructure .or t5e "APIRETURN *tructure2 It 5a* +een copied .rom t5e
- "API Compan6 Code 4etli*t
-((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((-
- ((7P#&ESSA4E text
- 8((P#RETURN text
-((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((-
form E!1$E!2$#13E%=E 2&#= ;%L2E(013E%=E) L&KE 3E%=E
C>%#=&#= 01$E!2$# L&KE "%0&$E!2$#6
C>ECK #O! 3E%=E & &#&!&%L6
C%LL 92#C!&O# B"%L<1"%0&$E!2$#1=E!B
E)0O$!&#=
!40E C 013E%=E-3=!4
CL C 013E%=E-3=&,
#23"E$ C 013E%=E-3=#O
0%$A C 013E%=E-3=;A
0%$( C 013E%=E-3=;(
0%$+ C 013E%=E-3=;+
0%$* C 013E%=E-3=;*
D LO=1#O C B B
D LO=13=1#O C B B
&30O$!&#=
"%0&$E!2$# C 01$E!2$#
E)CE0!&O#
O!>E$ C A6
endform6 ? E!1$E!2$#13E%=E
FUNCTION Z*BAPI*GET*ORDER*STATUS
92#C!&O# 71ba5i1get1order1system1status6
-9((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
-9-9,ocal inter.ace
-9 I&P$RTIN4
-9 :A,UE;I#AUFNR< T=PE AUFNR
-9 :A,UE;I#SPRAS< T=PE SPRAS %EFAU,T S=(,AN4U
-9 :A,UE;I#EXC,U%EINACTI:E< T=PE C>AR1 $PTI$NA,
-9 EXP$RTIN4
-9 :A,UE;RETURN< T=PE "APIRETURN
-9 TA",ES
-9 T#"APISTAT STRUCTURE !"APISTAT
-9((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
,%!%.
l1aufnr L&KE afko-aufnr8
l1ob@nr L&KE @est-ob@nr6
********************************************
* C+(,- &. o%(% (/&)!)
********************************************
SELECT SINGLE aufnr
FROM afk
INTO !_aufnr
"#ERE aufnr $ BAPI_ORDER_STATUS_IMPORT%r&'r(&.
IF )*%)u+r, NE 0.
CLEAR -'))a.'.
-'))a.'%-)./* $ 0E0.
-'))a.'%-).(& $ 0Z10.
-'))a.'%-).n $ 00000.
-'))a.'%-).23 $ BAPI_ORDER_STATUS_IMPORT%r&'r(&.
PERFORM )'/_r'/urn_-'))a.' USING -'))a.'
C#ANGING r'/urn.
IF 3 $ 2.
* T+( o'l0 %(a)o' !o &',l$( !+&) )!a!(1('!, !+a! 2&ll ob3&o$)l0
* '(3(% (/(,$!(, &) !+a! &! 2&ll ,%(a!( a %(.(%(,(',( )o !+a! 0o$
* ,a' .&' o$! 2+(%( a 4a%!&,$la% 1())a5( &) b(&'5 $)(. T+&)
* .$',!&o'al&!0 &) $)( b0 !+( BAPI) 4%o5%a11( b0 SAP
MESSAGE '0004516.
ENDIF.
ENDIF.
C#EC7 r'/urn IS INITIAL.
--------------------------------------------
- Read order *tatu*
--------------------------------------------
CO#C%!E#%!E BO$B "%0&1O$,E$1!%!21&30O$!-orderid &#!O l1ob@nr6
&9 "%0&1O$,E$1!%!21&30O$!-i1excludeinactive C B)B6
ELEC! ob@nr stat inact
9$O3 @est
&#!O !%"LE t1ba5istat
<>E$E ob@nr C l1ob@nr %#,
inact EF B)B6
ELE6
ELEC! ob@nr stat inact
9$O3 @est
&#!O !%"LE t1ba5istat
<>E$E ob@nr C l1ob@nr6
E#,&96
&9 sy-subrc EF '6
- No o+/ect *tatu* .ound
CLE%$ message6
message-msgty C BEB6
message-msgid C B/+B6
message-msgno C B''AB6
message-msgvA C "%0&1O$,E$1!%!21&30O$!-orderid6
0E$9O$3 set1return1message 2&#= message
C>%#=&#= return6
&9 A C (6
3E%=E e''A(7+)6
E#,&96
E#,&96
C>ECK return & &#&!&%L6
--------------------------------------------
- Read order *tatu* text*
--------------------------------------------
ELEC! istat txt'* txt+'
9$O3 t@'(t
&#!O !%"LE t1t@'(t
9O$ %LL E#!$&E &# t1ba5istat
<>E$E istat C t1ba5istat-stat %#,
s5ras C "%0&1O$,E$1!%!21&30O$!-i1s5ras6
O$! t1t@'(t "4 istat6
LOO0 %! t1ba5istat &#!O g1ba5istat6
$E%, !%"LE t1t@'(t
<&!> KE4 istat C g1ba5istat-stat "&#%$4 E%$C>
&#!O g1t@'(t6
&9 sy-subrc C '6
3O;E.
g1t@'(t-txt'* !O g1ba5istat-txt'*8
g1t@'(t-txt+' !O g1ba5istat-txt+'6
3O,&94 t1ba5istat 9$O3 g1ba5istat !$%#0O$!&#= txt'* txt+'6
E#,&96
E#,LOO06
E#,92#C!&O#6
STEP 3 ( Create t5e API &et5od
U*in0 t5e "API )I!AR%
!he "%0& -i7ard is used to!o ex5ose the remote function module as a "%0&6 !he -i7ard -ill
generate some additional code8 so the function module is a valid method of the "O$6 !his allo-s
the "%0i to be called as a -orkflo- method in addition to be called by an outside 5rogram6
No!(6 Each function module corres5onds to a method in the "O$
=o to the "usiness Ob@ect "uilder <OA6
4ou can either create the ne- Ob@ect ty5e as a subty5e of an existing business ob@ect or create a
ne- business ob@ect from scratch6 &n this exam5le it -ould be obvious to create the Ob@ect ty5e
as a subty5e of "2(''G 0roduction order6 >o-ever8 to illustrate ho- to create a ne- Ob@ect
ty5e from scratch8 -e -ill do this6
&n the Object/Interface type field -rite the name of the ne- "usiness Ob@ect. /O$,E$!%!6
0ress enter and fill in the additional fields necessary to create the ob@ect ty5e6
u5erty5e. #ot relevant because -e are creating our ob@ect from scratch
0rogram6 !his is the name of the 5rogram -here the -i7ard generates code for the Ob@ect ty5e8
#O! the function module -e created earlier6 !he 5rogram name must not be the name of an
existing 5rogram6
0ress enter and create the ne- business ob@ect6 #ote that -hen you create the business ob@ect a
standard interface8 an attribute Ob@ect!y5e and the methods ExistenceCheck and ,is5lay are
automatically generated6 !hese cannot be changed H
!he next ste5 is to add the /1"%0&1=E!1O$,E$1!%!2 method to the business ob@ect6
elect Utlte! -> "#I met$od! -> "dd met$od and -rite the name of the function module in the
dialog box6 #ext the dialog ox sho- belo- -ill be sho-n6 !his is the start screen of the "%0&
-i7ard6 0roceed -ith -i7ard by 5ressing the button6
%fter you have finished the -i7ard8 you -ill notice that the /=etOrdertatus has been added to
the business ob@ect.
4ou can double-click on the method to see its 5ro5erties6 !o use the business ob@ect you must
change the Ob@ect ty5e status to &m5lemented6 2se menu Edit-FChange releases status-FOb@ect
ty5e-F!o im5lemented6 #o you can test the ob@ect (0ress 9I)6
#ote that the "%0& -i7ard has added a -ra55er class for the function module so it can be sued
as method in the business ob@ect6 Choose menu %oto->#ro&ram to dis5lay the 5rogram.
----- Implementation o. o+/ect t6pe !$R%ERSTAT -----
&#CL2,E EO"JEC!F6
"E=&#1,%!% O"JEC!6 9 %o not c5an0e22 %ATA i* 0enerated
- onl6 private mem+er* ma6 +e in*erted into *tructure private
,%!%.
9 +e0in o. private?
9 to declare private attri+ute* remove comment* and
9 in*ert private attri+ute* 5ere 222
9 end o. private?
KE4 L&KE <O!O"J&,-O"JKE46
E#,1,%!% O"JEC!6 ? ,o not change66 ,%!% is generated
"E=&#13E!>O, /=E!O$,E$!%!2 C>%#=&#= CO#!%&#E$6
,%!%.
"%0&O$,E$!%!2&30O$! L&KE /"%0&1O$,E$1!%!21&30O$!8
$E!2$# L&KE "%0&$E!2$#8
!"%0&!%! L&KE /"%0&!%! OCC2$ '6
<C1=E!1ELE3E#! CO#!%&#E$ B"a5iOrdertatus&m5ortB
"%0&O$,E$!%!2&30O$!6
<C1=E!1!%"LE CO#!%&#E$ B!"a5istatB !"%0&!%!6
CA,, FUNCTI$N @!#"API#4ET#$R%ER#STATUS@
E)0O$!&#=
"%0&1O$,E$1!%!21&30O$! C "%0&O$,E$!%!2&30O$!
&30O$!&#=
$E!2$# C $E!2$#
!%"LE
!1"%0&!%! C !"%0&!%!
E)CE0!&O#
O!>E$ C 'A6
C%E 4-2"$C6
<>E# '6 ? OK
<>E# O!>E$6 ? to be im5lemented
E#,C%E6
<C1E!1ELE3E#! CO#!%&#E$ B$eturnB $E!2$#6
<C1E!1!%"LE CO#!%&#E$ B!"a5istatB !"%0&!%!6
E#,13E!>O,6
STEP 7 - F&'al )!(4)
<hen the "usiness ob@ect has been checked and the documentation created8 the follo-ing ste5s
must be carried out.
Release the BAPI funti!n "!dule #in the $unti!n Builder%.
$elease the business ob@ect ty5e (in the "O$ Object'ype -> ($an&e relea!e !tatu! to ->
Implemented )6
Release the BAPI as a "eth!d in the BOR #Release the "eth!ds &!u has reated ' Set the urs!r
!n the "eth!d then
Edit -> Change release status -> Object type component -> To released %
9or 5otential -rite "%0&s. $elease the &,oc and its segments
Yu ,an n8 &()9!a* /:' BAPI (n /:' BAPI E;9!r'r<
)$o need! "*"#er!+++
The functional person's tool for stress testing and data conversions..

You might also like