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

Creating RFC Function Module

Former Member
0 Likes
1,500

I am trying to use PHP and SAPRFC to pull out some information from our SAP database. I've been reading documentation and I've stumbled across this in the SAP documentation:

There are two restrictions on writing remote functions that are to be called transactionally:

+ Transactional calls cannot return parameter values. As a result, the interface for these functions should not specify any EXPORT parameters.

+ Functions that run transactionally may not perform call-backs: the caller's context does not necessarily still exist when the call-back is relayed back to the original system.

If this is true then how am I suppose to receive any data back from the RFC? I am trying to pull a couple of variables out of a few tables using basic select statements like this:


FUNCTION Z_RFC_COMMISSION_INDIVIDUAL.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(COMM_ACCOUNT) TYPE  LIFNR DEFAULT 000000
*"  EXPORTING
*"     VALUE(NAME) TYPE  LFA1-NAME1
*"----------------------------------------------------------------------
SELECT SINGLE name1 FROM lfa1 INTO name
  WHERE lifnr = comm_account.

ENDFUNCTION.

I can connect to the SAP database but I am still not clear on the best way to pull out single values instead of a whole table from SAP using PHP. Does anyone have any example function module source code they could share?

1 ACCEPTED SOLUTION
Read only

karol_seman
Active Participant
0 Likes
1,097

Hi Mark,

look for example at RFC FM 'TABLE_ENTRIES_GET_VIA_RFC' which is returning table with values of specified importing table name.

you call this function as:


  CALL FUNCTION 'TABLE_ENTRIES_GET_VIA_RFC'
    DESTINATION p_dest
    EXPORTING
      tabname               = 'MARA'
      langu                 = 'D'
      only                  = 'X'
    TABLES
      tabentry              = gt_mara
    EXCEPTIONS
      system_failure        = 1  MESSAGE l_mes
      communication_failure = 2  MESSAGE l_mes
      OTHERS                = 3.

Regards,

Karol

7 REPLIES 7
Read only

karol_seman
Active Participant
0 Likes
1,098

Hi Mark,

look for example at RFC FM 'TABLE_ENTRIES_GET_VIA_RFC' which is returning table with values of specified importing table name.

you call this function as:


  CALL FUNCTION 'TABLE_ENTRIES_GET_VIA_RFC'
    DESTINATION p_dest
    EXPORTING
      tabname               = 'MARA'
      langu                 = 'D'
      only                  = 'X'
    TABLES
      tabentry              = gt_mara
    EXCEPTIONS
      system_failure        = 1  MESSAGE l_mes
      communication_failure = 2  MESSAGE l_mes
      OTHERS                = 3.

Regards,

Karol

Read only

0 Likes
1,097

Thanks a lot Karol, I used PHP to call that RFC instead of through my own FM. It returns the table fine. Is there an easy way to parse it though? Are there delimeters in the 'ENTRY' lines of the returned table structure? This is something that is returned to me for reading the table CE4ZOC1:

001X00000000020001990806240 0000001132ZNCI_I1000 000000 10001000V000120 10001411ZC1001 00000000 0001011920 400000000000

How would I parse this so I could get say the 4th element? Is this even possible or do I have to traverse to the correct offset to tokenize my variable?

Read only

0 Likes
1,097

Hi Mark,

the easiest way to parse it is following (I expect that you have the same table structure in rfc system and dev. system):



data: wa(1000) type c.
data: gt_CE4ZOC1 type table of CE4ZOC1.
data: gt_output type table of wa.

CALL FUNCTION 'TABLE_ENTRIES_GET_VIA_RFC'
    DESTINATION p_dest
    EXPORTING
      tabname               = 'CE4ZOC1'
      langu                 = 'D'
      only                  = 'X'
    TABLES
      tabentry              = gt_output
    EXCEPTIONS
      system_failure        = 1  MESSAGE l_mes
      communication_failure = 2  MESSAGE l_mes
      OTHERS                = 3.

gt_CE4ZOC1[] = gt_output[].

you will automatically get parsed values in table gt_CE4ZOC1

If you don't have the same structure of table in RFC and dev. system then you have to create a similar type in dev. system and use this type for your internal table.

If there is any syntax error please correct it on your own - I have no access to SAP at the moment...

