Encapsulating External Services as Application
Services
You can access remote function calls (RFC) or Web services (WS) within an application service. You can use the following procedure as an example of the how you could call remote BAPIs to retrieve detailed data about a user.

Automatic transactions are used for all interactions with the remote BAPI. The changes you make are visible only when the transaction is committed. For more information, see Transaction Support.
1. Import BAPI_USER_GET_DETAILas an external service in your application.
For more information on importing remote function calls, see Importing External RFC Services.
2. Create an application service UserServices:
a. In the Service Explorer, select the Application Services node with the secondary mouse button and choose New.
b. Enter UserServices as application name.
c. Choose Finish and wait until the application is created.
3. Add BAPI_USER_GET_DETAILas application dependency:
a. In the Service Explorer, select Application Services → UserServices.
b. Open the Dependencies tab page.
c. Select BAPI_USER_GET_DETAIL.
d.
Choose
with the quick info text Add.
4. Create a new data structure called UserData:
a. Open the Operations tab page.
b. In Attributes/Type Repository, select the node of your application with the secondary mouse button and choose New.
c. Enter Userdata as data structure name.
5. Add attributes to the data structure:
a. In Data Objects select the node Data Objects Catalog → Simple Types → com.sap.caf.core → shortText.
b.
Click
twice with
the quick info text Add.
The following attributes appear in Data Structure attributes:
■ arg0
■ arg1
c. Rename the attributes to:
■ shortText firstname
■ shortText lastname
d. Choose Finish.
6. Create the custom operation getUserDetails:
a. In Operations, choose Add.
The New Operation wizard opens.
Select Custom operation type and choose Next.
b. Enter the following data:
■ Name: getUserDetails
■ Description: Get user details
c. Choose Finish.
7. Set getUserDetailsparameters and exceptions:
a. In Attributes/Type Repository, select the node Catalog → Simple Types → com.sap.caf.core → shortText.
b. Choose Input to add the attribute as input parameter.
c. In Input, Output Parameters and Exceptions select the newly created node arg0.
d. In Properties, change Name from arg0 to id.
e. In Attributes/Type Repository, select the node of your application under Catalog → Data Structures.
f. Select the data structure Userdata and add it as output.
g. In Attributes/Type Repository, select the node Catalog → Faults → caf.core → Service Exception and add it as fault.
8. For the implementation of the import statements and the methods you need at least the following classes:
○ Container class for input parameters:
Subpackage names for output parameters have the following syntax:

input.Parameters.(RFC name in uppercase)
○ Container class for output parameters:
Subpackage names for output parameters have the following syntax:

output.Parameters.(RFC name in uppercase)_dot_response
○ Class for calling the RFC:
Package names for all classes related to the RFC have the following syntax:

com.sap.(CAF project name).extsrv.(RFC name in lowercase)
Class name for the reference to the RFC object has the following syntax:

(RFC name in lowercase)Local
It can be retrieved by calling a method with a name with the following syntax:

get(RFC name in uppercase)()

For this example, you use the following statement:
BAPI__USER__GET__DETAILLocal bapi_local =
this.getBAPI__USER__GET__DETAIL();
Each table or structure is a separate class within the input or output parameter classes. The names of the types and the parameters are identical to the names defined in the SAP system.

When entering source code, use the Code Completion feature to avoid mistakes.
9. To implement the import statements and the methods, do the following:
a. Select Implementation tab page.
b. Enter the following code for imports:

//@@custom code start - [imports]
import com.sap.caf.rt.exception.ServiceException;
import com.sap.yourProjectName.appsrv.datatypes.Userdata;
import com.sap.yourProjectName.extsrv.bapi__user__get__detail.output.Parameters;
//@@custom code end - [imports]
c. Enter the following code for method implementation:

//@@custom code start - getUserDetails(java.lang.String)
retValue = null;
// Create data container for Input-Parameter and fill it.
com.sap.yourProjectName.extsrv.bapi__user__get__detail.input.Parameters.BAPI__USER__GET__DETAIL
params = new com.sap.yourProjectName.extsrv.bapi__user__get__detail.input.Parameters.BAPI__USER__GET__DETAIL();
params.setUSERNAME(id);
// Get reference to the object representing the external service call.
BAPI__USER__GET__DETAILLocal bapi_local = this.getBAPI__USER__GET__DETAIL();
try {
// Call the BAPI.
// Return value of this method call contains all the data that the BAPI returns.
Parameters.BAPI__USER__GET__DETAIL_dot_Response result =
bapi_local.BAPI__USER__GET__DETAIL(params);
// Retrieve the address structure out of the result data container.
Parameters.BAPI__USER__GET__DETAIL_dot_Response.BAPIADDR3 addr =
result.getADDRESS();
// Fill the application services return structure.
retValue = new Userdata();
retValue.setFirstname(addr.getFIRSTNAME());
retValue.setLastname(addr.getLASTNAME());
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException(e);
}
//@@custom code end - getUserDetails(java.lang.String)
10. Save all metadata, generate project code, build and deploy.
11. Configure the system using the External Service Configurator (see External Service Configuration).