I have two triggers on the Lead object. One (before insert) checks to see if the Lead matches any existing Contacts (through a custom email object) and the other (after insert, after update) converts the Lead if the source and status match our criteria.
My issue is that it's very inconsistent with actually doing the conversion. Most leads are not converted, even though there isn't any difference in the leads that are compared to those that aren't. When I observed it wasn't usually working, I added an else that changes the status to "Conversion Pending" if it does meet the criteria, and this is in fact what happens.
Trigger 1
trigger CheckLead on Lead (before insert) {
// make a list of email addresses for the leads we are adding
List<String> leadEmails = new List<String>();
for (Lead lead:Trigger.new) {
leadEmails.add(lead.Email);
}
// find all email objects associated with a matching email address for any of the leads
List<Email__c> allMatchingEmails = [SELECT Id, Email_Address__c FROM Email__c WHERE Email_Address__c IN :leadEmails];
// for each new lead
for (Lead lead:Trigger.new) {
// set the company value to the lead name
if (lead.Company != null && lead.Company != '[not provided]') {
lead.PersonCompany__c = lead.Company;
}
lead.Company = lead.FirstName + ' ' + lead.LastName;
// get the name of the lead we want to add
String leadName = '';
if (lead.FirstName == null || lead.FirstName == 'not provided' || lead.FirstName == '[not provided]' || lead.FirstName.contains('not provided') == true || lead.FirstName.contains('null') == true || lead.FirstName.contains('not') == true || lead.FirstName.contains('provided') == true) {
lead.FirstName = '[unknown]';
}
if (lead.LastName == null || lead.LastName.contains('not provided') == true || lead.LastName.contains('null') == true || lead.LastName.contains('not') == true || lead.LastName.contains('provided') == true) {
lead.LastName = '[unknown]';
}
if (lead.Salutation != '' && lead.Salutation != 'not provided' && lead.Salutation != null) {
leadName += lead.Salutation + ' ';
}
if (lead.FirstName != '') {
leadName += lead.FirstName + ' ';
}
if (lead.MiddleName != '' && lead.MiddleName != null) {
leadName += lead.MiddleName;
}
if (lead.LastName != '') {
leadName += lead.LastName;
}
if (lead.Suffix != '' && lead.Suffix != null) {
leadName += lead.Suffix;
}
// the email address we want to add exists
if (allMatchingEmails.size() > 0) {
// are any of the email addresses we want to add already associated with another user?
List<Contact_Email__c> primaryEmailMatches = [SELECT Id, Email__c, Contact__c FROM Contact_Email__c WHERE Email__c IN :allMatchingEmails AND Primary_contact_for_email__c = true];
List<Contact_Email__c> noPrimaryEmail = [SELECT Id, Email__c, Contact__c FROM Contact_Email__c WHERE Email__c IN :allMatchingEmails AND Primary_contact_for_email__c != true];
if (!primaryEmailMatches.isEmpty()) {
// there is a contact who is primary for this email
// for each lead, if there is another primary user with the email, get that contact's information
// don't convert it automatically if the names don't match
String contactID = primaryEmailMatches[0].Contact__c;
Contact contact = [Select Id, Salutation, FirstName, MiddleName, LastName, Suffix, Name FROM Contact WHERE Id = :contactId];
// the name and email are the same; assume this is the same person
if (leadName == contact.Name) {
lead.Status = 'Matched With Contact';
lead.Lead_Status_Reason__c = 'Matches primary email address and name';
} else {
// there is a matching primary here, but the names do not match
// change the status
lead.Status = 'Contact Check Pending';
lead.Lead_Status_Reason__c = 'Primary Contact Name Mismatch';
}
} else if (!noPrimaryEmail.isEmpty()) {
// no contact has this email as primary but other contacts do have it
// don't convert it automatically if the names don't match
// for each lead, if there is another user with the email, get that contact's information
String contactID = noPrimaryEmail[0].Contact__c;
Contact contact = [Select Id, Salutation, FirstName, MiddleName, LastName, Suffix, Name FROM Contact WHERE Id = :contactId];
// the name and email are the same; assume this is the same person
if (leadName == contact.Name) {
lead.Status = 'Matched With Contact';
lead.Lead_Status_Reason__c = 'Matches non-primary email address and name';
} else {
// there is a matching email here, but it is not primary and the names do not match
// change the status
lead.Status = 'Contact Check Pending';
lead.Lead_Status_Reason__c = 'No Primary Contact';
}
} else {
lead.Status = 'Conversion Pending';
lead.Lead_Status_Reason__c = 'No Contact For Email';
}
} else {
lead.Status = 'Conversion Pending';
lead.Lead_Status_Reason__c = 'Email Does Not Exist';
}
}
}
** Trigger 2**
trigger ConvertLead on Lead (after insert, after update) {
// make a leadconvert list
list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
// for each lead, change its status
for (Lead myLead : Trigger.new) {
if (myLead.LeadSource == 'MailChimp' && (myLead.Status == 'Open' || myLead.Status == 'Conversion Pending') && myLead.Status != 'Contact Check Pending') {
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(myLead.Id);
lc.convertedStatus = 'Qualified';
lc.setDoNotCreateOpportunity(true);
leadConverts.add(lc);
}
}
// run the conversion on those leads
if (!leadConverts.isEmpty()) {
for (Database.LeadConvert myConvert : leadConverts) {
Database.LeadConvertResult lcr = Database.convertLead(myConvert,false);
}
}
}
So again, the vast majority of the Leads that I'm seeing get imported (via manual import, in this case) are being assigned the status of "Conversion Pending" but they're not actually getting converted.
I did notice that I can go and manually update a Lead - for example change a field's value to "1" where it was empty, and then it gets converted. It does not do this if I use something to mass update the values (Apsona, for example).
Any help would be most appreciated.
Thanks.
trigger.new
. At the end of your trigger, do an upsert on any leads in the error message class.