Skip to Content
Author's profile photo varun boyina

SAP UI 5 – CALLING RFC THROUGH SAP JCO.

In this demo I would like to show how to call a SAP RFC  in SAP UI5  using SAP JCO Connector.


I am  passing a vendor number to RFC “BAPI_ADDRESSORG_GETDETAIL” which then returns the vendor office number or address number

Pre-Requisites for this demo :  

  1. a) Eclipse  with  SAP UI5 Installed
  2. b) Tomcat server 7.0
  3. c) javax.servlet.jar
  4. d) sapjco3.jar
  5. e) sapjco3.dll

Before proceeding to this demo tutorial I would suggest to go through the below links where in I explained how to Call a Servlet using  JQUERY,AJAX and JSON.

http://scn.sap.com/docs/DOC-73103

Step1 : I have already shown how to create a Dynamic Web Project in the above link.SO I will be adding new html,view and controller to the .

Step2 :To the existing project “SAPUI5_JQUERY_AJAX_DEMO” , Add a new view as below:

                WebContent-> sapui5_jquery_ajax_demo-> Right Click->New-> SAPUI5 Application Development->View->Next-> Enter Name “Call_RFC_Using_SapJco.view”->select  Java Script Button->Finish


Capture_1.JPG



Capture_2.JPG



Step 3: Create a new html.

WebContent->RightClick->New->HTML File-> Enter name “index5.html”->Ensure HTML 5 is selected->Click Finish


Capture_3.JPG




Step 4:   Enter the below code in “index5.html” .

<!DOCTYPE html>

<html>

<head>

              <meta http-equiv=“X-UA-Compatible” content=“IE=edge”>

              <meta http-equiv=‘Content-Type’ content=‘text/html;charset=UTF-8’/>

            

              <script src=“resources/sap-ui-core.js”

                           id=“sap-ui-bootstrap”

                           data-sap-ui-libs=“sap.ui.commons”

                           data-sap-ui-theme=“sap_bluecrystal”>

              </script>

              <!– add sap.ui.table,sap.ui.ux3 and/or other libraries to ‘data-sap-uilibs‘ if required –>

              <script>

                           sap.ui.localResources(“sapui5_jquery_ajax_demo”);

                           var view = sap.ui.view({id:“idCall_RFC_Using_SapJco1”, viewName:“sapui5_jquery_ajax_demo.Call_RFC_Using_SapJco”, type:sap.ui.core.mvc.ViewType.JS});

                           //view.placeAt(“jqueryajaxGetUserServletResponse”);

              </script>

       </head>

<body>

<h1>SAP UI5 Demo – Calling RFC Using SAP JCO </h1>:

<form>

<br>Enter Vendor Number: <input type=“text” id=“vendorNumber” /></br>

<br> <input type=“button” value=“Show Table” id=“showTable”/></br>

    </form>

    <br>

    <br>  

    <div id=“geRfcResponse”></div>

</body>

</html>


