Skip to main content
Open In ColabOpen on GitHub

Salesforce

A comprehensive tool for interacting with Salesforce CRM using LangChain.

Overview

The langchain-salesforce package provides a powerful integration between LangChain and Salesforce CRM, enabling you to perform various operations including querying data, managing records, and exploring object schemas directly from your 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: Full asynchronous operation support
  • Error Handling: Comprehensive error handling with meaningful error messages
  • Environment Variable Support: Automatic credential loading from environment variables

Setup

Install the required dependencies:

pip install langchain-salesforce

Authentication Setup

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_core.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-20250514")

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

API Reference

For comprehensive documentation and API reference, see:

Additional Resources


Was this page helpful?