You need to sign in to do that
Don't have an account?
Lead Conversion Trigger
Hey there,
I have this trigger, except it will not let me deploy it via changesets without testing the code coverage. I need to test for this, I was wondering if someone could help me out with some code in order to test the trigger?
The trigger is written as:
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());
}
}
}
How would I also go about removing the creation of an opportunity? Do i just remove the section that goes:
String oppName = lead.Name;
lc.setOpportunityName(oppName);
Any help I can get would be very much appreciated.
Mikie
Hi miki,
By using the below code snnipet u can test your trigger. and u can deploy the trigger. just copy thic code and create a new apex class. then run the test, now u can see the code coverage.
@isTest(seealldata=true)
private class ConvertLead_Test{
static testMethod void invenCheck(){
Lead a = new Lead(LastName = 'TestLead', Company = 'Test', Status = 'Open - Not Contacted', isConverted = false);
insert a;
}
}
All Answers
remove that line and replace them with
Thankyou very much. How would I go about testing this?
Hi miki,
By using the below code snnipet u can test your trigger. and u can deploy the trigger. just copy thic code and create a new apex class. then run the test, now u can see the code coverage.
@isTest(seealldata=true)
private class ConvertLead_Test{
static testMethod void invenCheck(){
Lead a = new Lead(LastName = 'TestLead', Company = 'Test', Status = 'Open - Not Contacted', isConverted = false);
insert a;
}
}
Thankyou sooo much Surren and Avid,
You have helped me out big time. I really appreciate it.
Mikie
Hey Surren,
I have recently deployed the trigger. Except when attempting to use the web to lead, the lead was not created because of error:
caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, System.DmlException: Update failed. First exception on row 0 with id 00Q9000000JsHTXEA3; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: []
(System Code)
: []: Trigger.ConvertLead: line 14, column 1
Lead Capture Page: Not available.
Do you know how I may rectify this?
Mikie