Creating custom RFC function modules for use in SUP hybrid application development (SAP MBO)
When I started to work on a mobile app development, about two months ago, one of the main obstacles for me was interaction between MBOs and SAP ERP backend. I knew that it is done with BAPI or RFC but for my first application a needed to read and update custom Z tables and so to create custom RFC.
At the time I didn’t had any experience with MBOs nor with RFCs so I start experimenting and seeking the best way to accomplish that communication.
Although the approach that I will present here did work for me, maybe there is another or better way so I hope that this document may lead to discussion about this subject.
Let’s say we have to make mobile application for Work order that is consists of four database tables, Header, Item, Material and Worker so that primary key of Header is foreign key in Item and primary key of Item is foreign key in Material and Worker. Primary key of Header is Company code and Order number. We will first create RFC for reading and then for update or create operations.
Ok, let’s go to SE37 and create new Function group from menu GoTo->Function groups->Create group. In the main screen of function builder we write a name that should start with “ZBAPI” and click create. We assign our function module to the function group that we just create, or some other.
Crucial aspect of our function module is that it can be remotely called so on the Attributes tab we need to check Remote-Enabled module.
On the Import tab we have to provide RFCs import parameters that in this case will be fields of Header table primary key, Company code and Order number. For RFC we need to check Pass Values checkbox in parameters definition area and Optional for our case.
Next, we need to define four internal tables in Tables tab that our RFC will return in MBO. To define these tables, first we need to define the structures for them in SE11, one structure for every table. Structures should have all the fields from our database tables and names fields, like MAKTG for material and so on.
Finally, we need to provide code for manipulation with our parameters and internal tables. In Source code tab we declare two structure types for our two parameters. Structure types must have four fields: sign, option, low and high. This is necessary because we need to do Select statements with optional parameters that have “IN” in Where clause instead of “=”. Here is the source code:
*” VALUE(P_BUKRS) LIKE ZRNZ-BUKRS OPTIONAL
*” VALUE(P_BRNAL) LIKE ZRNZ-BRNAL OPTIONAL
*” ITABZ STRUCTURE ZRNZ_STR OPTIONAL
*” ITABS STRUCTURE ZRNS_STR OPTIONAL
*” ITABR STRUCTURE ZRNR_STR OPTIONAL
*” ITABM STRUCTURE ZRNM_STR OPTIONAL
TYPES: BEGIN OF company_code,
sign TYPE bapisign,
option TYPE bapioption,
low TYPE bukrs,
high TYPE bukrs,
END OF company_code,
BEGIN OF order_number,
sign TYPE bapisign,
option TYPE bapioption,
low TYPE znalbr,
high TYPE znalbr,
END OF order_number.
DATA: rng_company_code TYPE TABLE OF company_code,
itm_company_code TYPE company_code,
rng_order_number TYPE TABLE OF order_number,
itm_order_number TYPE order_number.
FIELD-SYMBOLS: <wa_mat> LIKE LINE OF itabm.
IF p_bukrs IS NOT INITIAL.
TRANSLATE p_bukrs TO UPPER CASE.
itm_company_code–sign = ‘I’.
itm_company_code–option = ‘EQ’.
itm_company_code–low = p_bukrs.
APPEND itm_company_code TO rng_company_code.
IF p_brnal IS NOT INITIAL.
UNPACK p_brnal TO p_brnal.
itm_order_number–sign = ‘I’.
itm_order_number–option = ‘EQ’.
itm_order_number–low = p_brnal.
APPEND itm_order_number TO rng_order_number.
SELECT * INTO CORRESPONDING FIELDS OF TABLE itabz
WHERE zrnz~bukrs IN rng_company_code
AND zrnz~brnal IN rng_order_number.
SELECT * INTO CORRESPONDING FIELDS OF TABLE itabs
WHERE zrns~bukrs IN rng_company_code
AND zrns~brnal IN rng_order_number.
SELECT * INTO CORRESPONDING FIELDS OF TABLE itabr
WHERE zrnr~bukrs IN rng_company_code
AND zrnr~brnal IN rng_order_number.
SELECT * INTO CORRESPONDING FIELDS OF TABLE itabm
WHERE zrnm~bukrs IN rng_company_code
AND zrnm~brnal IN rng_order_number.
IF itabm IS NOT INITIAL.
LOOP AT itabm ASSIGNING <wa_mat>.
SELECT SINGLE maktg INTO <wa_mat>–maktg
WHERE makt~matnr = <wa_mat>–matnr
AND makt~spras = sy–langu.
Off course, here displayed selection is only for material name, but the same principle is for all other name fields. Save and activate and our read RFC is finished.
Next, we need to create create/update RFCs.
Only difference is that we need to provide all the fields of the database table as import parameters and that we create one RFC for every database table. Also, here we don’t need export tables.
In the source code we define work area and fill it with our parameters values and then modify database table from that work area. Here is the source code:
FUNCTION zbapi_scn_zrnz_create .
*” VALUE(BUKRS) LIKE ZRNZ-BUKRS
*” VALUE(BRNAL) LIKE ZRNZ-BRNAL
*” VALUE(PJ) LIKE ZRNZ-PJ OPTIONAL
*” VALUE(TIPNAL) LIKE ZRNZ-TIPNAL OPTIONAL
*” VALUE(TIPP) LIKE ZRNZ-TIPP OPTIONAL
*” VALUE(DATNAL) LIKE ZRNZ-DATNAL OPTIONAL
*” VALUE(SMENA) LIKE ZRNZ-SMENA OPTIONAL
*” VALUE(DATOTV) LIKE ZRNZ-DATOTV OPTIONAL
*” VALUE(DATZAT) LIKE ZRNZ-DATZAT OPTIONAL
*” VALUE(ODGOV) LIKE ZRNZ-ODGOV OPTIONAL
*” VALUE(IZDAO) LIKE ZRNZ-IZDAO OPTIONAL
*” VALUE(PRCTRI) LIKE ZRNZ-PRCTRI OPTIONAL
*” VALUE(PRCTRP) LIKE ZRNZ-PRCTRP OPTIONAL
DATA wa LIKE zrnz.
UNPACK brnal TO brnal.
wa–bukrs = bukrs.
wa–brnal = brnal.
wa–pj = pj.
wa–tipnal = tipnal.
wa–tipp = tipp.
wa–datnal = datnal.
wa–smena = smena.
wa–datotv = datotv.
wa–datzat = datzat.
wa–odgov = odgov.
wa–izdao = izdao.
wa–prctri = prctri.
wa–prctrp = prctrp.
MODIFY zrnz FROM wa.
You can notice that all parameters are optional except the one for primary key of database table. And that’s it.
These are off course the simplest RFCs without any checks or exceptions return information.
I’m planning to make a blog post on how to make hybrid mobile application in Sybase unwired platform with SAP MBOs and this document will be the reference for creating RFC functions that will be used.
Thank you for reading.