Contact Free trial Login

SAP Connector Examples - Mule 4

Establish a SAP Connection

Create a Mule app that establishes a connection to SAP and receives a function using the SAP connector’s Function source operation. The Function source operation passes the export parameter back to SAP.

  1. Create a new Mule project in Anypoint Studio.

  2. Add the SAP connector to your project from Exchange.

  3. Drag the SAP Function source operation to the source area of the canvas at the start of a flow.

  4. Create a new SAP Inbound global element configuration and specify the environment values.

  5. Add a Transform Message component to the flow and specify the details based on the metadata.

    sap function return response

    The flow now contains Function source in the canvas source area and the Transform Message:

    sap flow response

    The SAP Design Studio displays feedback about the received object and its response:

    sap result sap gui

Send an IDoc to SAP

In this example, you create a Mule app to send an IDoc to SAP:

  1. Create a new Mule project in Anypoint Studio.

  2. Drag the HTTP Listener operation to the canvas and use the default values.

  3. Drag the HTTP Request operation to the canvas and set the Path to /sendIDoc. Use the same HTTP configuration as the HTTP Listener operation.

  4. Create a new SAP outbound global element configuration and specify the environment values.

  5. Click Test Connection to confirm that Mule runtime engine can connect with the SAP instance.

  6. If the connection is successful, save the configuration. Otherwise, review and correct any invalid parameters, and test again.

  7. Select the SAP Connector and add the Send IDoc operation next to the HTTP operation.

  8. Configure the operation with the following values:

    Parameter Value

    Display Name

    Send IDoc (or any other name you prefer).

    Connector configuration

    SAP_Outbound - The name of the global element you created.

    Key

    MATMAS01

    Content

    #[payload]

  9. Drag a Transform Message component before the SAP connector, and click the component to open its properties editor.

  10. Once metadata has been retrieved, select the respective fields to populate for the employee.

    The XML for the transform script shows the DataWeave content for Transform Message:

    %dw 2.0
    output application/xml
    ---
    read('<?xml version="1.0"?>
    <MATMAS01>
        <IDOC BEGIN="1">
            <EDI_DC40 SEGMENT="1">
                 <TABNAM>EDI_DC40</TABNAM>
                <MANDT>800</MANDT>
            </EDI_DC40>
        </IDOC>
    </MATMAS01>
    ',"application/xml")
  11. Add a Logger right after the SAP endpoint. When you run your app, Logger displays messages in the Anypoint Studio console that enables you to see the connector payload in the logs.

  12. Click File > Save to save your app.

  13. Click Run > Run as > Mule Application to run the app. Anypoint Studio provides a web server so that you can test the app from a browser.

  14. From a web browser, test the application by entering an employee’s internal ID, first name, and last name in the form of the following query parameters:

    http://localhost:8081/sendIDoc

    Mule runtime engine conducts the query and adds the employee record.

XML to Send an IDoc to SAP

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    xmlns:sap="http://www.mulesoft.org/schema/mule/sap"
    xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
    http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/http
    http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/sap
    http://www.mulesoft.org/schema/mule/sap/current/mule-sap.xsd
    http://www.mulesoft.org/schema/mule/ee/core
    http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <configuration-properties file="mule-artifact.properties"/>
    <sap:outbound-config name="SAP_Outbound" doc:name="SAP Outbound" >
        <sap:simple-connection-provider-connection
        applicationServerHost="${sap.jcoAsHost}"
        username="${sap.jcoUser}"
        password="${sap.jcoPasswd}"
        systemNumber="${sap.jcoSysnr}"
        client="${sap.jcoClient}"
        language="${sap.jcoLang}" />
    </sap:outbound-config>
    <http:listener-config name="HTTP_Listener_config"
      doc:name="HTTP Listener config" >
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
    <flow name="demo-idoc-clientFlow" >
        <http:listener config-ref="HTTP_Listener_config"
        path="/idoc" doc:name="Listener" />
        <ee:transform doc:name="Transform Message" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/xml
---
read('<?xml version="1.0"?>
<MATMAS01>
    <IDOC BEGIN="1">
        <EDI_DC40 SEGMENT="1">
             <TABNAM>EDI_DC40</TABNAM>
            <MANDT>800</MANDT>
        </EDI_DC40>
    </IDOC>
</MATMAS01>
',"application/xml")
]]></ee:set-payload>
    </ee:message>
    </ee:transform>
    <sap:send config-ref="SAP_Outbound" doc:name="Send IDoc"
    key="MATMAS01"/>
    <logger level="INFO" doc:name="Logger" message="#[payload]"/>
	</flow>
</mule>

See Also

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.