Skip to main content
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:
 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
Set up your Salesforce credentials as environment variables:
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

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

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.
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.
describe_result = execute_salesforce_operation(
    operation="describe", object_name="Account"
)

List Available Objects

Retrieves all objects available in the Salesforce instance.
list_objects_result = execute_salesforce_operation(operation="list_objects")

Create a New Contact

Creates a new contact record in Salesforce.
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.
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.
delete_result = execute_salesforce_operation(
    operation="delete", object_name="Contact", record_id="003XXXXXXXXXXXXXXX"
)

Chaining

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-5-20250929")

# 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:

Additional Resources


Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.