You need to sign in to do that
Don't have an account?
Problem with auto-convert trigger on Lead
I have a requirement to automatically convert a Lead if the Lead status is a specific value (the Lead is to be converted when Lead.Status = 'Complete Request - Convert'). I wrote an insert/update trigger that performs the conversion process. It seems to be working fine when I test it by creating a Lead thru the Salesforce UI. If I create it with the specified status value, it is immediately converted upon saving my new record. So, I then wrote a unit test to test it programmatically. My unit test code looks like this:
// Tests successful conversion of a single Lead inserted directly in the 'Complete Request - Convert' status
public static testMethod void testSingleInsert() {
RecordType clientLeadRT = [SELECT Id FROM RecordType WHERE DeveloperName = 'Client' AND SObjectType = 'Lead'];
Lead l = new Lead();
l.FirstName = 'Test';
l.LastName = 'Lead1';
l.Company = 'Test Company';
l.Phone = '3335552233';
l.Email = 'tlead1@test.com';
l.Status = 'Complete Request - Convert';
l.Street = '123 Test Street';
l.City = 'Test Town';
l.State = 'TN';
l.PostalCode = '12345';
l.RecordTypeId = clientLeadRT.Id;
insert l;
// Retrieve the Lead... it should have been converted
Lead convertedLead = [SELECT Id, IsConverted, ConvertedContactId, ConvertedAccountId, Status FROM Lead WHERE Id = :l.Id];
System.assert(convertedLead.IsConverted);
System.assertEquals('Qualified', convertedLead.Status);
// Retrieve the converted Contact
Contact convertedContact = [SELECT Id, FirstName, LastName, CompanyName__c, AccountId, RecordType.Name,
Phone, Email, MailingStreet, MailingCity, MailingState, MailingPostalCode
FROM Contact WHERE Id = :convertedLead.ConvertedContactId];
System.assertEquals(l.FirstName, convertedContact.FirstName);
System.assertEquals(l.LastName, convertedContact.LastName);
System.assertEquals(l.Company, convertedContact.CompanyName__c);
System.assertEquals(convertedLead.ConvertedAccountId, convertedContact.AccountId);
System.assertEquals(l.Phone, convertedContact.Phone);
System.assertEquals(l.Street, convertedContact.MailingStreet);
System.assertEquals(l.City, convertedContact.MailingCity);
System.assertEquals(l.State, convertedContact.MailingState);
System.assertEquals(l.PostalCode, convertedContact.MailingPostalCode);
System.assertEquals('Client', convertedContact.RecordType.Name);
}
The status value 'Complete Request - Convert' is the value that triggers the auto-conversion. When I run this test, I get an exception at the "insert l;" statement (highlighted in red). The error is:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: []
I get the same error if I try to create a Lead in this state from the "Execute Anonymous" window.
If I create the Lead in a different state, and then update it to "Complete Request - Convert", it works fine. Is there any workaround for this, or can I not insert a Lead that is converted by a trigger in the same transaction?
By the way, the trigger is an "after insert, after update" trigger.
Please post your trigger code. Is the status you're using a "converted status"?
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_leadstatus.htm?SearchType=Stem&Highlight=LeadStatus
The status value I am using is not a "converted" status value. It is merely a value that I am using to trigger the conversion operation. Here is the trigger code:
An here is the LeadConversionUtils class that contains the "converClientLeads" method:
Note that it works fine if I first create the Lead in some other status and then update the Lead to set the status to the trigger value of "Complete Request - Convert". In this case, the Lead is converted and all of the additional DML operations are performed.
I'm not sure what the problem is as you're checking for recursion w/ the !l.IsConverted check, which should prevent any converted leads from popping into your utility.
Do you have any other updates that occur upon lead update?
My best suggestion is to add a bunch more logging to understand where the issue starts.
Add lead status value (which you are passing to the converted lead status) to status field picklist on lead.
DOC Link : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_convertLead.htm http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_leadstatus.htm
trigger ConvertLead on Lead (after insert, after update) {
for (Lead lead : Trigger.new) {
if (lead.isConverted == false) //to prevent recursion {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id); String oppName = lead.Name;
lc.setOpportunityName(oppName);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
}
}
}
Regards,
Mayank Joshi