Step 5:   Enter the below code in  “Call_RFC_Using_SapJco.view.js”

  1. sap.ui.jsview(“sapui5_jquery_ajax_demo.Call_RFC_Using_SapJco”, {

       /** Specifies the Controller belonging to this View.

       * In the case that it is not implemented, or that “null” is returned, this View does not have a Controller.

       * @memberOf sapui5_jquery_ajax_demo.Call_RFC_Using_SapJco

       */

       getControllerName : function() {

              return “sapui5_jquery_ajax_demo.Call_RFC_Using_SapJco”;

       },

       /** Is initially called once after the Controller has been instantiated. It is the place where the UI is constructed.

       * Since the Controller is given to this method, its event handlers can be attached right away.

       * @memberOf sapui5_jquery_ajax_demo.Call_RFC_Using_SapJco

       */

       createContent : function(oController) {

$(document).ready(function() {

                   

                     $(“#showTable”).click(function(event) {

                          var number = $(‘#vendorNumber’).val();

                          $.get(‘CallRfcUsingSapJco’, {

                                  vNumber : number

                          }, function(responseText) {

                                  $(‘#geRfcResponse’).text(responseText);

                          });

                  });

              });

       }

});



Step 6:    Import the jar file “javax.servlet.jar”  into WebContent->WEB-INF->lib

Simplest way to do this is to drag and drop “javax.servlet.jar”  file into lib directory.Select  “Copy Files” an press “OK”.

Capture_4.JPG



Step 7 : In Src folder .create a class file “CallRfcUsingSapJco.java” in package “ajaxdemo”. Enter the  following code:

package ajaxdemo;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Properties;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

import org.xml.sax.InputSource;

import com.sap.conn.jco.AbapException;

import com.sap.conn.jco.JCoDestination;

import com.sap.conn.jco.JCoDestinationManager;

import com.sap.conn.jco.JCoException;

import com.sap.conn.jco.JCoFunction;

import com.sap.conn.jco.ext.DestinationDataProvider;

@WebServlet(“/CallRfcUsingSapJco”)

public class CallRfcUsingSapJco extends HttpServlet {

               

                  private final String DESTINATION_NAME1 = “ABAP_AS_WITH_POOL”;

                 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                                

                                 try

                                {

                                                 String vNumber = request.getParameter(“vNumber”).trim();

                                System.out.println(“init”);

                                initDestinationProperty();

                                System.out.println(“init2”);

                                String addressNumber=”whats happening”;

                                addressNumber=consumeABAPFM(vNumber);

                                System.out.println(“addressNumber is”+addressNumber);

                                 response.getWriter().write(“Vendor Office Number is: “+addressNumber);

                                }catch(Exception e){

                                                e.printStackTrace();

                                }

                    }

                

                 private void createDestinationDataFile(String destinationName,

                                                Properties connectProperties)

                    {

                                File destCfg = new File(destinationName+”.jcoDestination”);

                                try

                                {

                                                FileOutputStream fos = new FileOutputStream(destCfg,false);

                                                connectProperties.store(fos, “for tests only !”);

                                                fos.close();

                                }

                                catch (Exception e)

                                {

                                                throw new RuntimeException(“Unable to create the destination files”, e);

                                }

                    }

                

                 private void initDestinationProperty()

                    {

                                Properties connectProperties = new Properties();

                                connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, “10.100.110.3”);

                                connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, “00”);

                                connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT,”210″);

                                connectProperties.setProperty(DestinationDataProvider.JCO_USER,”DC_VARUN”);

                                connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,”mar2016#”);

                                connectProperties.setProperty(DestinationDataProvider.JCO_LANG,”en”);

                               

                                createDestinationDataFile(DESTINATION_NAME1, connectProperties);

                                connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, “3”);

                    }

                   

                    public String consumeABAPFM(String vNumber) throws JCoException

                    {

                                String endResult=””;

                        JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME1);

                        JCoFunction function = destination.getRepository().getFunction(“BAPI_ADDRESSORG_GETDETAIL”);

                        if(function == null)

                            throw new RuntimeException(“BAPI_ADDRESSORG_GETDETAIL not found in SAP.”);

                        try

                        {

                                function.getImportParameterList().setValue(“OBJ_TYPE”, “LFA1”);

                                function.getImportParameterList().setValue(“OBJ_ID”, vNumber);

                                function.getImportParameterList().setValue(“CONTEXT”, “0001”);

                                function.getImportParameterList().setValue(“IV_CURRENT_COMM_DATA”, “X”);

                            function.execute(destination);

                        }

                        catch(AbapException e)

                        {

                            System.out.println(e.toString());

                            return “”;

                        }

                       

                       

                        try{

                               

                                String output=function.getExportParameterList().toXML();

                                System.out.println(output);

                                //create instance of document builder factory

                                                                DocumentBuilderFactory domBuilderFactory=DocumentBuilderFactory.newInstance();

                                                               

                                                                //from document builder factory build dom builder.                                      

                                                                DocumentBuilder domBuilder=domBuilderFactory.newDocumentBuilder();

                                                               

                                                                //from document builder,parse the input file

                                                                Document dom=domBuilder.parse(new InputSource(new ByteArrayInputStream(output.getBytes(“utf-8”))));

                                                               

                                                                //get all “Transactions” nodes from doc

                                                                NodeList outputList= dom.getElementsByTagName(“OUTPUT”);

                                                               

                                               

                                                               

                                                                endResult=((Element)(outputList.item(0))).getElementsByTagName(“ADDRESS_NUMBER”).item(0).getFirstChild().getTextContent();

                                                               

                                System.out.println(“endResult is”+endResult);

                               

                        }catch(Exception e){

                                e.printStackTrace();

                        }

                        return endResult;

                    }

}


