> ## Documentation Index
> Fetch the complete documentation index at: https://docs.langchain.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Salesforce integration

> Integrate with the Salesforce tool using LangChain Python.

A tool for interacting with Salesforce CRM using LangChain.

## Overview

The `langchain-salesforce` package integrates LangChain with Salesforce CRM,
allowing you to query data, manage records, and explore object schemas
from LangChain applications.

### Key features

* **SOQL Queries**: Execute Salesforce Object Query Language (SOQL) queries
* **Object Management**: Create, read, update, and delete (CRUD) operations on Salesforce objects
* **Schema Exploration**: Describe object schemas and list available objects
* **Async Support**: Asynchronous operation support
* **Error Handling**: Detailed error messages
* **Environment Variable Support**: Load credentials from environment variables

## Setup

Install the required dependencies:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
 pip install langchain-salesforce
```

## Authentication setup

These environment variables will be automatically picked up by the integration.

## Getting your security token

If you need a security token:

1. Log into Salesforce
2. Go to Settings
3. Click on "Reset My Security Token" under "My Personal Information"
4. Check your email for the new token

### Environment variables (recommended)

Set up your Salesforce credentials as environment variables:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
export SALESFORCE_USERNAME="your-username@company.com"
export SALESFORCE_PASSWORD="your-password"
export SALESFORCE_SECURITY_TOKEN="your-security-token"
export SALESFORCE_DOMAIN="login"  # Use "test" for sandbox environments
```

## Instantiation

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os

from langchain_salesforce import SalesforceTool

username = os.getenv("SALESFORCE_USERNAME", "your-username")
password = os.getenv("SALESFORCE_PASSWORD", "your-password")
security_token = os.getenv("SALESFORCE_SECURITY_TOKEN", "your-security-token")
domain = os.getenv("SALESFORCE_DOMAIN", "login")

tool = SalesforceTool(
    username=username, password=password, security_token=security_token, domain=domain
)
```

## Invocation

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
def execute_salesforce_operation(
    operation, object_name=None, query=None, record_data=None, record_id=None
):
    """Executes a given Salesforce operation."""
    request = {"operation": operation}
    if object_name:
        request["object_name"] = object_name
    if query:
        request["query"] = query
    if record_data:
        request["record_data"] = record_data
    if record_id:
        request["record_id"] = record_id
    result = tool.invoke(request)
    return result
```

## Query

This example queries Salesforce for 5 contacts.

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
query_result = execute_salesforce_operation(
    operation="query", query="SELECT Id, Name, Email FROM Contact LIMIT 5"
)
```

## Describe an object

Fetches metadata for a specific Salesforce object.

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
describe_result = execute_salesforce_operation(
    operation="describe", object_name="Account"
)
```

## List available objects

Retrieves all objects available in the Salesforce instance.

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
list_objects_result = execute_salesforce_operation(operation="list_objects")
```

## Create a new contact

Creates a new contact record in Salesforce.

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
create_result = execute_salesforce_operation(
    operation="create",
    object_name="Contact",
    record_data={"LastName": "Doe", "Email": "doe@example.com"},
)
```

## Update a contact

Updates an existing contact record.

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
update_result = execute_salesforce_operation(
    operation="update",
    object_name="Contact",
    record_id="003XXXXXXXXXXXXXXX",
    record_data={"Email": "updated@example.com"},
)
```

## Delete a contact

Deletes a contact record from Salesforce.

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
delete_result = execute_salesforce_operation(
    operation="delete", object_name="Contact", record_id="003XXXXXXXXXXXXXXX"
)
```

## Chaining

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic
from langchain.messages import HumanMessage
from langchain_salesforce import SalesforceTool

# Initialize the Salesforce tool
tool = SalesforceTool(
    username=username, password=password, security_token=security_token, domain=domain
)

# Initialize Anthropic LLM
llm = ChatAnthropic(model="claude-sonnet-4-6")

# First, let's query some contacts to get real data
contacts_query = {
    "operation": "query",
    "query": "SELECT Id, Name, Email, Phone FROM Contact LIMIT 3",
}

contacts_result = tool.invoke(contacts_query)

# Now let's use the LLM to analyze and summarize the contact data
if contacts_result and "records" in contacts_result:
    contact_data = contacts_result["records"]

    # Create a message asking the LLM to analyze the contact data
    analysis_prompt = f"""
    Please analyze the following Salesforce contact data and provide insights:

    Contact Data: {contact_data}

    Please provide:
    1. A summary of the contacts
    2. Any patterns you notice
    3. Suggestions for data quality improvements
    """

    message = HumanMessage(content=analysis_prompt)
    analysis_result = llm.invoke([message])

    print("\nLLM Analysis:")
    print(analysis_result.content)
```

***

## API reference

For comprehensive documentation and API reference, see:

* [langchain-salesforce README](https://github.com/colesmcintosh/langchain-salesforce/blob/main/README.md)
* [Simple Salesforce Documentation](https://simple-salesforce.readthedocs.io/en/latest/)

## Additional resources

* [Salesforce SOQL Reference](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/)
* [Salesforce REST API Developer Guide](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/tools/salesforce.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
