PM1: SAP Service notification

Seting up SAP for Service Notifictaion.

Step 1: In this lab we will create a SAP Notification using SAP application

➡️ Before we proceed with service notification we need to create a Functional location and Equipment.

Work with you instructor to gain access to SAP training system access.

  1. To create a Functional Location go to Transaction IL01 or from SAP Menu go to Logistics > Plant Maintenance > Technical objects > Functional Location > Create.

Populate the functional location in the format XXXX-XXX-AA-NN.XXXX-XXX-AA-NN.

Preparation menu

  • Populate Description, MaintPlant and Location and save.

Preparation menu

  1. To create an Equipment go to Transaction IE01 or from SAP Menu go to Logistics > Plant Maintenance > Technical objects > Equipment > Create.

Preparation menu

Preparation menu

  • Populate values based on avilable list and click on Save.

Preparation menu

  1. To create a notification, navigate to Logistics > Plant Maintenance > Maintenance Processing > Notifications > Create

Preparation menu

OR

Enter Transaction Code IW21 > to create SAP Service notification.

Preparation menu

  1. Select the Notification type menu to get the dropdown list and notification type as M2 − Malfunction report. Click Enter.

Preparation menu

FunctLoc : MUM1-THA-AB-02
Equipment : 299998888
Description 1: Desired value
Description 2:  Desired value

Note: Please reach our to your internal teams (SAP PM Technical consultants) if you do not see the notification type drop options.
  1. In the service notification screen, enter the Notification description FunctLoc,Equipment

  2. Go to the functional location field and search from the available list and select the functional location. For this Lab use the above values given.

    A functional location represents system area at which an object can be installed. Objects that can be installed at functional locations are called pieces of equipment in the SAPSystem. See SAP documentationfor more information.

  3. Next, select an equipment number by technical Id number and click the tick mark button to populate this. For this Lab use the above values given.

Preparation menu

  1. Click save to get the notification number.

Preparation menu

In this lab section we will create OData service (ABAP Sample) within SAP Gateway Foundation (SAP_GWFND)software component for AWS IoT to send service notification payload. In otherwords, we are automatating service notification with AWS IoT and AWS Lambda Integration.

OData is a Web protocol based on REST, for querying and updating data, applying and building on Web technologies such as HTTP, Atom Publishing Protocol (AtomPub), and RSS (Really Simple Syndication) to provide access to information from a variety of applications. It is easy to understand and extensible, and provides consumers with a predictable interface for querying a variety of data sources. Refer SAP documentation to understand OData with SAP.

Step 2: Creating a Structure in ABAP
  1. Using Transaction SE11, create the Structure ZSERVICE_MESSAGE as shown in the following screenshot. Activate the structure.

Preparation menu

Enter the following information for the ABAP field in the component section.

Component Typing Method Component Type
Type Types BAPI_MTYPE
MSG Types BAPI_MSG

Preparation menu

Note: Activate the structure.

Preparation menu

  1. Using Transaction SE11, create similar Structure as mentioned previously for notification. See ZPM_SERVICE_NOTIF_AWS_STR with the following Fields shown in screenshot.
Component Typing Method Component Type
NOTIF_NO Types QMNUM
EQUIPMENT Types EQUNR
FUNCT_LOC Types TPLNR
SHORT_TEXT Types QMTXT
LONG_TEXT Types TDLINE
MESSAGE Types ZSERVICE_MESSAGE_IOT

➡️ SAP Table-Field Component Type information.

  • QMNUM = Quality notification no field from the table QMEL - SAP Table used to store Quality Notification data.
  • EQUNR = Equipment number field from the table EQUI - SAP Table used to store Quality Notification data.
  • TPLNR = Functional Location field from the table IlOA - SAP Table used to store Functional Location information.
  • QMTXT = QMTXT is a standard field within SAP Table qmel that stores Description information
  • TDLINE = is a standard field within SAP Table that stores Text Line information

Preparation menu

Note: Activate the structure.

Preparation menu

Step 3: Create the ODATA Project to explore HTTPS endpoint for Post request.

By exposing SAP Business Suite functionality as REST-based OData (Open Data Protocol) services, SAP Gateway enables SAP applications to share data with a wide range of devices,technologies, and platforms in a way that is easy to understand and consume.**

Using REST services provides the following advantages:
  • Obtain human readable results; you can use your browser to see what data you will get.

  • Use stateless applications.

  • Receive related pieces of information, one leading to another.

  • Use standard GET, PUT, POST, DELETE, and QUERY. You can use the same format for both GET and PUT data.

Creating Gateway Service
  1. Using Transaction SEGW, create the SEGW ODATA Project.

Preparation menu

Project = Desired name
Description = Desired name

  1. Press OK button to display the project for next steps.

Preparation menu

  1. Right click the Data Model and create the NOTIF_CREATE entity by importing the structure ZPM_SERVICE_NOTIF_AWS_STR. Select DDIC structure under Import option.

Preparation menu

  1. Enter the desired entity name. For demonstration purpose we will use ‘NOTIF_CREATE’ and select the ABAP structure ‘ZPM_SERVICE_NOTIF_AWS_STR’ you created in the previous steps. Click Next in the wizard.