Step 8 : Now deploy the project on to  Tomcat server. Right click on “index5.html” -> “Run On Server”->Choose “Tomcat Server 7.0” ->Click Finish

Capture_5.JPG

Capture_6.JPG

Capture_7.JPG

Click on the button and the below screen will be displayed:


Capture_8.JPG

Assigned Tags

      9 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo LAN XIAO
      LAN XIAO

      i also choose jco instead of odata,because we have a sap server which don't have a extranet ip and we connect it by using router, i know jco can call rfc by router but i don't know whether odata can. 😛

      Author's profile photo varun boyina
      varun boyina
      Blog Post Author

      Thank you Lan Xiao for the feedback and good to know that you have called JCO.

      Author's profile photo Former Member
      Former Member

      how can i use this code  to conect a destination  in sap hana cloud conector  ?

      Author's profile photo Michael Appleby
      Michael Appleby

      Unless you are asking for clarification/correction of some part of the Document, please create a new Discussion marked as a Question.  The Comments section of a Blog (or Document) is not the right vehicle for asking questions as the results are not easily searchable.  Once your issue is solved, a Discussion with the solution (and marked with Correct Answer) makes the results visible to others experiencing a similar problem.  If a blog or document is related, put in a link.  Read the Getting Started documents (link at the top right) including the Rules of Engagement. 

      NOTE: Getting the link is easy enough for both the author and Blog.  Simply MouseOver the item, Right Click, and select Copy Shortcut.  Paste it into your Discussion.  You can also click on the url after pasting.  Click on the A to expand the options and select T (on the right) to Auto-Title the url.

      Thanks, Mike (Moderator)

      SAP Technology RIG

      Author's profile photo Former Member
      Former Member

      Hi varun boyina,

      I had a question where can i download the sapjco3.jar and sapjco.dll

      i was looking in the sap market place but i dont find can you help me with this?

      thank you very much.

      Regards,

      Simón.

      Author's profile photo Former Member
      Former Member

      Hi varun sorry i already downloaded the jco how do you use them?

      please help.

      thank you!

      Regards

      Simon.

      Author's profile photo Michael Appleby
      Michael Appleby

      Simon,

      Unless you are asking for clarification/correction of some part of the Document, please create a new Discussion marked as a Question.  The Comments section of a Blog (or Document) is not the right vehicle for asking questions as the results are not easily searchable.  Once your issue is solved, a Discussion with the solution (and marked with Correct Answer) makes the results visible to others experiencing a similar problem.  If a blog or document is related, put in a link.  Read the Getting Started documents (link at the top right) including the Rules of Engagement.

      NOTE: Getting the link is easy enough for both the author and Blog.  Simply MouseOver the item, Right Click, and select Copy Shortcut.  Paste it into your Discussion.  You can also click on the url after pasting.  Click on the A to expand the options and select T (on the right) to Auto-Title the url.

      Thanks, Mike (Moderator)

      SAP Technology RIG

      Author's profile photo Former Member
      Former Member

      Hi varun i follow all the steps but i have these  problems

      Captura.JPG

      Captura1.JPG

      Captura2.JPG

      i hope you can help me.

      Regards

      Simón.

      Author's profile photo Anastasius Tripsaptian
      Anastasius Tripsaptian

      Hi Varun, thanks a lot for helpful blog. I have a question regarding access this sapui5 (which already using sap jco to calling rfc) through internet. Is it possible to access this sapui5 from internet or we can only access it from out intranet ?

      Regards

      Anas