communication to presentation server in background mode[1]

25
Communication to Presentation Server in Background Mode © 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 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. 1

Upload: andrea-negro

Post on 27-Apr-2015

1.216 views

Category:

Documents


23 download

TRANSCRIPT

Page 1: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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.

1

Page 2: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 2

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

Page 3: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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.

3

Page 4: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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.

4

Page 5: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

5

Page 6: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

3. After ‘Saving’ we get the below screen. Here just click the ‘Registration’ button.

6

Page 7: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

After clicking the ‘Registration’ button we get the below screen.

7

Page 8: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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

8

Page 9: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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’ .

9

Page 10: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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.

10

Page 11: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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.

11

Page 12: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

12

Page 13: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

13

Page 14: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 14

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).

Page 15: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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>

15

Page 16: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

16

Page 17: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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

17

Page 18: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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.

18

Page 19: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 19

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.

Page 20: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 20

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

Page 21: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 21

exporting file = p_readpath read = p_flag write = p_flag1 tables filedata = P_final exceptions system_failure = 1 message dg_ermsg communication_failure = 2 message 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.

Page 22: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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.

22

Page 23: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

Sample Input text file:

23

Page 24: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

Sample Output:

24

Page 25: Communication to Presentation Server in Background Mode[1]

Communication to Presentation Server in Background Mode

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com

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.

25