Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
varun_boyina
Participant

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






Step 3: Create a new html.

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





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-ui-libs' 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”.



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

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


9 Comments
Labels in this area