Groovy script to test SAP RFC calls

Sometimes you need to execute RFC into SAP from hybris before implementation.

To do this you need to add hybris OOTB extensions, required for execution of SAP RFC.

1
2
3
        <!-- SAP RFC integration extensions -->
<extension name="sapcorejco"/>
<extension name="sapcoreconfiguration"/>

And than groovy script can be executed from HAC (don’t forget to change script parameters before execution)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import com.sap.conn.jco.JCoFunction
import de.hybris.platform.sap.core.configuration.model.SAPConfigurationModel
import de.hybris.platform.sap.core.configuration.model.SAPRFCDestinationModel
import de.hybris.platform.sap.core.jco.connection.JCoConnection
import de.hybris.platform.sap.core.jco.connection.JCoManagedConnectionContainer
import de.hybris.platform.servicelayer.model.ModelService
import de.hybris.platform.servicelayer.search.FlexibleSearchService
import de.hybris.platform.site.BaseSiteService
import de.hybris.platform.store.services.BaseStoreService

ModelService modelService = spring.getBean("modelService")
FlexibleSearchService flexibleSearchService = spring.getBean("flexibleSearchService")
BaseStoreService baseStoreService = spring.getBean("baseStoreService")
BaseSiteService baseSiteService = spring.getBean("baseSiteService")

JCoManagedConnectionContainer managedConnectionContainer = spring.getBean("sapCoreManagedConnectionContainer")

// Set session data
baseSiteService.setCurrentBaseSite("BASE_SITE", true)

// Get or create test destination
SAPRFCDestinationModel destination = modelService.create(SAPRFCDestinationModel.class)
destination.setRfcDestinationName("RFC_CODE")
destination.setTargetHost("TARGET_HOST")
destination.setInstance("TARGET_INSTANCE")
destination.setClient("TARGET_CLIENT_NUMBER")
destination.setUserid("USER_NAME")
destination.setPassword("PASSWORD")
List<SAPRFCDestinationModel> exampleDestinations = flexibleSearchService.getModelsByExample(destination)
if (!exampleDestinations.isEmpty()) {
    destination = exampleDestinations.get(0)
} else {
    modelService.save(destination)
}

// Get or create sap configuration
SAPConfigurationModel configurationModel = modelService.create(SAPConfigurationModel.class)
configurationModel.setCore_name("TestRFCConfiguration")
List<SAPConfigurationModel> exampleCofigurations = flexibleSearchService.getModelsByExample(configurationModel)
if (!exampleCofigurations.isEmpty()) {
    configurationModel = exampleCofigurations.get(0)
}
// Assign destination and base store to configuration
configurationModel.setBaseStores(baseStoreService.getAllBaseStores())
configurationModel.setSAPRFCDestination(destination)
modelService.save(configurationModel)

// Execute function
JCoConnection managedConnection = managedConnectionContainer.getManagedConnection("JCoStateless")
// Below is defined on of the standard SAP RFC
final JCoFunction function = managedConnection.getFunction("STFC_CONNECTION")
function.getImportParameterList().setValue("REQUTEXT", "Hello Backend")
managedConnection.execute(function)
final String result = (function.getExportParameterList().getString("RESPTEXT"))
println("Result is " + result)
comments powered by Disqus