Automating Sales Order Entry in SAP using Google Cloud's AI services

Google recently released ABAP SDK for Google Cloud, providing bi-directional, real-time integration between SAP and Google Cloud services. SAP developers can easily leverage this SDK to integrate their SAP applications with Google Cloud services such as Vertex AI, Document AI, Translation AI, Pub/Sub, and more. With the ABAP SDK, customers can accelerate their digital transformation and achieve business goals faster.

This blog provides a walkthrough of an art of the possible use case for democratizing AI for SAP developers through ABAP SDK for Google Cloud. In this demo, we showcase a way to automate sales order entry in SAP which is a common business process with SAP customers, using Google’s AI services.

Problem Statement

A mass manufacturer of industrial fasteners receives a significant portion of their purchase orders in un-structured formats (e.g. hand written forms or PDFs sent as email attachments). Customer service reps at the company use SAP to manage Sales Orders and the challenges currently faced by them are,

  • Time-consuming and error-prone order entry: Manually entering orders is time-consuming and error-prone, which can lead to costly returns and decreased customer satisfaction.
  • Language barriers: The company has a global customer base, and sometimes orders are placed in the local language. This can be difficult for customer service representatives to understand important shipping/handling instructions, and they may have to use third-party translation services, which can be costly.
  • Incorrect delivery addresses: Delivery addresses in purchase order forms are not always correct, which can lead to unwanted delivery delays.

The company is looking to address these challenges by automating the order entry process with Google’s AI services.

How ABAP SDK for Google Cloud can help

ABAP Developers supporting SAP ERP applications at the organization can now use the ABAP SDK for Google Cloud to solve the challenges listed above by:

  • Loading the purchase order documents stored in Google Cloud Storage buckets,
  • Parsing the contents of the purchase order using DocumentAI purchase order parser,
  • Validating the delivery address using Address Validation API,
  • Translating delivery instructions from customer’s language to English using TranslationAI API,
  • Sending a notification to the customer about successful creation of Sales Order through Cloud Pub/Sub.

Google Cloud’s services are enabled as ABAP client library classes within ABAP SDK for Google Cloud, and the SDK can be leveraged to invoke the APIs listed above directly from SAP modules for order processing.

“ABAP SDK for Google Cloud abstracts the complexities of integration with Google’s services through these classes and provides a “Single-window of Interaction” for SAP developers. Customers need not invest time and effort in building complex SAP applications to consume Google’s services by following Google’s Discovery Document based REST API specifications and can directly consume the API resources using SDK’s client library classes in a simple two step approach —
1. Instantiate the client library class for Google’s service, and
2. Call the class’s method to invoke the API’s resource operation.”

Below is a visual representation of the customer service representative’s user journey of the company for automatic order processing with Google’s AI services using ABAP SDK for Google Cloud.

Sales Order Entry 1.jpgUser Journey of the Customer Service Representative

Prerequisites

Let’s look at some prerequisites before deep diving into the solution in detail.

  • A Google Cloud service account with required IAM permissions to create, manage and access artifacts for Google’s Cloud Storage, Cloud Document AI, Address Validation, Translation AI, Cloud Pub/Sub Services.
  • SAP software installed as per guidelines here.
  • ABAP SDK for Google Cloud installed and configured in the SAP system as per guidelines here.

Solution in detail with code snippets

Here is a high level architecture of the components and flow of the solution.

deveshsapcloud_1-1698750415042.jpeg
High level architecture diagram

An SAP ABAP program can be written to run as a background job to run at a frequency and leverage client library classes of ABAP SDK for Google Cloud to,

  • Poll the GCS bucket for new Purchase Orders,
  • If a new PO is received,
    (a) orchestrate the calls to the GCS bucket to list and read POs,
    (b) parse and extract PO information,
    (c) validate addresses,
    (d) translate delivery text,
    (e) create sales order by calling SAP standard BAPI,
    (f) send notification to Cloud Pub/Sub.

A Fiori Application can be built to introduce human review for documents in case of any parsing errors or incorrect addresses, the same can be “scanned” in foreground and corrected before clicking on a button to “Create” Sales Orders.

In case of errors within the library classes, ABAP SDK would log errors within SAP BAL, please refer to “Application Logging” within ABAP SDK for more details.

Below are the SDK’s client library classes for the Google’s services used in the solution.

  • Cloud Storage V1 — /GOOG/CL_STORAGE_V1
  • Cloud Document AI V1 — /GOOG/CL_DOCUMENTAI_V1
  • Address Validation V1 — /GOOG/CL_ADDRVALDN_V1
  • Cloud Translation V2 — /GOOG/CL_TRANSLATION_V2
  • Cloud Pub/Sub V1 — /GOOG/CL_PUBSUB_V1

