This is part of a multi-blog series concentrating on how to consume the backend APIs in SAP Cloud Platform Workflow Service.

Context

Building and extending cloud application workflows can unify processes across systems and connect human-centric activities and information across the enterprise. By taking a low-code approach to creating and extending workflows, SAP Cloud Platform Workflow reduces development time and expense. Often, you would want to consume an existing RFC[1] as part of the extension workflows to perform certain tasks – get the custom details from CRM system, get details of a quotation etc. This blog explains how an application developer can expose any backend RFC as REST service so that it can be consumed in the SAP CP workflow

[1] RFC (Remote Function Call) is the standard SAP interface for communicating to any SAP backend system like SAP ERP, SAP HCM, or even S/4HANA. The RFC calls a function to be executed in a backend system.

What to Expect in this Article

There are a few different ways by which we can create a service consuming a RFC that can be accessed from SAP CP Workflow via cloud connector:

  1. Create a Java servlet and expose the RFC as a service.
  2. Model a OData service consuming the RFC using SAP NW Gateway Service Builder.
  3. Use SAP Cloud Platform OData Provisioning and expose business data and business logic as OData services.

In this article, we will create and deploy a Java servlet in SAP Cloud Platform, which consumes a standard RFC which used to test connectivity – STFC_CONNECTION. Once we understand how to consume such a RFC, the same approach can be used to expose any RFC as a REST service and then consume the same from the SAP CP workflow.

Pre-Requisite

Since SAP Gateway is an on-premise application which is behind a secure network (intranet), you need to use the SAP Cloud Connector.

1. IT Administrator has already installed & setup the cloud connector and established a SSL tunnel from the connector to your SAP Cloud Platform account.

2. IT Administrator has exposed the backend system and specific resources to be consumed via cloud connector.

Note that in the cloud connector configuration, I have created a cloud to on-premise connection with RFC protocol and the backend type as ABAP System. For this host, I have enabled all resources (RFC) which has STFC as prefix, so I will be able to access the RFC STFC_CONNECTION or any other RFC starting with STFC.

3. Application Developer has created a SAP CP RFC Destination pointing to the required backend system.

4. Application Developer has the Eclipse IDE & has installed the SAP Cloud Platform Tools for Java and has completed the setup of development environment.

5. Last but not least, you have at least one Java Quota available in your tenant.

References for Completing Pre-Requisite

RFC Destination https://help.sap.com/viewer/cca91383641e40ffbe03bdc78f00f681/Cloud/en-US/e184daba118b46679c6968567bacc98e.html

SAP Cloud Platform Tools: https://tools.hana.ondemand.com/#cloud

Setting up Development Environment: https://help.sap.com/viewer/cca91383641e40ffbe03bdc78f00f681/Cloud/en-US/e815ca4cbb5710148376c549fd74c0db.html

Install Cloud Connector: https://help.sap.com/viewer/cca91383641e40ffbe03bdc78f00f681/Cloud/en-US/57ae3d62f63440f7952e57bfcef948d3.html

Configuring Access Control for RFC https://help.sap.com/viewer/cca91383641e40ffbe03bdc78f00f681/Cloud/en-US/ca5868997e48468395cf0ca4882f5783.html

How to achieve it

You can call a service from a ‘fenced’ customer network using a simple application which consumes an on-premise remote enabled function module. The invocation of function modules via RFC is offered via the JCo API like the one available in SAP NetWeaver Application Server Java since version 7.10, and in JCo standalone 3.0. If you are an experienced JCo developer, you can easily develop a Web application using JCo: you simply consume the APIs like you do in other Java environments.

Create a Dynamic Project

  1. In the Eclipse IDE, open the Java EE perspective
  2. From the Eclipse main menu, choose “New Dynamic Web Project”.
  3. In the Project name field, enter any distinct project name.
  4. In the Target Runtime pane, select the runtime you want to use to deploy the application. In this example, we choose Java Web. If you don’t see this option, make sure your Eclipse development environment setup is complete.
  5. In the Configuration pane, leave the default configuration.
  6. Choose Finish to complete the creation of your project.

 

Create a Java Servlet

1. From the jcorfc context menu, choose “New Servlet”

2. Enter com.sap.demo.jco as the Java package and RFCConnectivity as the Class name and choose Next.

3. Choose Finish so that the ConnectivityRFCExample.java servlet is created and opened in the eclipse editor.

4. Implement the call to RFC via JCo. In this blog, I have made a call to a function module STFC_CONNECTION which is a standard RFC Function module to test connectivity to the backend. Note that I have used a JSON library to easily work with JSON in Java EE. If you are using maven, then add a dependency to org.json. If you are not using maven to build your project, then you can download the JAR file from here and manually add the dependency to the library.

