Examples & Use Cases
Convert Plugin
Basic Example Code
global class DefaultConvert implements dupcheck.dc3Plugin.InterfaceConvert {
global void beforeConvert(Database.LeadConvert leadConvertData) {
// YOUR CUSTOM CODE
System.debug('beforeConvert called with LeadConvert data: ' + leadConvertData);
System.debug('Lead ID: ' + leadConvertData.getLeadId());
System.debug('Converted Status: ' + leadConvertData.getConvertedStatus());
return;
}
global void convertFailed(Database.LeadConvert leadConvertData, dupcheck.dc3Exception.ConvertException exceptionData) {
// YOUR CUSTOM CODE
System.debug('convertFailed called for LeadConvert data: ' + leadConvertData);
System.debug('Exception: ' + exceptionData.getMessage());
return;
}
global void afterConvert(Database.LeadConvertResult leadConvertResult, Task taskData) {
// YOUR CUSTOM CODE
System.debug('afterConvert called with LeadConvertResult: ' + leadConvertResult);
System.debug('Conversion Success: ' + leadConvertResult.isSuccess());
System.debug('Account ID: ' + leadConvertResult.getAccountId());
System.debug('Contact ID: ' + leadConvertResult.getContactId());
System.debug('Opportunity ID: ' + leadConvertResult.getOpportunityId());
if (taskData != null) {
System.debug('Task created during conversion: ' + taskData);
}
return;
}
}
Example 1: Send a notification to current Contact owner
This code example sends a notification to the current Contact owner, indicating that a Lead was converted into a Contact they own.
Required steps in Salesforce setup
- Create a Custom Notification with the API Name
ConvertNotification
at Setup -> Notification Builder -> Custom Notifications. - Enable Users to receive notifications.
global class NotifyConvertPlugin implements dupcheck.dc3Plugin.InterfaceConvert {
// Static variables to hold Lead details for use after conversion
private static Id convertedLeadId;
private static String convertedLeadName;
private static String convertedLeadSource;
global void beforeConvert(Database.LeadConvert leadConvertData) {
System.debug('--- beforeConvert START ---');
// Store Lead Id for later use
convertedLeadId = leadConvertData.getLeadId();
// Retrieve basic Lead details (Name and Lead Source)
Lead leadRec = [SELECT Id, Name, LeadSource FROM Lead WHERE Id = :convertedLeadId LIMIT 1];
convertedLeadName = leadRec.Name;
convertedLeadSource = leadRec.LeadSource;
System.debug('Stored Lead ID: ' + convertedLeadId);
System.debug('Stored Lead Name: ' + convertedLeadName);
System.debug('Stored Lead Source: ' + convertedLeadSource);
System.debug('--- beforeConvert END ---');
}
global void convertFailed(Database.LeadConvert leadConvertData, dupcheck.dc3Exception.ConvertException exceptionData) {
System.debug('--- convertFailed ---');
System.debug('LeadConvert Object: ' + leadConvertData);
System.debug('Exception Message: ' + exceptionData.getMessage());
}
global void afterConvert(Database.LeadConvertResult leadConvertResult, Task taskData) {
System.debug('--- afterConvert START ---');
if (leadConvertResult.isSuccess()) {
Id contactId = leadConvertResult.getContactId();
if (contactId != null) {
// Retrieve the converted Contact record and its owner
Contact con = [SELECT Id, Name, OwnerId FROM Contact WHERE Id = :contactId LIMIT 1];
User contactOwner = [SELECT Id, Name FROM User WHERE Id = :con.OwnerId LIMIT 1];
// Build the notification message details
String notificationTitle = 'A new Lead was converted into a Contact you own: ' + convertedLeadName;
String messageBody = 'Lead "' + convertedLeadName + '" (ID: ' + convertedLeadId +
', Source: ' + convertedLeadSource +
') was successfully converted. View Contact: ' + con.Name;
// Query the custom notification type used for notifications
CustomNotificationType cnt = [SELECT Id FROM CustomNotificationType WHERE DeveloperName = 'ConvertNotification' LIMIT 1];
// Create and send the custom notification
Messaging.CustomNotification notification = new Messaging.CustomNotification();
notification.setNotificationTypeId(cnt.Id);
notification.setTitle(notificationTitle);
notification.setBody(messageBody);
notification.setTargetId(con.Id);
notification.setSenderId(UserInfo.getUserId());
// Send notification to the Contact Owner
notification.send(new Set<String>{ String.valueOf(contactOwner.Id) });
System.debug('Notification sent to Contact Owner: ' + contactOwner.Name);
} else {
System.debug('No Contact was created in the conversion result.');
}
} else {
System.debug('Lead conversion failed.');
}
System.debug('--- afterConvert END ---');
}
}
Example 2: Create a log on the Contact record of all Leads converted into that Contact
This plugin tracks every lead that is converted into a specific contact. It captures basic lead details before conversion (Name, LeadSource and Date&Time of conversion) and, after a successful conversion, appends a formatted log entry to the contact’s Description field, preserving any existing log data.
global class LeadConversionLogger implements dupcheck.dc3Plugin.InterfaceConvert {
// Store lead details keyed by Lead Id for use after conversion
private static Map<Id, LeadInfo> leadInfoMap = new Map<Id, LeadInfo>();
// Store the current Lead Id being processed
private static Id currentLeadId;
// Inner class to hold basic Lead information
public class LeadInfo {
public String firstName;
public String lastName;
public String leadSource;
}
global void beforeConvert(Database.LeadConvert leadConvertData) {
System.debug('--- beforeConvert START ---');
// Capture the Lead Id from the conversion data and store it statically
currentLeadId = leadConvertData.getLeadId();
// Query the Lead to get basic details
Lead leadRec = [SELECT Id, FirstName, LastName, LeadSource FROM Lead WHERE Id = :currentLeadId LIMIT 1];
// Store the details in our LeadInfo map
LeadInfo info = new LeadInfo();
info.firstName = leadRec.FirstName;
info.lastName = leadRec.LastName;
info.leadSource = leadRec.LeadSource;
leadInfoMap.put(currentLeadId, info);
System.debug('Stored Lead Info for Id: ' + currentLeadId);
System.debug('--- beforeConvert END ---');
}
global void convertFailed(Database.LeadConvert leadConvertData, dupcheck.dc3Exception.ConvertException exceptionData) {
System.debug('--- convertFailed ---');
System.debug('Conversion failed for Lead Id: ' + leadConvertData.getLeadId());
System.debug('Exception: ' + exceptionData.getMessage());
}
global void afterConvert(Database.LeadConvertResult leadConvertResult, Task taskData) {
System.debug('--- afterConvert START ---');
if (leadConvertResult.isSuccess()) {
Id contactId = leadConvertResult.getContactId();
if (contactId != null && currentLeadId != null && leadInfoMap.containsKey(currentLeadId)) {
// Retrieve the Contact to update the Description field
Contact con = [SELECT Id, Description FROM Contact WHERE Id = :contactId LIMIT 1];
// Get stored Lead information
LeadInfo info = leadInfoMap.get(currentLeadId);
String currentDateTime = DateTime.now().format();
// Build the log string in the specified format
String logEntry = 'Lead: ' + info.firstName + ' ' + info.lastName +
' (Source: ' + info.leadSource + ', ID: ' + currentLeadId +
') was converted into this Contact at ' + currentDateTime;
// Append the new log entry on a new line to any existing Description content
if (con.Description == null) {
con.Description = logEntry;
} else {
con.Description += '\n' + logEntry;
}
update con;
System.debug('Updated Contact (Id: ' + contactId + ') Description with log entry: ' + logEntry);
} else {
System.debug('No Contact Id returned or Lead information missing.');
}
} else {
System.debug('Lead conversion was not successful.');
}
// Clear the static variables for next conversion
currentLeadId = null;
leadInfoMap.clear();
System.debug('--- afterConvert END ---');
}
}
Example 3: Override the Contact Email with the Lead Email during conversion
This plugin overrides the converted Contact’s email address by updating it with the original Lead’s email after a successful conversion. This deviates from the standard convert logic, which always retains information from the Contact over the Lead.
global class OverrideEmailConvertPlugin implements dupcheck.dc3Plugin.InterfaceConvert {
// Store the Lead email for use after conversion
private static String leadEmail;
private static Id currentLeadId;
global void beforeConvert(Database.LeadConvert leadConvertData) {
System.debug('--- beforeConvert START ---');
// Capture the Lead Id and query the Lead to get the email
currentLeadId = leadConvertData.getLeadId();
Lead leadRec = [SELECT Id, Email FROM Lead WHERE Id = :currentLeadId LIMIT 1];
leadEmail = leadRec.Email;
System.debug('Stored Lead Id: ' + currentLeadId);
System.debug('Stored Lead Email: ' + leadEmail);
System.debug('--- beforeConvert END ---');
}
global void convertFailed(Database.LeadConvert leadConvertData, dupcheck.dc3Exception.ConvertException exceptionData) {
System.debug('--- convertFailed ---');
System.debug('Conversion failed for Lead Id: ' + leadConvertData.getLeadId());
System.debug('Exception: ' + exceptionData.getMessage());
}
global void afterConvert(Database.LeadConvertResult leadConvertResult, Task taskData) {
System.debug('--- afterConvert START ---');
if (leadConvertResult.isSuccess()) {
Id contactId = leadConvertResult.getContactId();
if (contactId != null && leadEmail != null) {
// Retrieve the converted Contact record
Contact con = [SELECT Id, Email FROM Contact WHERE Id = :contactId LIMIT 1];
System.debug('Original Contact Email: ' + con.Email);
// Override the Contact's email with the Lead's email
con.Email = leadEmail;
update con;
System.debug('Updated Contact Email to: ' + leadEmail);
} else {
System.debug('No Contact was created or Lead Email not available.');
}
} else {
System.debug('Lead conversion was not successful.');
}
// Clear static variables for next conversion
currentLeadId = null;
leadEmail = null;
System.debug('--- afterConvert END ---');
}
}
Updated 17 days ago