You are on page 1of 25

Communication to Presentation

Server in Background Mode

SDN Community Contribution


(This is not an official SAP document.)

Disclaimer & Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces
and therefore is not supported by SAP. Changes made based on this information are not supported and can
be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods
suggested in this document, and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of
this technical article or code sample, including any liability resulting from incompatibility between the content
within this document and the materials and services offered by SAP. You agree that you will not hold, or seek
to hold, SAP responsible or liable with respect to the content of this document.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Communication to Presentation
Server in Background Mode

Applies To:
SAP R/3 System.
This document is intended for those who want to communicate (read/write data) with the Presentation Server
(desktop system) in the Background Mode. In real time scenario some times we need to read or write data to
Presentation Server while the program is running in Background mode on the Application server. As we can
not use the usual GUI_UPLOAD / GUI_DOWNLOAD function modules for this purpose when the program is
running in Background mode, we end up with first writing data to Application Server and later again reading it
back from there. In this document we will see how to achieve Communicating to the Presentation Server
when the program is running in the Back ground mode on the Application Server, with few settings at your
SAP R/3 system and local machine.

Summary
In this article we will create one RFC destination in R/3 system to communicate with SAP front end and do
some settings at our Desktop System. Then we will write one sample program to read data from a text file on
the Presentation Server and again write back the data to an Excel file on the same Presentation Server, while
the program is running in Background mode (on the Application Server).

By: Srilatha Thirukkovalluri


Company: Intelligroup Asia Pvt Ltd.
Date: 08 Dec 2005

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Communication to Presentation
Server in Background Mode
SAP R/3 Settings
First we will create RFC destination of TCP/IP Connection type to communicate with the Presentation Server.
Follow the below steps to do that.
1. Go to SM59 transaction and press Create button on the Application tool bar.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Communication to Presentation
Server in Background Mode
2. . Next we get the below screen

Here give some name (Ex:BJRFC) at the RFC Destination field and mention Connection type as T
(TCP/IP Connection) and enter some Description (Ex: TCP / IP Connection to the Presentation Server.)
and SAVE it. Please see below screen.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Communication to Presentation
Server in Background Mode

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Communication to Presentation
Server in Background Mode
3. After Saving we get the below screen. Here just click the Registration button.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Communication to Presentation
Server in Background Mode
After clicking the Registration button we get the below screen.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Communication to Presentation
Server in Background Mode

4. At the above screen give some name (ex: BJACCESS) at the Program ID field. See screen below.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Communication to Presentation
Server in Background Mode
5. AT the above screen, in the Menu select Destination->Gateway Options. You get the below popup
screen. Here enter your system Host name(i.e IP address of the system) and Gateway Service(This
will be sapgw00, here 00 is your system no). and click OK .

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Communication to Presentation
Server in Background Mode

6. After clicking OK on the Popup screen, we are presented with the below screen . Here Save . This
completes creating the RFC destination of TCP/IP Connection type to the Presentation Server.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

10

Communication to Presentation
Server in Background Mode

Local Machine (Desktop System) Settings


In your local machine look for SAP Folder usually located in the Program files folder and open SAP GUI
folder. In that open RFCSDK folder and Copy SAPRFC.INI file from the TEXT folder to the BIN Folder.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

11

Communication to Presentation
Server in Background Mode

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

12

Communication to Presentation
Server in Background Mode

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

13

Communication to Presentation
Server in Background Mode

Then open the SAPRFC.INI file in BIN folder and Register a RFC server program at a SAP Gateway.

We need to enter two entries for R type in the SAPRFC.INI file like below.
DEST=RTL_R
TYPE=R
PROGID=BJACCESS.rfcexec
GWHOST=10.250.4.203
GWSERV=sapgw00
RFC_TRACE=0

DEST=RTL_R
TYPE=R
PROGID=BJACCESS.trfcexec
GWHOST=10.250.4.203
GWSERV=sapgw00
RFC_TRACE=0
Here DEST = SystemID_R, in our case system ID is RTL. So we gave like RTL_R.
TYPE = R, because we need to register a RFC server program at a SAP Gateway and wait for RFC calls by
R/3 or R/2 System.
PROGID = BJACCESS.trfcexec and BJACCESS.rfcexec. Here BJACCESS is the name of Program ID we
gave when we are creating RFC destination in SM59.
GWHOST= this is your system I.P. address that we enter in Gateway options of RFC Destination.
GWSERV= Gateway service name (This will be sapgw00, here 00 is your system no).
RFC_TRACE = 0 ( 0-Off, 1-ON).

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