5. Note the following line numbers in the below servlet:

5.a Line No 38 – This is the SAP CP RFC destination created as part of pre-requisite

5.b Line No 57: The Content-type of the servlet’s return message should be set as application/json

5.c You can also close the JCo connection in a ‘finally’ block.

package com.sap.demo.jco;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;
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.JCoParameterList;
import com.sap.conn.jco.JCoRepository;
/**
 * Servlet implementation class RFCConnectivity
 */
public class RFCConnectivity extends HttpServlet {
	private static final long serialVersionUID = 1L;  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RFCConnectivity() {
        super();
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException {
		 PrintWriter responseWriter = response.getWriter();
	        try
	        {
	            // LINE NO 38
	            // Access the SAP Cloud Platform Destination "Q7W_RFC"
	            JCoDestination destination=JCoDestinationManager.getDestination("Q7W_RFC");
	            // Make an invocation of STFC_CONNECTION in the backend
	            JCoRepository repo=destination.getRepository();
	            JCoFunction stfcConnection=repo.getFunction("STFC_CONNECTION");
	            // Set Importing Parameter REQUTEXT for the RFC
	            JCoParameterList imports=stfcConnection.getImportParameterList();
	            imports.setValue("REQUTEXT", "SAP HANA Cloud connectivity for SAP CP Workflow");
	            //Execute the RFC via the SAP CP Destination
	            stfcConnection.execute(destination);
	            // Get the exporting parameters ECHOTEXT and RESPTEXT of the RFC
	            JCoParameterList exports=stfcConnection.getExportParameterList();
	            String echotext=exports.getString("ECHOTEXT");
	            String resptext=exports.getString("RESPTEXT");
	            
	            // Output of this servlet needs to be JSON if consumed from SAP CP Workflow
	            JSONObject responseJson = new JSONObject();
	            // Form the JSON object
	            responseJson.put("echotext", echotext);
	            responseJson.put("resptext", resptext);
	            // LINE NO 57 
	            response.addHeader("Content-type", "application/json");
	            // Set the response as the JSON STRING
	            responseWriter.write(responseJson.toString());
	        }
	        catch (AbapException ae)
	        {
	            //TODO Handle Exception.
	        }
	        catch (JCoException e)
	        {
	            // TODO Handle Exception
	        } catch (JSONException e) {
	        	// TODO handle expetion
				e.printStackTrace();
			}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Deploy the Application

To deploy your Web application locally or on the cloud, see the following two procedures, respectively:

Deploying Locally from Eclipse IDE

Deploying on the Cloud from Eclipse IDE

Once the application is deployed you can execute the same in a browser or any http client to check if the service is executed successfully. As you can see from the below image, we get the response with the parameters resptext and echotext in JSON format.

You can monitor the state and logs of your Web application deployed on SAP Cloud Platform. For more information, see Using Logs in the Eclipse IDE

 

Consume the Deployed Service in SAP Cloud Platform Workflow Service

Once you have the RFC exposed as a service, we can easily consume it in the SAP CP Workflow’s service task.

1. Create a SAP CP HTTP Destination for connecting to your Java service.

2. In the Workflow Editor drag & drop a service task into the editor’s canvas.

3. Enter the details as mentioned below:

  1. Destination: The SAP CP HTTP destination created for connecting to your service
  2. Path: The path to access the service apart from the destination.
  3. HTTP Method: GET (since we have implemented the GET method)
  4. Response Variable: ${context.<placeholder>} This is where the response will be available after the service call and it is ${context.result} in the example below.

4. You can continue with modelling the workflow and consume the output of the RFC call in other activities. If the response variable was ${context.result} then the value of echotext can be accessed via ${context.result.echotext}

Restrictions

  1. JCoServer functionality cannot be used within SAP Cloud Platform.
  2. Environment embedding, such as offered by JCo standalone 3.0, is not possible. This is, however, similar to SAP NetWeaver AS Java.
  3. Currently, a stateful sequence of function module invocations needs to occur in a single HTTP request/response cycle.
  4. Initially, only a logon with user/password credentials is supported.
  5. Provider/subscription model for applications is not fully supported. If you still want to use it, you need to make sure that destinations are named differently in all accounts.
  6. The supported set of configuration properties is restricted. For more information, see RFC Destinations.

Conclusion

It is sometimes inevitable that a backend RFC needs to be called when you are extending a business suite process with SAP Cloud Platform Workflow. It is fairly simple to expose a synchronous RFC as REST service which can return the results in JSON format and it is even more easier to consume those services in SAP Cloud Platform workflow.

 

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply