Streamlining SAP BPA Bot Execution with Outlook Integration: A Guide
Indrajit S.
SAP Build LCNC Certified | Proven Record of Developing Automated Solutions using SAP BPA/Build Process Automation (formally SAP iRPA) v2 & v3 | JS | VBA | VB.NET | GenAI
In today's dynamic business landscape, maximizing efficiency through automation is paramount. Integrating various tools and platforms not only streamlines workflows but also optimizes resource utilization. One powerful integration lies between Microsoft Outlook and SAP Build Process Automation (BPA), where incoming emails can trigger actions within SAP's BPA framework. Leveraging this integration effectively can significantly reduce unnecessary bot executions, thereby leading to cost savings.
Enabling Developer Mode
Before delving into the code, it's essential to enable Developer Mode in Outlook:
Enabling Macro Settings
To ensure smooth functioning of macros, follow these steps:
Opening the Visual Basic Editor
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor within Outlook.
Creating a Module
In the VBA editor, navigate to Insert > Module to create a new module for writing VBA code.
Adding References
To utilize MSXML2.XMLHTTP, add a reference to "Microsoft XML, v6.0" in the VBA editor:
Now, let's proceed with writing the VBA code to automate SAP Build Process from Outlook.
Sub CheckEmailSubject(Item As Outlook.MailItem)
If InStr(1, Item.Subject, "Process Invoice", vbTextCompare) > 0 Then
Call CallAPI
End If
End Sub
This subroutine, CheckEmailSubject, triggers when a new email arrives. If the subject contains "Process Invoice," it calls the CallAPI subroutine.
Sub CallAPI()
' Variable declarations
Dim xmlhttp As Object
Dim url As String
Dim data As String
Dim accessToken As String
Dim response As String
' Create a new XMLHTTP object
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
' First request to get access token
url = "https://[URL]/oauth/token?grant_type=client_credentials"
xmlhttp.Open "POST", url, False
xmlhttp.setRequestHeader "Authorization", "Basic clientid:clientsecret"
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlhttp.Send
' Check if the request was successful
If xmlhttp.Status = 200 Then
' Extract the access token from the response
Debug.Print xmlhttp.responseText
accessToken = ExtractAccessToken(xmlhttp.responseText)
' Print the access token value to the Immediate window
Debug.Print "Access Token: " & accessToken
Else
Debug.Print "Error in getting access token"
Exit Sub
End If
' Prepare PayLoad data for the second request/ API trigger
data = "{""invocationContext"":""${invocation_context}"",""input"":{""Name"":""ABC""}}"
' Second request to the API endpoint
Debug.Print "Bearer " & accessToken
url = "[API trigger URL]"
xmlhttp.Open "POST", url, False
xmlhttp.setRequestHeader "irpa-api-key", "[API KEY]"
xmlhttp.setRequestHeader "Authorization", "Bearer " & accessToken
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlhttp.Send (data)
' Check if the second request was successful
If xmlhttp.Status = 200 Or xmlhttp.Status = 201 Then
' Get the response from the second request
response = xmlhttp.responseText
' Print the API response to the Immediate window
Debug.Print "API Response:"
Debug.Print xmlhttp.responseText
Else
Debug.Print "Error in sending request"
Debug.Print xmlhttp.Status
Debug.Print xmlhttp.responseText
End If
' Clean up
Set xmlhttp = Nothing
End Sub
Function ExtractAccessToken(response As String) As String
Dim tokenStart As Long
Dim tokenEnd As Long
Dim token As String
' Find the position of "access_token" in the response
tokenStart = InStr(response, """access_token"":""") + Len("""access_token"":""")
' Find the position of the next quotation mark after "access_token"
tokenEnd = InStr(tokenStart, response, """")
' Extract the access token value
token = Mid(response, tokenStart, tokenEnd - tokenStart)
' Trim leading and trailing whitespace from the token
token = Trim(token)
' Return the access token
ExtractAccessToken = token
End Function
These code blocks work together to make API calls and extract the access token from the response, demonstrating a basic implementation of interacting with APIs using VBA in Outlook.
Testing the Code
Now that we have written the code, let's test it:
Running the Script from Rules
To run the script automatically when specific emails arrive, you can set up a rule in Outlook:
If "Run a Script" Option is Not Found
If you don't see the "Run a script" option when creating a rule, it's likely because Outlook's security settings restrict script execution:
Configuring Script Execution via Rules for Every New Email Arrival
Follow these steps:
Open Outlook:
Access Rules and Alerts:
Create a New Rule:
Select Condition for the Rule:
Define Conditions (Optional):
Specify Action for the Rule:
Set Exceptions (Optional):
Name and Finalize the Rule:
Now, every new email that arrives in your Outlook inbox will trigger the execution of the specified VBA script, triggering the SAP BPA bot according to the conditions you've set. This automation helps streamline your task and ensures that important tasks are promptly addressed without manual intervention and saving bot execution and saving money.
By seamlessly integrating Outlook with SAP BPA, you can efficiently manage tasks and enhance productivity. Happy automating!