14

Communication to Presentation
Server in Background Mode

Then save the file and close it. With this we have finished the settings at our Local system.
Testing the Connection
Open command prompt and go to the bin Directory, where we have RFCEXEC.EXE file. Then enter below
command and press enter it will not come back to dos prompt. If it has come back check your settings and
again try this.
C:\Program Files\SAP\FrontEnd\SAPgui\rfcsdk\bin>rfcexec -aBJACCESS -g10.250.4.203 -xsapgw00.
After going to bin directory enter command rfcexec a<Program ID in RFC destination>
-g<SAP Host>
-x<Sap Service No>

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

15

Communication to Presentation
Server in Background Mode

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

16

Communication to Presentation
Server in Background Mode

Now go to SM59 Transaction and test our RFC destination BJRFC, by clicking the Test Connection button

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

17

Communication to Presentation
Server in Background Mode

Be sure that the connection is successful. If it fails check your settings and try again. And we need to
remember one thing here before testing first we should run the RFCEXEC file in the command prompt like we
did here.
Now we will write a program which will read Text file from presentation server and write back to the
presentation server in Excel format while running in background mode.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

18

Communication to Presentation
Server in Background Mode

Sample Program
This program will use the function module RFC_REMOTE_FILE to read and write data to presentation
server. We use the RFC destination created above (BJRFC), when we call this RFC function module.
REPORT ZBJ_RFC
LINE-COUNT 65
LINE-SIZE 130
NO STANDARD PAGE HEADING
MESSAGE-ID ZTEST.
*----------------------------------------------------------------------*
* Developer:
Srilatha T
*
* Date:
12/06/2005
*
* Program Type:
Executable Program
*
* Description:
This report will read text file from presentation*
*
server and writes back to presentation server in *
*
Excel file while running in Background mode
*
* Tables Used:
*
* Function Modules: RFC_REMOTE_FILE
*
* Other Dependencies:
*
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
*
T Y P E D E C L A R A T I O N S
*----------------------------------------------------------------------*
*--Data Structure For reading data
TYPES: BEGIN OF tp_final,
pernr
type pa0001-pernr,
" Pernr
sname
TYPE pa0001-sname,
" Employee Name
END OF tp_final.
*----------------------------------------------------------------------*
*
I N T E R N A L T A B L E D E C L A R A T I O N S
*----------------------------------------------------------------------*
*--Internal table declaration for Employee Details
DATA: dt_final TYPE STANDARD TABLE OF tp_final,
dt_final1 TYPE STANDARD TABLE OF tp_final.
*----------------------------------------------------------------------*
*
D A T A D E C L A R A T I O N S
*----------------------------------------------------------------------*
data : dg_flag
type c,
" Flag
dg_flag1
type c,
" Flag
ds_final
type tp_final,
dg_ermsg(120).
" Error message
*----------------------------------------------------------------------*
*
C O N S T A N T S D E C L A R A T I O N S
*----------------------------------------------------------------------*
CONSTANTS: c_dest type rfcdes-rfcdest value 'BJRFC',
c_x
TYPE c
VALUE 'X'.
" For X
*----------------------------------------------------------------------*
*
S E L E C T I O N S C R E E N
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

19

