# Salesforce CRM Integration

### Introduction

Salesforce CRM integration via **Toollake** allows businesses to automate lead generation, customer management, and account tracking seamlessly. This documentation provides step-by-step instructions on how to create leads, accounts, and customers, as well as retrieve information using the **Salesforce** module.

### Prerequisites

* A valid **Salesforce** account.
* Salesforce **username**, **password**, and **security token**.
* Installed **Groclake Toollake CRM Salesforce** library.

### Installation

Ensure you have **Groclake** installed in your environment:

```sh
pip install groclake
```

### Initializing Salesforce Connection

To interact with Salesforce, instantiate the `Salesforce` class with the required credentials.

```python
from groclake.toollake.crm.salesforce import Salesforce

sf = Salesforce(
    username="your_username@example.com",
    password="your_password",
    security_token="your_security_token"
)
```

### Creating a Lead

A **Lead** represents a potential customer. Use the `create_lead` method to add a new lead.

```python
lead_data = {
    "FirstName": "John",
    "LastName": "Doe",
    "Email": "john.doe@example.com",
    "Company": "Tech Corp",
    "Phone": "1234567890"
}
lead_id = sf.create_lead(lead_data)
print(f"Successfully created lead with ID: {lead_id}")
```

### Creating an Account

An **Account** represents a business entity.

```python
account_id = sf.create_account(
    company_name="New Tech Solutions",
    industry="Technology"
)
print(f"Successfully created account with ID: {account_id}")
```

### Creating a Customer (Contact)

A **Customer** is represented as a **Contact** in Salesforce.

```python
customer_data = {
    "FirstName": "Jane",
    "LastName": "Smith",
    "Email": "jane.smith@example.com",
    "Phone": "9876543210",
    "Company": "Innovation Labs",
    "Industry": "Research"
}
customer_id = sf.create_customer(customer_data)
print(f"Successfully created customer with ID: {customer_id}")
```

### Fetching a Lead

Retrieve a lead using an email identifier.

```python
lead = sf.fetch_lead("john.doe@example.com")
print(f"Found lead: {lead['FirstName']} {lead['LastName']}")
```

### Fetching an Account

Retrieve an account by company name.

```python
account = sf.fetch_account("Tech Corp")
print(f"Found account: {account['Name']}")
```

### Fetching a Customer

Retrieve a customer using an email identifier.

```python
customer = sf.fetch_customer("jane.smith@example.com")
print(f"Found customer: {customer['FirstName']} {customer['LastName']}")
```

### Fetching All Leads for a Company

Retrieve all leads associated with a company.

```python
company_leads = sf.fetch_account_leads("Tech Corp")
print(f"Found {len(company_leads)} leads for Tech Corp")
```

### Fetching All Customers for an Account

Retrieve all customers linked to an account.

```python
account_customers = sf.fetch_account_customers(account['Id'])
print(f"Found {len(account_customers)} customers for {account['Name']}")
```

### Validation Cases & Error Handling

#### Invalid Email Format

```python
invalid_lead = {
    "FirstName": "John",
    "LastName": "Doe",
    "Email": "invalid.email",  # Invalid email format
    "Company": "Tech Corp",
    "Phone": "1234567890"
}
sf.create_lead(invalid_lead)
```

#### Invalid Phone Number

```python
invalid_phone = {
    "FirstName": "John",
    "LastName": "Doe",
    "Email": "john.doe@example.com",
    "Company": "Tech Corp",
    "Phone": "123"  # Too short
}
sf.create_lead(invalid_phone)
```

#### Missing Required Fields

```python
incomplete_data = {
    "FirstName": "John",
    "Email": "john.doe@example.com",  # Missing LastName
    "Company": "Tech Corp",
    "Phone": "1234567890"
}
sf.create_lead(incomplete_data)
```

### Conclusion

The **Salesforce CRM Integration using Toollake** simplifies lead, customer, and account management with a structured API. By following the examples above, you can easily integrate Salesforce into your workflows, automate CRM tasks, and optimize business processes.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.groclake.ai/lakes/tool-management-and-gateway/toollake/salesforce-crm-integration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