Preparation menu

  1. Select the required fields by choosing the checkbox and press Next.

Preparation menu

  1. Select a Key field for Entity type. Click the finish option to complete the step.

Preparation menu

  1. Once notif entity is created, we can find the entity ‘NOTIF_CREATE’ under Entity Type

Preparation menu

  1. Click the generate button and Model Provider Class (MPC), and a new Data Provider Class (DPC) will get generated.

Preparation menu

  • In the next screen: Copy the Technical Service name into your notepad to register the service. Click green check mark, and click local object to complete the runtime generation.

Preparation menu

  1. Click the generate button again.

Preparation menu

  1. The following message will be displayed once class created.

Preparation menu

Step 4: Abap logic for Create Entity

  1. Go to Service Implementation menu and right click on the CREATE option under NOTIF_CREATESET and click Go To Abap Workbench write the logic for Create Entity.

Preparation menu

  1. Acknowledge the message by clicking green check mark.

Preparation menu

  1. Find the create NOTIF_CREATESET_CREATE_ENTITY under methods. Right click and click onto redefine option to customize the entity.

Preparation menu

  1. Access Redefinition folder and copy and paste the below code in method NOTIF_CREATESET_CREATE_ENTITY.

Preparation menu

Please note this is a test code to demostrate the integration step between SAP and AWS service. You can review the commented notes for more information. For any production use cases you should create technical specification according to your internal business requirements and best practices.

This Abap program calls the SAP standard CALL FUNCTIONS BAPI_ALM_NOTIF_CREATE, BAPI_ALM_NOTIF_SAVE & BAPI_TRANSACTION_COMMIT to create a SAP PM service notification when a http post request is generated from external systems.

METHOD notif_createset_create_entity.
  "Data Declarations
  DATA: lwa_entity_header TYPE zcl_zpm_service_notifi_mpc=>ts_notif_create,     "Entity Header
        lwa_notif_header  TYPE bapi2080_nothdri,                                "Notification Header SAP ABAP Table BAPI2080_NOTHDRI (BAPI Service Notification Header for Creation)
        lwa_notif_export  TYPE bapi2080_nothdre,                                "Notification Export Value from SAP ABAP Table BAPI2080_NOTHDRE (BAPI Service Notification Header)
        lwa_notif_export2 TYPE bapi2080_nothdre,                                "Notification Export Value
        lwa_longtext      TYPE bapi2080_notfulltxti,                            "WA for Long Text - SAP ABAP Table BAPI2080_NOTFULLTXTI (Work table for Full texts to message objects)
        lt_longtext       TYPE TABLE OF bapi2080_notfulltxti,                   "IT for Long Text
        lt_return         TYPE TABLE OF bapiret2,                               "IT for Return Table - BAPIRET2 is an SAP Structure so does not store data like a database table does but can be used to process "Return Parameter" Information within sap
        lwa_return        TYPE bapiret2,                                        "WA for Return Table
        lv_msg            TYPE bapi_msg,                                        "Message
        lv_msgv1          TYPE symsgv,                                          "Message Value -standard DATA Element within the SAP ABAP dictionary
        lv_rollback_flag  TYPE c VALUE IS INITIAL.                              "Roll Back Flag

  "Clear Flag
  CLEAR lv_rollback_flag.

  "Read the Payload file using ‘READ_ENTRY_DATA’ and store into LWA_ENTITY_HEADER variable 
  io_data_provider->read_entry_data( IMPORTING es_data = lwa_entity_header ).

  "Fill the Equipment, Functional Location and Short text in Header record LWA_NOTIF_HEADER variable
  lwa_notif_header-equipment  = lwa_entity_header-equipment.      "Equipment
  lwa_notif_header-funct_loc  = lwa_entity_header-funct_loc.      "Functional Location
  lwa_notif_header-short_text  = lwa_entity_header-short_text.    "Short Text

  "Fill the Long text in LT_LONGTEXT variable
  lwa_longtext-objtype = 'QMEL'.
  lwa_longtext-objkey = '00000000'.
  lwa_longtext-format_col = '*'.
  lwa_longtext-text_line = lwa_entity_header-long_text.
  APPEND lwa_longtext TO lt_longtext.

  "Use Notification Type as ‘M2’ and pass both the Header record(LWA_NOTIF_HEADER)
  "Long text(LT_LONGTEXT) in BAPI BAPI_ALM_NOTIF_CREATE to get the temporary Service notification number
  CALL FUNCTION 'BAPI_ALM_NOTIF_CREATE'
    EXPORTING
      notif_type         = 'M2'
      notifheader        = lwa_notif_header
    IMPORTING
      notifheader_export = lwa_notif_export
    TABLES
      longtexts          = lt_longtext
      return             = lt_return.
  IF lwa_notif_export IS NOT INITIAL.
    "Pass the temporary notification number LWA_NOTIF_EXPORT-NOTIF_NO to BAPI_ALM_NOTIF_SAVE to generate "the Service Notification number 
    CALL FUNCTION 'BAPI_ALM_NOTIF_SAVE'
      EXPORTING
        number      = lwa_notif_export-notif_no
      IMPORTING
        notifheader = lwa_notif_export2
      TABLES
        return      = lt_return.

    "Check whether the return table contains any Errors
    IF NOT ( line_exists( lt_return[ type = 'E' ] ) OR line_exists( lt_return[ type = 'A' ] ) ).
      "Commit the work
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = abap_true.

      "Update the Entity Structure
      MOVE-CORRESPONDING lwa_entity_header TO er_entity.

      "Assign the Notification Number
      er_entity-notif_no = lwa_notif_export2-notif_no.

      "Message Type
      er_entity-message-type = 'S'.

      "Form the Message
      CONCATENATE 'Notification'
                  lwa_notif_export2-notif_no
                  'saved'
             INTO er_entity-message-msg SEPARATED BY space.

      "Get the Header message container
      DATA(lo_message_container) = me->mo_context->get_message_container( ).

      "Get the Notification number
      lv_msgv1 = lwa_notif_export2-notif_no.

      "Add the Message to the Message container by calling method ADD_MESSAGE
      lo_message_container->add_message(
                            EXPORTING
                              iv_msg_id = 'IM'
                              iv_msg_number = '405'
                              iv_msg_type = 'S'
                              iv_msg_v1 = lv_msgv1
                              iv_is_leading_message = abap_true
                              iv_add_to_response_header = abap_true
                              ).
    ELSE.
      "When Return table contains error record, update the Roll back Flag
      lv_rollback_flag = abap_true.
    ENDIF.
  ELSE.
    "When Return table contains error record, update the Roll back Flag
    lv_rollback_flag = abap_true.
  ENDIF.

  "Check whether any records are generated in LT_RETURN table
    "If any error records found, the following actions done
    "Return the error message to output entity ER_ENTITY and 
    "Update the error message in Header Message container 
    "Rollback all the changes done in Notification creation using BAPI_TRANSACTION_ROLLBACK
    "If the transaction success, 
    "Return the Success message to output entity ER_ENTITY 
    "Update the Success message in Header Message container 
    "Commit the Transaction using BAPI_TRANSACTION_COMMIT

  IF lv_rollback_flag IS NOT INITIAL.

    "Rollback the work
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

    "Update Message type as 'Error'
    er_entity-message-type = 'E'.

    "Get the Error Message
    lwa_return = VALUE #( lt_return[ type = 'E' ] DEFAULT space ).

    "Assign error message to output structure
    er_entity-message-msg = lwa_return-message.

    "Get the Header message container
    lo_message_container = me->mo_context->get_message_container( ).

    "Get the return message
    lv_msg = lwa_return-message.

    "Add the Message to the Message container by calling method ADD_MESSAGE
    lo_message_container->add_message(
                          EXPORTING
                            iv_msg_id = 'IM'
                            iv_msg_number = '405'
                            iv_msg_type = 'E'
                            iv_msg_text = lv_msg
                            iv_is_leading_message = abap_true
                            iv_add_to_response_header = abap_true
                            ).
  ENDIF.