Code Snippets

Here are some code snippets to showcase implementation for each step using ABAP SDK for Google Cloud’s client library classes.

List Purchase Orders in Cloud Storage Bucket
Call method “LIST_OBJECTS” of class /GOOG/CL_STORAGE_V1 to list “Purchase Orders” in the Cloud Storage bucket and collect the list of file objects for Purchase Orders to be processed in an internal table.

DATA lt_objects TYPE /goog/cl_storage_v1=>ty_t_013.
TRY.
* Open HTTP Connection by instantiating client class for GCS
    DATA(lo_storage_client) = NEW /goog/cl_storage_v1( iv_key_name = 'client_key' ).
* Call ABAP SDK's GCS client library class method to list file objects
    CALL METHOD lo_storage_client->list_objects
      EXPORTING
        iv_q_prefix = p_prefix
        iv_p_bucket = p_bucket
      IMPORTING
        es_output   = DATA(ls_output)
        ev_ret_code = DATA(lv_ret_code)
        ev_err_text = DATA(lv_err_text)
        es_err_resp = DATA(ls_err_resp).
* Collect list of file objects for further processing
    APPEND LINES OF ls_output-items TO lt_objects.
* Close HTTP Connection
    lo_storage_client->close( ).
  CATCH /goog/cx_sdk INTO DATA(lo_cx_sdk).
*Handle Exceptions
ENDTRY.

Get content of the Purchase Orders
For each file object, call method “GET_OBJECTS” of class /GOOG/CL_STORAGE_V1 by passing the file names to get the content of the Purchase Orders, the content is returned in “XSTRING” format.

DATA lv_content TYPE xstring.
TRY.
* Open HTTP Connection by instantiating client class for GCS
    DATA(lo_storage_client) = NEW /goog/cl_storage_v1( iv_key_name = 'client_key' ).
    lo_storage_client->add_common_qparam( iv_name  = 'alt' iv_value = 'media' ).
* Call ABAP SDK's GCS client library class method to read file objects content
    lo_storage_client->get_objects(
      EXPORTING
        iv_p_bucket = p_bucket
        iv_p_object = lv_object_name
      IMPORTING
        ev_ret_code = DATA(lv_ret_code)
        ev_err_text = DATA(lv_err_text)
        es_err_resp = DATA(ls_err_resp)
        es_raw      = lv_content ).
* Close HTTP Connection
    lo_storage_client->close( ).
  CATCH /goog/cx_sdk INTO DATA(lo_cx_sdk).
*Handle Exceptions
ENDTRY.

Parse content of the Purchase Orders
Parse each PO file content to get purchase order information.
(a) Transform the content of Purchase Order to Base 64 encoded string using SAP function module “SCMS_BASE64_ENCODE_STR”.
(b) Call method “PROCESS_PROCESSORS” of class /GOOG/CL_DOCUMENTAI_V1 to extract Purchase Order entities using the “Purchase Order” parser of Document AI API by passing file content in base 64 encoded string and mime type of the file (Code Sample to get the mime type of the file).
(c) Entities of the document after parsing can be referenced from response structure LS_OUTPUT-DOCUMENT-ENTITIES.

DATA ls_input TYPE /goog/cl_documentai_v1=>ty_084.
DATA lv_output TYPE string.
DATA lv_project_id TYPE string.
* Call function module to convert file content in XSTRING format to Base 64 encoded string
CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
  EXPORTING
    input  = lv_content
  IMPORTING
    output = lv_output.
TRY.
* Open HTTP Connection by instantiating client class for Document AI
    DATA(lo_docai_client) = NEW /goog/cl_documentai_v1( iv_key_name = 'client_key' ).
    lv_project_id = lo_docai_client->gv_project_id.
    ls_input-raw_document-content   = lv_output.
    ls_input-raw_document-mime_type = p_mime_type.
* Call ABAP SDK's Document AI client library class method to process a PO document
    CALL METHOD lo_docai_client->process_processors
      EXPORTING
        iv_p_projects_id   = lv_project_id
        iv_p_locations_id  = p_location
        iv_p_processors_id = p_po_processor_id
        is_input           = ls_input
      IMPORTING
        es_output          = DATA(ls_output)
        ev_ret_code        = DATA(lv_ret_code)
        ev_err_text        = DATA(lv_err_text)
        es_err_resp        = DATA(ls_err_resp).
* Close HTTP Connection
    lo_docai_client->close( ).
  CATCH /goog/cx_sdk INTO DATA(lo_cx_sdk).
*Handle Exceptions
ENDTRY.

