Skip to Content
0
Jan 17, 2020 at 10:12 AM

Calling BAPI from Java using SAP Cloud SDK version 3.0

4807 Views Last edit Jan 17, 2020 at 10:18 AM 2 rev

I am working on a POC currently where I need to call a BAPI inside S/4HANA On-Premise from a cloud application using Java. For this I am using SAP Cloud SDK.

For initial setup I followed these steps: https://developers.sap.com/tutorials/s4sdk-setup.html

I am trying to call BAPI_COSTCENTER_GETLIST1 from my application. Here is the code snippet:

final BapiRequestResult resultGetCostCenters = new BapiRequest("BAPI_COSTCENTER_GETLIST1")
 .withExporting("CONTROLLINGAREA", "BAPI0012_GEN-CO_AREA", "0001")
 .withTable("COSTCENTERLIST", "BAPI0012_CCLIST").end()
 .withTableAsReturn("BAPIRET2")
 .execute(destination);

And here the declaration of destination variable:

Map<String, String> destinationProps = new HashMap<String, String>();
destinationProps.put("URL", "<system_url>");
destinationProps.put("Type", "HTTP");
destinationProps.put("Password", "<system_password>");
destinationProps.put("Authentication", "BasicAuthentication");
destinationProps.put("User", "<system_user>");

final Destination destination= new DefaultDestination(destinationProps);

But from execute(), one exception in being thrown which says java.util.NoSuchElementException: No value present.

Here is the complete stack trace:

java.util.NoSuchElementException: No value present
	at io.vavr.control.Option$None.get(Option.java:526)
	at com.sap.cloud.sdk.s4hana.connectivity.rfc.SoapTransaction.before(SoapTransaction.java:56)
	at com.sap.cloud.sdk.s4hana.connectivity.rfc.RemoteFunctionRequestExecutor.execute(RemoteFunctionRequestExecutor.java:43)
	at com.sap.cloud.sdk.s4hana.connectivity.rfc.BapiRequest.execute(BapiRequest.java:78)
	at com.sap.csc.CostCenterCreationServlet.retrieveCostCenterList(CostCenterCreationServlet.java:73)
	at com.sap.csc.CostCenterCreationServlet.doGet(CostCenterCreationServlet.java:39)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.sap.cloud.sdk.cloudplatform.servlet.RequestAccessorFilter.lambda$doFilter$1(RequestAccessorFilter.java:71)
	at com.sap.cloud.sdk.cloudplatform.thread.AbstractThreadContextExecutor.lambda$execute$0(AbstractThreadContextExecutor.java:317)
	at com.sap.cloud.sdk.cloudplatform.thread.ThreadContextCallable.call(ThreadContextCallable.java:247)
	at com.sap.cloud.sdk.cloudplatform.thread.AbstractThreadContextExecutor.execute(AbstractThreadContextExecutor.java:319)
	at com.sap.cloud.sdk.cloudplatform.servlet.RequestAccessorFilter.doFilter(RequestAccessorFilter.java:71)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.sap.cloud.sdk.cloudplatform.security.servlet.HttpCachingHeaderFilter.doFilter(HttpCachingHeaderFilter.java:80)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.sap.cloud.sdk.cloudplatform.security.servlet.HttpSecurityHeadersFilter.doFilter(HttpSecurityHeadersFilter.java:41)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.filters.RestCsrfPreventionFilter.doFilter(RestCsrfPreventionFilter.java:113)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.base/java.lang.Thread.run(Thread.java:835)

I assume I am missing something very basic in it. Or probably I am doing it completely wrong. I could not find any well documented tutorial on this topic. So please correct me where I am going wrong.

Thanks in advance.

(SAP Cloud SDK Overview Page - https://help.sap.com/doc/0786ec6134c04c9bbafec959b6b737de/1.0/en-US/index.html)