Skip to content

heroku-examples/df25-heroku-minihack-code-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TDX25 - Heroku at Camp Mini Hacks - Code (Python Version)

Important

The actions in this repository are used in a Salesforce Dreamforce 25 Heroku Mini Hack. They're here to help people who did the minihack challenge, and have access to the minihack org and its objects, apps, and agents. So, these examples won't work outside the minihack. Also, the steps below are just for reference and won't work unless you use them in an org that has the correct objects already deployed. If you want to explore Heroku in the context of Agentforce please check out this tutorial and also further samples here.

Requirements

  • Python 3.8+ and pip
  • Heroku login
  • Heroku CLI installed
  • Heroku AppLink CLI plugin is installed
  • Salesforce CLI installed
  • Login information for one or more Scratch, Development or Sandbox orgs containing the Dreamforce 2025 Heroku Mini Hack apps.

Local Development and Testing

Code invoked from Salesforce requires specific HTTP headers to connect back to the invoking Salesforce org. The Heroku AppLink Python SDK and CLI can be used to simulate requests from Salesforce with the correct headers, enabling you to develop and test locally before deploying to test from Apex, Flow, or Agentforce.

First, set up your Python environment:

python -m venv venv
source venv/bin/activate
pip install -r requirements.txt

Run the following commands to locally authenticate and run the sample:

sf org login web --alias my-org
uvicorn app.main:app --host 127.0.0.1 --port 8080

In a new terminal window, you can use heroku applink:functions:exec to test your functions. Substitute the Id values for valid Contact and Vehicle Id records from your Salesforce org.

heroku applink:functions:exec -a YOUR_HEROKU_APP_NAME -u my-org -p '{"customerId": "0035g00000XyZbHAZ","vehicleId": "a04Hs00002EMj9PIAT","maxInterestRate": 0,"downPayment": 1000,"years": 3}' -- "/api/calculateFinanceAgreement"

You should see output similar to this:

{"recommendedFinanceOffer":{"finalCarPrice":41800.0,"adjustedInterestRate":3.5,"monthlyPayment":...}}

Run the following command substituting the Id values for a valid Flight record from your Salesforce org.

heroku applink:functions:exec -a YOUR_HEROKU_APP_NAME -u my-org -p '{"flightId": "a02Hs00001D2QtLIAV"}' -- "/api/calculateCarbonFootprint"

You should see output similar to this:

{"flight":{"flightNumber":"...","departureAirport":"SFO","arrivalAirport":"LAX",...}}

Deploying and Testing from Apex and Flow

To test from Apex, Flow and other tools within your Salesforce org you must deploy the code and import it into your org.

First, create a .python-version file to specify the Python version for Heroku:

3.11

The following commands create a Heroku application and configure the Heroku Integration add-on.

heroku create
git push heroku main

Next install and configure the Heroku Integration add-on:

heroku addons:create heroku-integration
heroku buildpacks:add heroku/python
heroku buildpacks:add https://github.com/heroku/heroku-buildpack-heroku-integration-service-mesh
heroku salesforce:connect my-org --store-as-run-as-user
# You will need to regenerate an api-docs.yaml for the python app if you want to re-import
# heroku salesforce:import api-docs.yaml --org-name my-org --client-name ActionsService 

Once imported grant permissions to users to invoke your code using the following sf command:

sf org assign permset --name ActionsService -o my-org

Deploy the Heroku application and confirm it has started.

git push heroku main
heroku logs --tail

Navigate to your orgs Setup menu and search for Heroku then click Apps to confirm your application has been imported.

Invoking from Apex

Now that you have imported your Heroku application. The following shows an Apex code fragment the demonstrates how to invoke your code in an synchronous manner (waits for response).

ExternalService.ActionsService service = new ExternalService.ActionsService();
ExternalService.ActionsService.calculateFinanceAgreement_Request request = new ExternalService.ActionsService.calculateFinanceAgreement_Request();
ExternalService.ActionsService_FinanceCalculationRequest body = new ExternalService.ActionsService_FinanceCalculationRequest();
request.body = body;
request.body.vehicleId = 'a04Hs00002EMj9PIAT';
System.debug('Final Car Price: ' + service.calculateFinanceAgreement(request).Code200.recommendedFinanceOffer.finalCarPrice);

Inspect the debug log output sent to to the console and you should see the generated Quote ID output.

About

Python code samples used for the Dreamforce 2025 Heroku Mini Hack

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published