You need to sign in to do that
Don't have an account?
adambreen.ax1546
How to properly test a Lead Conversion (trigger)?
I have this trigger defined:
trigger convertLeadToImplementationOpportunity on Lead (before update) { for (Lead lead : Trigger.new){ if(lead.Status == 'Qualified' && !lead.isConverted && lead.isApproved__c == true){ Database.LeadConvert leadConvert = new database.LeadConvert(); LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; leadConvert.setConvertedStatus(convertStatus.MasterLabel); // ("Closed - Converted"); leadConvert.setLeadId(lead.id); Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert); System.assert(leadConvertResult.isSuccess()); } } }
When I execute the main trigger code anonymously via the IDE, it succeeds.
I have this (failing) test defined for the trigger:
@isTest privateclass tests { static testMethod void approvedLeadShouldConvertToImplementationOpportunity() { Lead testLead = new Lead(FirstName='John', LastName='Smith', Status='Qualified'); insert testLead; test.startTest(); testlead.isApproved__c = true; update testlead; //trigger convertLeadToImplementationOpportunity test.stopTest(); Lead isConverted = [SELECT id, isConverted FROM Lead WHERE Id=:testlead.id]; System.assert(isConverted.isConverted); // fails - i.e. isConverted == false } }
Can anyone please tell me why the assert is failing? I'm quite new to Apex, so forgive any obvious stupidity.
Thanks!
Adam Breen
yeah that trigger needs to be re-written to bulkify it. but it will work in the ui also it needs to be after update not before update. that won't work with before update i don't think.
this test will work for that trigger but the trigger. this test passes that trigger but i believe you might want to write the trigger to handle bulk updates on leads
All Answers
adambreen,
Your trigger is not bulkified, has query and Dml statements inside a for loop, which is not a good practice. Perhaps it will fail while bulk update. Just wanted to let you know this, Which is not issue related to test class failure.
The best way to know the failure cause is to put system.debug's in appropriate places in trigger as well as test class.
Check if your trigger is getting fired from test class and also whether its getting inside trigger if loop.
yeah that trigger needs to be re-written to bulkify it. but it will work in the ui also it needs to be after update not before update. that won't work with before update i don't think.
this test will work for that trigger but the trigger. this test passes that trigger but i believe you might want to write the trigger to handle bulk updates on leads