
In today's fast-paced business environment, automation is key to increasing productivity and efficiency. Integrating different tools and platforms can streamline workflows and reduce you SAP BPA bot execution significantly. One such integration is between Microsoft Outlook and SAP BPA , where emails trigger actions in SAP's Build Process Automation. If you bot starts with an email arrival then with this solution you can save useless bot executions and that lead to saving money
Enabling Developer Mode
Before we dive into the code, let's first enable Developer Mode in Outlook:
Open Outlook.
Go to File > Options.
In the Outlook Options dialog box, click on Customize Ribbon.
Check the box next to "Developer" in the right column.
Click OK to save the changes.
Enabling Macro Settings
To ensure that macros are enabled, follow these steps:
Go to File > Options > Trust Center.
Click on Trust Center Settings.
In the Trust Center dialog box, select Macro Settings.
Choose "Enable all macros" or "Enable all macros (not recommended; potentially dangerous code can run)" based on your organization's security policies.
Click OK to save the changes.
Opening the Visual Basic Editor
Press Alt + F11 on your keyboard. This shortcut opens the Visual Basic for Applications (VBA) editor within Outlook.
Creating a Module
In the VBA editor, go to Insert > Module. This creates a new module where you can write your VBA code.
Adding References
To use MSXML2.XMLHTTP, we need to add a reference to "Microsoft XML, v6.0" in the VBA editor:
In the VBA editor, go to Tools > References.
Check "Microsoft XML, v6.0" from the list.
Click OK to add the reference.
Now, let's write the VBA code that will automate the SAP Build Process from Outlook.
Sub CheckEmailSubject(Item As Outlook.MailItem)
' This subroutine triggers when a new email arrives.
' If the subject contains "Process Invoice" it calls the CallAPI subroutine.
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:
Close the VBA editor.
Send an email with the subject containing "Fixed Assets" to your Outlook inbox.
Press Ctrl + G to view the Immediate window in the VBA editor.
The code will trigger, and you can monitor the SAP Build Process Automation from Outlook.
Running the Script from Rules
To run the script automatically when specific emails arrive, you can set up a rule in Outlook:
In Outlook, go to File > Manage Rules & Alerts.
Click on New Rule.
Select "Apply rule on messages I receive" and click Next.
Choose conditions as per your requirement (e.g., Subject contains "Fixed Assets") and click Next.
Check "Run a script" as the action to perform.
In the Step 2 box, click on "a script" and select the VBA script you wrote.
Click Next, specify any exceptions if needed, and click Finish.
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:
Close Outlook.
Open the Registry Editor by pressing Windows key + R, typing "regedit", and pressing Enter.
Navigate to the following path: HKEY_CURRENT_USER\Software\Policies\Microsoft\Office<version>\Outlook\Security
Replace <version> with your Outlook version (e.g., 16.0 for Outlook 2016).
If a DWORD value named "EnableUnsafeClientMailRules" exists, set its value to 1. If not, create a new DWORD value and set it to 1.
Close the Registry Editor and reopen Outlook.
Now, you should be able to see the "Run a script" option when creating rules.
Configuring Script Execution via Rules for Every New Email Arrival
Setting up a rule in Outlook to execute the script automatically for every new email arrival involves a few straightforward steps. Let's walk through the process:
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.
With these simple steps, you can trigger your SAP BPA bot directly from Outlook whenever the relevant email arrives to bot mailbox. Happy automating! Happy reading! Hope it will Help
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
9 | |
7 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |