Incercare reusita :) de lucru cu BAPI
BAPI sunt folosite pentru a putea accesa, prin intermediul unei aplicatii externe, de ex VB, prin RFC, obiecte din SAP, sau mai degraba prin BAPI se pot apela metodele definite pentru obiectul respectiv din SAP.
Tutorialul l-am gasit postat pe http://www.sapgenie.com/abap/bapi/example.htm
Mai intai, cu tranzactia SWO1 se creaza un obiect ZBAPITEST. La object type am pus ZBAPITEST, object name ZTextBapi.Programul este ZBAPITEST.La application se pune Z.
Prin acest BAPI se va incerca accesarea tabelei ZTEST, pe care am creat-o cu campul CHEIE (primary key, CHAR 3), si campurile FIELD1 si FIELD2, amandoua CHAR(15). Am pus trei inregistrari in tabela.
Pentru obiectul BAPI, care de fapt este un FUNCTION MODULE, am creat mai intai o structura pentru transmiterea parametrilor de import, la mine structura aceasta se numeste ZBAPISTRCT, care are doar campul CHEIE. Urmeaza crearea function module Z_BAPI_GET_FIELD, la care e foarte important ca la atribute sa se selecteze Remote-enabled module, altfel BAPI-ul nu va putea fi apelat prin RFC. La import am pus BAPIMPORT type ZBAPISTRCT, cu bifa PASS VALUE. La export se pune RETURN type BAPIRETURN, tot cu pass value.La tables ZBAPITABLE type ZTEST, evident cu pass value :)
Mai jos este source code-ul:
function z_bapi_get_field.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(BAPIMPORT) TYPE ZBAPISTRCT
*" EXPORTING
*" VALUE(RETURN) TYPE BAPIRETURN
*" TABLES
*" ZBAPITABLE STRUCTURE ZTEST
*"----------------------------------------------------------------------
data: l_field1 like ztest-field1,
l_field2 like ztest-field2,
l_cheie like ztest-cheie.
select * from ztest into table zbapitable
where cheie = bapimport-cheie.
if sy-subrc ne 0.
clear message.
message-msgty = 'E'.
message-msgid = 'Z3'.
message-msgno = '000'.
message-msgv1 = bapimport-cheie.
perform set_return_message using message
changing return.
if 1 = 2.
* The only reason to include this statement, that will obviously
* never execute, is that it will create a referecence so that you
* can find out where a particular message is being used. This
* functionality is used by the BAPIs programmed by SAP
message e000(z3).
endif.
endif.
check return is initial.
endfunction.
Function module SAPLZBAPITESTGR trebuie sa contina:
*******************************************************************
* System-defined Include-files. *
*******************************************************************
* REPORT SAPLZBAPITESTGR.
include lzbapitestgrtop. " Global Data
include lzbapitestgruxx. " Function Modules
*******************************************************************
* User-defined Include-files (if necessary). *
*******************************************************************
* INCLUDE LZBAPITESTGRF... " Subprograms
* INCLUDE LZBAPITESTGRO... " PBO-Modules
* INCLUDE LZBAPITESTGRI... " PAI-Modules
include lzbapitestgrf01.
Include-ul lzbapitestgrtop:
function-pool zbapitestgr. "MESSAGE-ID ..
types:
begin of type_tj02t,
istat like tj02t-istat,
txt04 like tj02t-txt04,
txt30 like tj02t-txt30,
end of type_tj02t.
data:
* Declarations for TABLE parameter
t_bapitable like ztest occurs 0,
g_bapitable like ztest,
* Table for object texts
t_tj02t type type_tj02t occurs 0,
g_tj02t type type_tj02t.
* Structure for return messages
data:
begin of message,
msgty like sy-msgty,
msgid like sy-msgid,
msgno like sy-msgno,
msgv1 like sy-msgv1,
msgv2 like sy-msgv2,
msgv3 like sy-msgv3,
msgv4 like sy-msgv4,
end of message.
Include-ul lzbapitestgruxx:
*****************************************************************
* THIS FILE IS GENERATED BY THE FUNCTION LIBRARY. *
* NEVER CHANGE IT MANUALLY, PLEASE! *
*****************************************************************
include lzbapitestgru01. "asta duce la function Z_BAPI_GET_FIELD (codul)
"Z_BAPI_GET_FIELD
*----------------------------------------------------------------------*
***INCLUDE LZBAPITESTGRF01 .
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form set_return_message
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_MESSAGE text
* <--P_RETURN text
*----------------------------------------------------------------------*
form set_return_message using value(p_message) like message
changing p_return like bapireturn.
check not message is initial.
call function 'BALW_BAPIRETURN_GET'
exporting
type = p_message-msgty
cl = p_message-msgid
number = p_message-msgno
par1 = p_message-msgv1
par2 = p_message-msgv2
par3 = p_message-msgv3
par4 = p_message-msgv4
* LOG_NO = ' '
* LOG_MSG_NO = ' '
importing
bapireturn = p_return
exceptions
others = 1.
endform. " SET_RETURN_MESSAGE
Dupa ce se activeaza toate astea, se revine la ZBAPITEST in SWO1,pe change, in meniul Utilities -> API methods ->Add method, aici se pune Z_BAPI_GET_FIELD (o sa dea o avertizare cum ca ar trebui alesi parametri care incep cu BAPI, dar se poate trece de ea), la method se completeaza automat cu ZGetField, la name cu ce vreau, la fel si la descriere.Dupa ce se termina cu wizard-ul, metoda apare in nodul methods, pentru obiectul ZBAPITEST.In continuare se merge in Edit->Change releases status->Object type->To implemented.
Pasii urmatori sunt cei referitori la modificarea status-ului Release:
1. Release pentru BAPI function module (in Function Builder)
2.Release in BOR pentru business object type, adica din meniu ObjectType->Change release status to->Implemented
3. Release pentru metoda din BAPI in BOR, pune cursorul pe metoda, si apoi din Edit->Change release status->Object Type componen->To released.
Dupa toate astea, cu tranzactia BAPI, la filtrare alfabetica, trebuie sa apara cam in coada listei.
0 Comments:
Post a Comment
<< Home