Communication to Presentation
Server in Background Mode
PARAMETERS : Readpath LIKE rlgrap-filename obligatory,
" Read File
wrtepath like rlgrap-filename obligatory.
" Write file
SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
*
A T S E L E C T I O N S C R E E N O N
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR Readpath.
*- Get the file path
PERFORM get_filepath USING Readpath.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR wrtepath.
*- Get the file path
PERFORM get_filepath USING wrtepath.
*----------------------------------------------------------------------*
*
S T A R T O F S E L E C T I O N
*----------------------------------------------------------------------*
START-OF-SELECTION.
* Form to read data
perform read_data.
if not dt_final[] is initial.
dt_final1[] = dt_final[].
* Form to write data
perform write_data.
else.
write : / 'Data is not found'(002).
endif.
*----------------------------------------------------------------------*
*
E N D O F S E L E C T I O N
*----------------------------------------------------------------------*
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*&
Form read_data
*&---------------------------------------------------------------------*
*
To read data from Excel file
*----------------------------------------------------------------------*
FORM read_data.
dg_flag = c_x.
* Fom to read func for reading
perform call_func
tables dt_final
using readpath
dg_flag
dg_flag1.
ENDFORM.
" read_data
*&---------------------------------------------------------------------*
*&
Form call_func
*&---------------------------------------------------------------------*
*
Call function
*----------------------------------------------------------------------*
FORM call_func TABLES
P_FINAL
STRUCTURE DS_FINAL
USING
P_READPATH
P_FLAG
P_FLAG1.
call function 'RFC_REMOTE_FILE' destination c_dest

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

20

Communication to Presentation
Server in Background Mode
exporting
file
= p_readpath
read
= p_flag
write
= p_flag1
tables
filedata = P_final
exceptions
system_failure
= 1 message
communication_failure = 2 message

dg_ermsg
dg_ermsg.

if sy-subrc <> 0.
format reset intensified off.
write: / 'RFC ERROR:'(003), dg_ermsg.
else.
if dg_flag = c_x.
write : / 'Data is successfully Uploaded'(004).
else. dg_flag1 = c_x.
write : / 'Data is successfully Downloaded'(005).
endif.
endif.
ENDFORM.
" call_func
*&---------------------------------------------------------------------*
*&
Form write_data
*&---------------------------------------------------------------------*
*
Write data
*----------------------------------------------------------------------*
FORM write_data.
dg_flag1 = c_x.
clear dg_flag.
* Fom to read func for reading
perform call_func
tables dt_final1
using wrtepath
dg_flag
dg_flag1.
ENDFORM.
" write_data
*&---------------------------------------------------------------------*
*&
Form GET_FILEPATH
*&---------------------------------------------------------------------*
*
Get the file Name
*----------------------------------------------------------------------*
FORM get_filepath USING
p_file TYPE any.
DATA: dl_file LIKE ibipparms-path,
" File name
dl_repid LIKE sy-repid.
" Program name
dl_repid = sy-repid.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = dl_repid
dynpro_number = syst-dynnr
field_name
= ' '
IMPORTING
file_name
= dl_file
EXCEPTIONS
OTHERS
= 1.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

21

Communication to Presentation
Server in Background Mode
IF sy-subrc = 0.
p_file = dl_file.
ELSE.
MESSAGE I999 WITH 'Error while getting the file name'(007).
ENDIF.
ENDFORM.

" GET_FILEPATH

Testing the program


Execute the sample program.

In the READPATH parameter give file path to read the file and in the WRTEPATH parameter give download
file path and run the program in Background mode. This program will read the text file from the path specified
and writes back to file in the entered path in excel format.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

22

Communication to Presentation
Server in Background Mode
Sample Input text file:

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

23

Communication to Presentation
Server in Background Mode
Sample Output:

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

24

Communication to Presentation
Server in Background Mode

Points to be noted

Before running the program first we need to run the rfcexec in the command prompt passing
parameters a<program ID> -g<SAP Host> -x<SAP GatewayService>.

It should not come back to command prompt when we execute the rfcexec. If it is returning to
command prompt then check your settings and do it again.

Be sure that rfcexec file should be running at your local machine, when ever we are communicating
to the presentation server in the background mode, then only it can communicate with the front end
in the background mode.

After scheduling the Back ground job we can log off the SAP system. This does not hinder the
communication between the desktop and the App Server in any way. Of course the Desktop should
remain switched on!!

Author Bio
Srilatha Thirukkovalluri has around 5 years of experience in ABAP/4 programming techniques
using quality-driven methodologies. She has expertise in Net weaver - XI Competency and
Business Server Pages. Currently she is working with Intelligroup Asia Pvt Ltd as Systems
Analyst.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

25

You might also like