ENDMETHOD.

  1. Align the code by clicking pretty printer & click check option to verify any errors.

Preparation menu

  1. Click Activate icon and click the green check mark.

Preparation menu

Step 4: Register the ODATA Service
  1. To register the OData service go to SAP transaction /n/IWFND/MAINT_SERVICE and click Add Service option. Search for the the service by using the Technical service name you copied in the previous steps. (example: ZSERVICE_PM_NOTIFICATION_SRV), select co-deployed option and click Get Services

Preparation menu

  1. Select your service and click Add Selected Services option.

Preparation menu

  1. For this lab save the service locally. Click local and click the ok option.

Preparation menu

  1. Find your service to access to the SAP Gateway client.

Preparation menu

  1. Click SAP Gateway client.

Preparation menu

  1. Select the Entity set to test. We will select the Entity set we created for our service Notif_CreateSet.

Preparation menu

  1. Click the POST radio option and populate the below payload in the HTTP request part of the protocal, see the screenshot below. This is the payload that we will send as part of the this IoT lab. Click Execute, verify the https response code = 201 success and copy the Notification number.
{
  "d" : {
    "Equipment" : "299998888",
    "FunctLoc" : "MUM1-THA-AB-02",
    "ShortText" : "Temperature Alarm",
    "LongText" : "Temperature Alarm in the Compressor Motor"
  }
}

Preparation menu

Note: Copy the request URI to update the service name and entity set name in AWS CDK for AWS Lambda PYODATA runtime connector to call this service name and commit a rest API post request.

o odpEntitySetName - NOTIF_CREATESet o odpServiceName - ZPM_SERVICE_NOTIFICATION_SRV

Step 5: Register the ODATA Service
  1. Go to SAP Transaction IW23
  2. Input the SAP Service Maintenance Notification generated. Click enter and verify the entries.

Congratulations! You have completed your first step to integrate SAP with AWS IoT services for Preventive Maintenance. Let’s proceed to the next step to complete the AWS IoT and Integration setup.