Validate Sold To and Ship To customer addresses
For each Purchase Order information extracted,
(a) Call method “VALIDATE_ADDRESS” of class /GOOG/CL_ADDRVALDN_V1 to extract the standard address for the delivery address for Sold To and Ship To customers.
(b) SAP customer master table “KNA1” can then be queried to fetch the Sold To and Ship To customer numbers based on the standard addresses returned by the Address Validation API.

DATA lv_project_id TYPE string.
DATA ls_input TYPE /goog/cl_addrvaldn_v1=>ty_012.
DATA lv_address TYPE string.
TRY.
* Open HTTP Connection by instantiating client class for Address Validation
    DATA(lo_addrvaldn_client) = NEW /goog/cl_addrvaldn_v1( iv_key_name = 'client_key' ).
    lv_project_id = lo_addrvaldn_client->gv_project_id.
    APPEND lv_address TO ls_input-address-address_lines.
* Call ABAP SDK's Address Validation client library class method to validate an address
    CALL METHOD lo_addrvaldn_client->validate_address
      EXPORTING
        is_input    = ls_input
      IMPORTING
        es_output   = DATA(ls_output)
        ev_ret_code = DATA(lv_ret_code)
        ev_err_text = DATA(lv_err_text)
        es_err_resp = DATA(ls_err_resp).
* Get valid standardized address from the API response
    DATA(lv_standardized_address) = ls_output-result-usps_data-standardized_address.
* Close HTTP Connection
    lo_addrvaldn_client->close( ).
  CATCH /goog/cx_sdk INTO DATA(lo_cx_sdk).
*Handle Exceptions
ENDTRY.

Translate Delivery Instructions
For each Purchase Order information extracted,
(a) Call method “DETECT_DETECTIONS” of class /GOOG/CL_TRANSLATION_V2 to detect the language of the delivery instructions.
(b) If the language is not “English”, call method “TRANSLATE_TRANSLATIONS” of class /GOOG/CL_TRANSLATION_V2 to translate the delivery instructions to english.

DATA lv_project_id TYPE string.
DATA ls_input_detect TYPE /goog/cl_translation_v2=>ty_001.
DATA ls_detections TYPE /goog/cl_translation_v2=>ty_t_003.
DATA lt_comments TYPE STANDARD TABLE OF string.
DATA ls_input_translate TYPE /goog/cl_translation_v2=>ty_006.
DATA lv_delivery_comments TYPE string.
DATA lv_comments_language TYPE string.
TRY.
* Open HTTP Connection by instantiating client class for Translation AI
    DATA(lo_translate_client) = NEW /goog/cl_translation_v2( iv_key_name = 'client_key' ).
    lv_project_id = lo_translate_client->gv_project_id.
    APPEND lv_delivery_comments TO ls_input_detect-q.
* Call ABAP SDK's Translation AI client library class method to detect language of delivery instructions
    CALL METHOD lo_translate_client->detect_detections
      EXPORTING
        is_input    = ls_input_detect
      IMPORTING
        es_output   = DATA(ls_output_detect)
        ev_ret_code = DATA(lv_ret_code)
        ev_err_text = DATA(lv_err_text)
        es_err_resp = DATA(ls_err_resp).
* Get language of delivery instructions from the API response
    ls_detections = ls_output_detect-data-detections[ 1 ].
    lv_comments_language = ls_detections[ 1 ]-language.
    IF lv_comments_language NE 'en'.
    APPEND lv_delivery_comments TO lt_comments.
    ls_input_translate-q = lt_comments.
    ls_input_translate-format = 'text'.
    ls_input_translate-target = 'en'.
    ls_input_translate-source = lv_comments_language.
* Call ABAP SDK's Translation AI V2 client library class method to translate delivery instructions to English
    lo_translate_client->translate_translations(
      EXPORTING
        is_input  = ls_input_translate
      IMPORTING
        es_output = DATA(ls_output_translate) ).
* Get translated delivery instructions in English from the API response
    DATA(ls_translated_text) = ls_output_translate-data-translations[ 1 ].
    DATA(lv_translated_comments) = ls_translated_text-translated_text.
  ENDIF.
* Close HTTP Connection
    lo_translate_client->close( ).
  CATCH /goog/cx_sdk INTO DATA(lo_cx_sdk).
*Handle Exceptions
ENDTRY.

Create Sales Orders from POs
Prepare parameters of BAPI “BAPI_SALESORDER_CREATEFROMDAT2” by referring to the extracted entities from Purchase Order parsing through Document AI, validated addresses of sold to and ship to customers and translated delivery instructions and call BAPI to create Sales Order.

