top of page

How to Integrate Salesforce and SAP Using Apex, Python and Heroku

Updated: Apr 3

Integrating systems can be challenging, especially when dealing with robust platforms like Salesforce and SAP Business One. This article will show you how you can connect these two tools, creating an automated, secure, and scalable workflow using technologies like Apex, Python and Heroku.


Integration Overview


The goal is to allow the SAP data to be consulted in real time from Salesforce, in a secure and stable manner, respecting SAP security restrictions.


To achieve this, dividing the integration into three main layers allows for better organization and maintainability:


  • Salesforce: responsible for triggering requests.

  • Heroku: responsible for consuming SAP and returning data to Salesforce.

  • SAP: data source system, accessed via REST endpoints.


How Integration Works


1. Automation in Salesforce


Every request to SAP begins within Salesforce. To do this, use a combination of native features of the Apex language and the Lightning platform:


Schedulable: interface that allows the creation of scheduled jobs, which define the time and frequency of an automation. In this case, we will use it to schedule our queries to SAP.


Queueable: interface that allows asynchronous processing in a queue, great for chaining and increasing execution limits, ensuring greater performance.


Named Credentials: used to securely store the authentication data needed to communicate with the middleware, and consequently SAP.


HTTP Class: Apex class that allows HTTP calls to be made using the REST architectural standard. In this case, using the JSON format to communicate with SAP.



private HttpResponse sendRequest() {
	HttpRequest req = new HttpRequest();
 	req.setEndpoint('callout:your_named_credential_here/params_here');
	req.setMethod('GET');
	req.setHeader('Content-Type', 'application/json');
	req.setTimeout(10000);
	Http http = new Http();
	HttpResponse res;

	try {
		res = http.send(req);
		if (res.getStatusCode() == 200) {
			return res;
		} else {
			throw new CalloutException('Error message here');
		}
	} catch (CalloutException e) {
		throw e;
	}
}	

Your Apex HTTP request might look something like this.



 


2. Middleware Hosted on Heroku


Python is a great option if you want to develop quickly and efficiently. And when it comes to building APIs securely and easily, FastAPI is the key - it’s the framework behind this article.


The implementation of this API aims to:


  • Receive requests from Salesforce;

  • Handle session expiration scenarios;

  • Connect to SAP using a fixed IP;

  • Make requests to SAP REST endpoints;

  • Return SAP data to Salesforce in a standardized format;



@router.get("/{id}/", response_model=schemas.ItemsResponseModel)
async def get_product_by_id(
	id: str = Path(..., min_length=1),
	cookie: str = Header(None),
	select: str = Query(None, alias="select")
):
	utils.validate_cookies(cookie)
	
	query_params = {}
	if select:
		query_params["$select"] = select

	base_url = f"{settings.sap_endpoint}/Items('{id}')"
	full_url = utils.build_url(base_url, query_params)

	response = None
	try:
		response = requests.get(
			full_url,
			headers=utils.build_headers(cookie),
			proxies=proxies,
			timeout=30,
			verify=settings.verify_ssl
		)
		
		if response.status_code == 200:
			data = response.json()
			return {"objects": [data]}
		else:
			utils.handle_status_code_error(response)
	
	except Exception as e:
		utils.handle_request_exception(e, response)

Sample FastAPI endpoint.



What is the best way to host and maintain logic that requests to SAP? The answer is Heroku.


Heroku is a platform as a service (PaaS) that allows you to host applications quickly and easily, without the need to manage infrastructure.


It offers native integration with Salesforce, automatic scalability, ease of deploying applications in different languages, and access to essential add-ons.


How to Integrate Salesforce and SAP Using Apex, Python and Heroku
Heroku management panel, where the FastAPI application is hosted. You can view the deployment history, dyno configuration and add-ons used in the integration.

 


3. Connect to SAP


SAP provides several integration services, one of which is the Service Layer — a RESTful API based on the OData protocol that allows you to interact with SAP data in a standardized and secure way.

How to Integrate Salesforce and SAP B1 Using Apex, Python and Heroku
Example of SAP Service Layer documentation, showing the operations available for data manipulation, such as queries (GET), creation (POST) and update (PATCH) of records.

Additionally, it requires that all requests come from a whitelisted IP address. To meet this requirement, we use QuotaGuard Shield — a proxy service that offers static IPs and perfectly integrates with Heroku.


QuotaGuard also provides a comprehensive dashboard for traffic monitoring. Through this dashboard, we can track, in real time, the number of requests made and the amount of data transferred, including details such as date, time, and request origin.



How to Integrate Salesforce and SAP Using Apex, Python and Heroku - QuotaGuard
QuotaGuard Shield main panel, displaying the number of requests made and the volume of data transferred.

How to Integrate Salesforce and SAP Using Apex, Python and Heroku - QuotaGuard 2
QuotaGuard Shield log view screen, showing detailed history of requests made by the proxy.

Conclusion


This solution enables Salesforce to connect to SAP in an automated, secure, and efficient manner, using a combination of Salesforce resources, the Heroku cloud, and the QuotaGuard Shield service to ensure fixed IP communication. This integration allows Salesforce to query and manipulate SAP data in real time, respecting all security and infrastructure requirements of both systems.


BRF Consulting can help your business continue to grow and be more efficient. Feel free to reach us at contact@brfconsulting.com


About the author: Gabriel Pereira Azevedo is a Salesforce Engineer at BRF Consulting, a Salesforce ISV partner specialized on Application Development and Data Engineering.



Comentários


bottom of page