Financial Management Blogs by SAP
Get financial management insights from blog posts by SAP experts. Find and share tips on how to increase efficiency, reduce risk, and optimize working capital.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member231153
Active Participant
2,784
With SAP S/4HANA 1809, you can use an email integration functionality with SAP Cash Application, to fetch payment advices from your inbox and then extract its relevant information with machine learning and computer vision.

So far, you had to upload your payment advices manually in the Fiori app Manage Payment Advices. For that, you first had to identify the emails containing payment advice information.

This is how you can make your life easier now:

Prerequisites

  • You have acquired the license for SAP Cash Application.

  • You have enabled the machine learning service Payment Advice Extraction as part of SAP Cash Application on your S/4HANA system (via the customizing activity Enable Machine Learning for Payment Advices).

  • You have access to the oData service <hostname>/sap/opu/odata/sap/FAR_MANAGE_PAYMENT_ADVICES_SRV/PaymentAdviceFileSet/ running on your system.


Create a simple connector to read the e-mails and post them to the oData service


In this example the connector was created in Python, however, you can use other languages as well.

Sample code:


from config import EXCHANGE_USER, EXCHANGE_PASSWORD, EXCHANGE_SERVER
from exchangelib import DELEGATE, Account, Credentials, Configuration, UTC_NOW
from datetime import timedelta
import requests
import time


def upload_attachment(attachment):

headers = dict()
headers['x-csrf-token'] = 'Fetch'
headers['Authorization'] = 'Basic abc123'
headers['Content-Type'] = 'application/pdf'

fetch_result = requests.head('http://s4hanaserver:port/sap/opu/odata/sap/FAR_MANAGE_PAYMENT_ADVICES_SRV/', headers=headers)

if fetch_result.status_code == 200:
del headers['x-csrf-token']
headers['x-csrf-token'] = fetch_result.headers['x-csrf-token']
headers['slug'] = 'companycode=1010;filename=' + attachment.name
files = {'file': (attachment.name, attachment.content)}

upload_result = requests.post('http://s4hanaserver:port/sap/opu/odata/sap/FAR_MANAGE_PAYMENT_ADVICES_SRV/PaymentAdviceFil Set/', headers=headers, files=files, cookies=fetch_result.cookies)

if upload_result.status_code == 201:
print('Document uploaded to the server successfully')
else:
print('Error: ' + upload_result.text)
else:
print('Error when getting CSRF token: {} {}'.format(fetch_result.status_code, fetch_result.text))


credentials = Credentials(username=EXCHANGE_USER, password=EXCHANGE_PASSWORD)
config = Configuration(server=EXCHANGE_SERVER, credentials=credentials)
account = Account(primary_smtp_address='mailbox@domain.com', config=config, autodiscover=False, access_type=DELEGATE)

while True:
last_minute = UTC_NOW() - timedelta(minutes=1)
mails = account.inbox.filter(datetime_received__gte=last_minute) # Filter by a date range

for mail in mails:
for attachment in mail.attachments:
upload_attachment(attachment)

print("sleeping...")
time.sleep(60)

Notes:



  • The Python library exchangelib is used, to simplify the connection from Python to the Microsoft Exchange Server using the Exchange Web Services (EWS) protocol.

  • In line 37, an infinite loop reads every new e-mail received in the last 60 seconds.

  • In case an e-mail contains an attachment in PDF format, the function upload_attachment is called, to upload the document to the oData service.

  • During the upload_attachment execution, to authenticate against the S/4HANA system, a HEAD request to the oData service is triggered, including x-csrf-token = Fetch and the encoded user and password of the backend system in the format Authorization = Basic <encoded user and password combination> as part of the header (lines 10 – 18).

  • A POST request to the oData service is triggered, including the x-csrf-token received, and a new header attribute called slug in the format companycode=XXXX;filename=YYYY.pdf. This defines the company code and the file name to be used in the S/4HANA system. As a result, the http code 201 indicates that the file was uploaded successfully.



Connect an inbox to the application via the connector



  1. Set up a shared inbox for e-mails that include payment advice documents as PDF attachments.

  2. Create a generic user for the app Manage Payment Advices, to be the default owner of all e-mails sent to the shared inbox.

  3. Follow the steps in the customizing activity Enable E-Mail Integration. Note: The user must be the same used to call the oData service.

  4. Run your connector.

  5. Send an e-mail with an attached payment advice in PDF format to the shared inbox.

  6. In the app, select Load from Email to have the latest payment advices from your inbox directly displayed under the Confirmation Pending


Result:

The extracted information from the payment advice is displayed in the application.

This possibility aims to further automate your process of cash application as part of the order-to-cash scenario in accounts receivable accounting, by reducing one more manual, tedious, and error-prone step.


 

Note:

This is not an out-of-the-box solution delivered and supported by SAP. However, it is an example of how to easily enhance your automation journey with SAP Cash Application.
3 Comments