Publish an order confirmation message to a Cloud Pub/Sub topic
For each Sales Order created,
(a) Take into account the sales order number returned by the BAPI.
(b) Call method “PUBLISH_TOPICS” of class /GOOG/CL_PUBSUB_V1 to publish a message with sales order number to a Cloud Pub/Sub topic.

DATA lv_project_id TYPE string.
DATA ls_message    TYPE /goog/cl_pubsub_v1=>ty_025.
DATA ls_input      TYPE /goog/cl_pubsub_v1=>ty_023.
TRY.
* Open HTTP Connection by instantiating client class for Cloud PubSub
    DATA(lo_pubsub_client) = NEW /goog/cl_pubsub_v1( iv_key_name = 'client_key' ).
    lv_project_id = lo_pubsub_client->gv_project_id.
    DATA(lv_msg) = | Sales Order { lv_sales_order_number } successfully created |.
    ls_message-data = cl_http_utility=>encode_base64( unencoded = lv_msg ).
    APPEND ls_message TO ls_input-messages.
* Call ABAP SDK's Cloud Pub Sub client library class method to publish a notification
    CALL METHOD lo_pubsub_client->publish_topics
      EXPORTING
        iv_p_projects_id = lv_project_id
        iv_p_topics_id   = p_topic_name
        is_input         = ls_input
      IMPORTING
        es_output        = DATA(ls_output)
        ev_ret_code      = DATA(lv_ret_code)
        ev_err_text      = DATA(lv_err_text)
        es_err_resp      = DATA(ls_err_resp).
* Close HTTP Connection
    lo_pubsub_client->close( ).
  CATCH /goog/cx_sdk INTO DATA(lo_cx_sdk).
*Handle Exceptions
ENDTRY.

An Example

Below is a demonstration of the above steps to extract information from a Purchase Order document GCS bucket and creating a Sales Order using Google Cloud’s AI services.

deveshsapcloud_8-1698750851696.pngAn example of automatic Sales Order Entry using ABAP SDK for Google Cloud

Human Review of documents in errors

A Fiori Application can be built to introduce human review for documents in case of any parsing errors or incorrect addresses, the same can be “scanned” in foreground and corrected before clicking on a button to “Create” Sales Orders.

deveshsapcloud_9-1698750976802.pngHuman review of documents in errors

Results & Impact

By automating the order entry process, the company is able to achieve the following business benefits,

  • Reduce order processing time by 80%. On average, it used to take ~10 mins to create an order manually in the system. Using the ABAP SDK to integrate with Google Cloud Cloud services, customer service representatives are able to create the orders in 1–2 minutes.
  • Reduce delivery errors and shipping costs ($xx) and improve time to delivery (xx days) through proactive address validation
  • Improve order accuracy and reduce manual effort by translating customer delivery instructions accurately
  • Provide near real-time order updates to customers using Cloud Pub/Sub.
  • Reduce custom development effort and cost ($xx) by using in-house ABAP development skills.
  • Reduce cost ($xx) by not using 3rd party middleware solutions or integration platforms, rather directly consuming Google Cloud services in ABAP.
  • Reduce integration errors (xx%) by reducing the number of integration hops.

Summary

ABAP SDK for Google Cloud makes it easier and convenient for SAP developers to connect to and use Google Cloud services natively from within their SAP ERP applications. This means that ABAP developers can use the language they are familiar with to access the power of Google Cloud, which can help them improve the performance, scalability, and security of their SAP ERP applications. ABAP SDK for Google Cloud provides “Single-window of interaction” for developers to consume Google Cloud services through client library classes and abstracts the complexities within the SDK’s foundation. SAP developers can just instantiate and call class methods to start their development journey without worrying about complexities such as authentication,etc..

Start your development journey with ABAP SDK for Google Cloud by downloading and installing the SDK free from the link here. Quick Start Guides and Code samples for enabled Google Services can be referenced from the public documentation.

 
Contributors
Comments
Kartik_Dodiya
Bronze 5
Bronze 5

This is a really good idea to implement in the business. Can we automate more use cases like this? If yes then can you provide its information?

Hello Kartik,

Yes....we can think of numerous SAP use cases that can be solved with Google's services....enabled by the ABAP SDK. It's really on the customers on how they would like to plug in the SDK into their architectures/solutions. If you are looking for some reference architectures, I would like to point you to our YouTube playlist which has quick videos for various industry use cases, along with reference architectures.  Also, here are our blogs to detail out the use case implementations in the videos.

If you have any specific use case that you would like help/insights on, feel free to post here and we can setup a meeting to help you.

Version history
Last update:
‎10-31-2023 04:25 AM
Updated by: