Skip to Content
Technical Articles

Consume SAP Sales Cloud (C4C) events with Kyma Function

Overview

This blog describes step by step how to do all the necessary configurations required to achieve event integration with SAP Sales Cloud (C4C) and SAP Cloud Platform, Kyma Runtime, by receiving events on an existent Kyma Function.

Prerequisites

  • A provisioned SAP Cloud Platform, Kyma runtime. Read this blog to get started. f you want to explore it individually or your company has no CEPA in place, the easiest way is to explore the trial account of SAP Business Technology Platform (BTP) as it is desribed in this tutorial.
  • An SAP Sales Cloud tenant.

Integration

These are the step to set up SAP Sales Cloud catalog service on the SAP Cloud Platform.

  1. Under Global Account on SAP Cloud Platform.
    • Select “System Landscape -> Systems; Click “Register System” button;
    • Click the “Register” button. After registering a new record will come up in “Pending” status.
    • Select “System Landscape -> Formations; Click “Create Formation” button;
  2. Login into SAP Sales Cloud portal using admin user and password, by using a similar URL https://myXXXXXX.crm.ondemand.com/.
    • Navigate under “Administrator -> General Settings -> Communication Systems”

      On the opened tab “All Communication Systems” click the “New” button from the top right. On the new form introduce all required information about the system including the ID of the system. In the current example, we gave the ID “US_SALES_MIDDLEWARE_DEMO”. As value for the “Host Name” field should be the Kyma host URL(for instance: https://gateway.XXXXXX.kyma.shoot.live.k8s-hana.ondemand.com)


      The system is in the
      “In Preparation” state/status. The target is to be into the “Active” state/status.

      Select the system record and activate it by clicking “Actions -> Set to Active”

    • Navigate under “Administrator -> General Settings -> Communication Arrangements”

      On the opened tab “All Communication Arrangements” click the “New” button from the top right. A wizard dialog will be opened.

      • Select from “Communication Scenarios”, the “OData Services for Business Objects” record.
      • Click the button “Next” from the bottom right.
      • Select created by you yearly system for this example “US_SALES_MIDDLEWARE_DEMO” on “System Instance ID”.
      • Click the button “Next” from the bottom right.
      • Select best fit “Authentication Method”; I selected “User ID and Password”. The “User ID” is autogenerated as “_US_SALES__0”, can be changed, as well specify the password by clicking the “Edit Credentials” button, we will need them later on. Select “Services Used” fitting to your uses case (selected are “activity”, “activityplanprocessing”, “contact”).
      • Click the button “Next” from the bottom right.
      • Review and click the button “Finish” from the bottom right.

      Now you should be able to see the created arrangements on the list.

    • Navigate under “Administrator -> General Settings -> Event Notification”
      On the opened tab under “All Consumers” click the “Add” button from the top right.

      • For field “Type” select “SAP Cloud Platform Extensions”. Give a convenient for you name, used “SALES_MIDDLEWARE_DEMO_KYMA”.
      • For the field “Remote Environment URL” go to SAP Cloud Platform Cockpit and take the token URL. Copy the generated URL token by clicking the key on registered early by you system.
      • For the field “Callback User” take the user that was autogenerated or specified by you, in our case is “_US_SALES__0”.
      • For the field “Callback Password” take the password that was specified by you for user “_US_SALES__0”, or of course your specified user.
      • Click the button “Save” from the bottom right.
      • Click new created consumer record “SALES_MIDDLEWARE_DEMO_KYMA” and activate it by clicking “Actions -> Activate”; State should change from “Inactive -> Active”
      • Select consumer record “SALES_MIDDLEWARE_DEMO_KYMA” and add subscriptions on the “Subscriptions” dialog and click the button “Add”. On popped dialog configure event subscription for which you are interested in to receive events on SAP Cloud Platform Kyma message broker or Cloud Foundry:
        1. As “Business Object” select “Visit”
        2. As “Node” select “Party”
        3. Enable “Created, Updated, Deleted, Enhanced Payload”
        4. Click the button “Save”

        Keep adding as much as you consider the subscription for which you want to receive events.

      After these steps on SAP Cloud Platform “System Landscape -> Systems”, the registered system should be in the “Registered” state.

  3. Navigate on SAP Cloud Platform to your sub-account where Kyma runtime is enabled already by clicking on the link, and a Kyma Console/Cockpit should open. Under “Applications/Systems” you should be able to see an integrated early created system under the name “mp-sap-cloud-for-customer-test”, and in-state/status “SERVING”.

    Details should look like this:

    Bind this system to a specific namespace by clicking “+ Create Binding”.

    Navigating to selected namespace into “Service Management -> Catalog”. You should be able to see the service.

    Details of created service:

    Provision the Service Class in the selected (an example is using “default”) Namespace, by clicking the “+ Add” button, and give a more friendly name.

  4. Bind “mp-sap-cloud-for-customer-test-pesky-bug” created service instance to an existent Kyma function in order that all events coming from SAP Sales Cloud to be consumed by serverless function. As well as using Kyma Cockpit subscribe to events.
  5. Create a basic function using Kyma console “Development -> Functions”, giving a name “c4c-consumers-events”.
    module.exports = { 
      main: async function (event, context) { 
        console.log("SAP Sales Cloud(C4C) event: %s", JSON.stringify(event.data)); 
        return 1; 
      } 
    }​


    After saving make sure that the final status is “Running”.

  6. Now is time to add triggers for events.
    Using Kyma Console/Cockpit “Operations -> Services”. Select “c4c-consumers-events-*” service. Click on the button “+ Add Event Trigger”, select events for which you want that function to be triggered.See logs of a function using Kyma Console/Cockpit or command line:

    kubectl logs -f <name of function pod> function
    
    
    On pod console logs, you should see messages with JSON like this:
    { "Changes": [ { "ChangedBy": "SAP_SYSTEM", "ChangedFields": [ { "Fieldname": "SystemAdministrativeData/LastChangeDateTime", "New": "2021-01-14T18:16:37.2393220Z", "Old": "2021-01-14T18:16:34.5940290Z" }, { "Fieldname": "VisitWorkItemGenerationCode", "New": "3", "Old": "1" }, { "Fieldname": "ChangeSource", "New": "JOB 20210114181637 ", "Old": "" } ], "Modification": "Updated", "ParentNodeID": "", "node": "Visit", "nodeID": "00163EAE7FFD1EEB95D282AD02F115EC" } ], "entity-id": "00163EAE7FFD1EEB95D282AD02F115EC", "root-entity-id": "00163EAE7FFD1EEB95D282AD02F115EC" }

     

  7. For communication, Kyma Function with SAP Sales Cloud OData API.
    Once there the bounding was created, Kyma will inject into the pod the following env variables:

    CONFIGURATION={"credentials":{"password":"....","username":"...."},"csrfConfig":{"tokenUrl":"https://myXXXXXX.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/"}}
    CREDENTIALS_TYPE=basicauth
    SAP_SALES_CLOUD_741C964E_854F_482E_A391_5A036C2403A9_GATEWAY_URL=http://default-gateway:8080/secret/c4c-sales-middleware-binding/api/SAP_SALES_CLOUD_741C964E_854F_482E_A391_5A036C2403A9
    SAP_SALES_CLOUD_741C964E_854F_482E_A391_5A036C2403A9_TARGET_URL=https://myXXXXXX.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/​

    This enables you to easily call the OData API from C4C without worrying about authentication.

    First, open the “Dependencies” tab and add:

    "dependencies": {
        "axios": "0.19.0"
    }
    

     

    Then go back to the “Source” tab and start with

    const axios = require("axios");
    const baseURL = `${process.env['SAP_SALES_CLOUD_***_GATEWAY_URL']}`; // this is where you need your GATEWAY_URL

    Afterward, replace the previous Kyma Function code with the following:

    module.exports = {
      main: async function (event, context) {
          // Get the accountId from the event payload 
          var visitId = event.data["entity-id"];
          console.log(`Visist ID from Event: ${visitId}`);
    
          try {
            const json = JSON.parse(process.env.CONFIGURATION)
            // read from C4C
            var response = await axios({
              method: 'get',
                url: `${baseURL}/VisitCollection('${visitId}')`,
                //params: {
                //  '$select': 'Field1,Field2'
                //},
                auth: {
                   username: json.credentials.username,
                   password: json.credentials.password
                }
            })
            entity = response.data.d.results;
            console.log("Visit Entity:")
            console.log(entity);
            
          } catch (error) {
            console.log("Error:")
            console.log(error);
            event.extensions.response.status(500).send("Error");
          }
        }
    }

    And that’s it!

This completes the whole setup and allows the function to start receiving events.

Now you have both events and APIs from SAP Sales Cloud connected to SAP Cloud Platform, Kyma Runtime.

 

Resource Links

Be the first to leave a comment
You must be Logged on to comment or reply to a post.