And good news. When you move you SAP system to never version it will be automatically parsed so you put into FM directly your table gt_CE4ZOC1

Regards,

Karol

Read only

Former Member
0 Likes
1,097

Some tables work and some tables don't for this function module. The ones that don't work say there is a unicode conversion issue where they are not mutually convertible. This is an example of one of the tables that gives me an error:

comm_info is type LFA1 in the tables tab of my FM


Data: comm_info_output type table of bdi_entry.

CALL FUNCTION 'TABLE_ENTRIES_GET_VIA_RFC'
    EXPORTING
      tabname               = 'LFA1'
*      langu                 = 'EN'
*      only                  = 'X'
    TABLES
      tabentry              = comm_info_output
      nametab               = gt_output2
      sel_tab               = gt_output3
    EXCEPTIONS
      system_failure        = 1  MESSAGE l_mes
      communication_failure = 2
      OTHERS                = 3.

comm_info[] = comm_info_output[].

Before you point out that comm_info and comm_info_output are different types, I know that. I read that the reason some fail and some don't is because of the "DEC" data type in the table structure. How do I get around this issue? I can't make comm_info_output type LFA1 because the imported table has to be of type bdi_entry or else it will fail. Is there a different function module I can call or is there a different way of going about this using the same FM (TABLE_ENTRIES_GET_VIA_RFC)

Read only

0 Likes
1,097

Hi Mark,

I used this code (copy of you)


DATA: comm_info_output TYPE TABLE OF bdi_entry.
DATA: comm_info TYPE TABLE OF lfa1.
DATA: nametab TYPE TABLE OF bdi_mfgrp.
DATA: sel_tab TYPE TABLE OF bdsel_stat.

CALL FUNCTION 'TABLE_ENTRIES_GET_VIA_RFC'
    EXPORTING
      tabname               = 'LFA1'
    TABLES
      tabentry              = comm_info_output
      nametab               = nametab
      sel_tab               = sel_tab
    EXCEPTIONS
      system_failure        = 1  MESSAGE l_mes
      communication_failure = 2
      OTHERS                = 3.

comm_info[] = comm_info_output[].

and I get no error message. Where exactly do you get error message. Resp. is it going to short dump? (if so please attach list from st22).

I think the problem is not in function module but in setting of unicode / non unicode systems. Probably with FM you get some not allowed characters.

I found following thread:

Is it suitable for you?

Sorry I am not expert for this. Maybe if you identify exact problem and you don't know solution create a new thread with good description in the title and you will find unicode issues experts.

Regards,

Karol

Edited by: Karol Seman on Jul 30, 2008 2:18 PM

Read only

0 Likes
1,097

I managed to get the LFA1 table to output using the FM "RFC_GET_TABLE_ENTRIES", but the last table CE1ZOC1 fails because of unicode conversion. I did not list this one last time simply because it is relative to our operating concern that no one else has. I have the correct table types being passed in to the function module which works fine and returns the correct output.

The place where it fails is when I set the table COMM_LINE_ITEMS to COMM_LINE_ITEMS_OUTPUT (returned table from FM RFC_GET_TABLE_ENTRIES).


comm_line_items[] = comm_line_items_output[].

I get the following error when I do a syntax check and when I test it(ignoring syntax error):

"COMM_LINE_ITEMS" and "COMM_LINE_ITEMS_OUTPUT" are not mutually convertible. In Unicode programs, "COMM_LINE_ITEMS" must have the same structure layout as "COMM_LINE_ITEMS_OUTPUT", independent of the length of a Unicode character.

I have a feeling this has to do with one of the data types in the table structure of CE1ZOC1 and not with the function module itself. I'm thinking of just writing my own function module to pull out certain characteristics/attributes instead of the entire table now.

Read only

0 Likes
1,097

ok, now I understand. So the problem is in parsing.

You can try to parse single lines of table you get via RFC FM on your own to eliminate the problem.

The idea is of creating substrings.


loop at comm_line_items_output into wa-comm_line_items_output.
  wa_comm_line_items-field1 = wa-comm_line_items_output(10).
  wa_comm_line_items-field2 = wa-comm_line_items_output+10(5).
  etc ...

  append wa_comm_line_items to comm_line_items.
endloop.

Regards,

Karol