This is a Python script that uses the Salesforce API to match leads to accounts based on fuzzy matching of the company name and email domain.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
- Create a Salesforce Apex REST service for converting leads:
- Log in to Salesforce.
- Click on the gear icon at the top right, select Setup.
- Use Quick Search at the top left to find and navigate to Apex Classes.
- Click 'New' to create a new Apex class.
- In the new class dialog, enter the class name LeadConvertService and the below code.
@RestResource(urlMapping='/LeadConvert/*')
global with sharing class LeadConversionService {
@HttpPost
global static String convertLead(String leadId, String convertedStatus, String accountId, Boolean doNotCreateOpportunity) {
Database.LeadConvert leadConvert = new Database.LeadConvert();
leadConvert.setLeadId(leadId);
leadConvert.setAccountId(accountId);
leadConvert.setConvertedStatus(convertedStatus);
leadConvert.setDoNotCreateOpportunity(doNotCreateOpportunity);
Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert);
if (leadConvertResult.isSuccess()) {
return 'Lead converted successfully: ' + leadConvertResult.getContactId();
} else {
return 'Error converting lead: ' + leadConvertResult.getErrors();
}
}
}
- Click 'Save'.
- Clone this repository to your local machine.
git clone https://github.com/kreitter/salesforce-lead-to-account-matching.git
- Ensure that you have the required Python libraries installed. You can install them using pip:
pip install simple-salesforce fuzzywuzzy pandas python-dotenv
- Set up the environment variables. Rename the .env.example to .env and fill in your Salesforce credentials:
SALESFORCE_USERNAME=your_username
SALESFORCE_PASSWORD=your_password
SALESFORCE_SECURITY_TOKEN=your_security_token
Run the script using a Python interpreter:
python lead_to_account_matching.py
To run the script:
python sf_l2a_matching.py
This script queries all Leads and Accounts from your Salesforce instance, standardizes the data, and performs lead-to-account matching using fuzzy string matching. If a suitable match is found for a Lead, the Lead is converted and linked to the matched Account. The script will print out the results of the lead-to-account matching, including any leads that are converted and linked to accounts.
Contributions are welcome! Please feel free to submit a pull request.
This project is licensed under the terms of the MIT license.
David Kreitter | [email protected] | www.kreitter.com | LinkedIn