You need to sign in to do that
Don't have an account?
Joaquin_tegui
Help with LeadConvert Trigger Unit Test
Hi, this is the first time I do an important trigger and test class, and the first time asking for help!
I'm writing a trigger to convert leads automatically for an organization that doesn't use leads and wants all leads converted.
These are the requirements:
If the email field of the lead exist as a contact it merge the converted lead with that Contact and Creates an opportunity.
If the email field of the lead is blank it only converts it as a Account and Contact with no opportunity
If the email field of the lead is new (is not in a contact) it converts the lead into Account, Contact and Opportunity
The trigger is working fine (although comments are much appreciated) but I'm stuck in the Test class and needing some guidance.
This is my trigger:
This is the test i got so far:
This is the error I'm getting:
When i do it manually it works, I don't understand why it's failing.
Thanks in advance, hope I'm in the right direction, please excuse me for the spanish variable names.
I'm writing a trigger to convert leads automatically for an organization that doesn't use leads and wants all leads converted.
These are the requirements:
If the email field of the lead exist as a contact it merge the converted lead with that Contact and Creates an opportunity.
If the email field of the lead is blank it only converts it as a Account and Contact with no opportunity
If the email field of the lead is new (is not in a contact) it converts the lead into Account, Contact and Opportunity
The trigger is working fine (although comments are much appreciated) but I'm stuck in the Test class and needing some guidance.
This is my trigger:
trigger AutoConvert on Lead (after insert) { list<Lead> LeadsSinDup = new list<Lead>(); list<Lead> LeadsDup = new List<Lead>(); list<Lead> LeadsSinEmail = new List<Lead>(); for(Lead myLead: Trigger.new){ list<Contact> contactDupEmail = [SELECT Id FROM Contact WHERE Email = :myLead.Email LIMIT 1]; if(contactDupEmail.size() == 0) { LeadsSinDup.add(myLead); }else if(myLead.Email == NULL ){ LeadsSinEmail.add(myLead); }else{ LeadsDup.add(myLead); } } list<Database.LeadConvert> leadConvertsSinDup = new list<Database.LeadConvert>(); list<Database.LeadConvert> leadConvertsDup = new list<Database.LeadConvert>(); list<Database.LeadConvert> leadConvertSinMail = new list<Database.LeadConvert>(); for(Lead myLead : LeadsSinDup){ Database.LeadConvert leadSinDup = new database.LeadConvert(); leadSinDup.setLeadId(myLead.Id); leadSinDup.convertedStatus = 'Closed - Converted'; Database.ConvertLead(leadSinDup,true); leadConvertsSinDup.add(leadSinDup); } for(Lead myLead : LeadsDup){ List<Contact> contactDup = [SELECT Id, AccountId, Title FROM Contact WHERE Email = :myLead.Email LIMIT 1]; Database.LeadConvert leadDup = new database.LeadConvert(); leadDup.setLeadId(myLead.Id); leadDup.setAccountId(contactDup[0].AccountId); leadDup.setContactId(contactDup[0].id); leadDup.convertedStatus = 'Closed - Converted'; leadConvertsDup.add(leadDup); } for(Lead myLead : LeadsSinEmail){ Database.LeadConvert LeadSinEmail = new database.LeadConvert(); LeadSinEmail.setLeadId(myLead.Id); LeadSinEmail.convertedStatus = 'Closed - Converted'; LeadSinEmail.setDoNotCreateOpportunity(true); leadConvertSinMail.add(LeadSinEmail); } Database.convertLead(leadConvertsDup,true); Database.convertLead(leadConvertSinMail,true); Database.convertLead(leadConvertsSinDup,true); }
This is the test i got so far:
@isTest private class TestAutoConvert { static testMethod void leadTest(){ List<Lead> leadsConv = new List<Lead>(); Contact testContacto = new Contact(); testContacto.LastName = 'salesforce'; testContacto.Email = 'duplicado@dup.com'; Lead testLeadNoDup = new Lead(); testLeadNoDup.LastName = 'joaquin'; testLeadNoDup.Company = 'La Cle'; testLeadNoDup.Email = 'joaquin@lacle.com'; leadsConv.add(testLeadNoDup); Lead testLeadDup = new Lead(); testLeadDup.LastName = 'Elena'; testLeadDup.Company = 'MVP'; testLeadDup.Email = 'duplicado@dup.com'; leadsConv.add(testLeadDup); Lead testLeadNoMail = new Lead(); testLeadNoMail.LastName = 'John'; testLeadNoMail.Company = 'No email'; leadsConv.add(testLeadNoMail); insert testContacto; insert leadsConv; System.assert(testLeadDup.IsConverted); System.assertEquals(testContacto.AccountId,testLeadDup.ConvertedAccountId); System.assertEquals(testContacto.Id, testLeadDup.ConvertedContactId); System.assertEquals(true, testLeadNoDup.IsConverted); System.assertNotEquals(null, testLeadNoDup.ConvertedOpportunityId); System.assertEquals(true, testLeadNoMail.IsConverted); System.assertEquals(null, testLeadNoMail.ConvertedOpportunityId); } }
This is the error I'm getting:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AutoConvert: execution of AfterInsert caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, accountId must be specified if contactId is not null: [Id] Trigger.AutoConvert: line 48, column 1: []And the Stack trace says 'Class.TestAutoConvert.leadTest: line 32, column 1'
When i do it manually it works, I don't understand why it's failing.
Thanks in advance, hope I'm in the right direction, please excuse me for the spanish variable names.
Then try below test class Let us know if this will help you
All Answers
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
Please try below test class. Please follow below salesforce Best Practice for Test Classes :-
1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .
Please let us know if this post will help you
Thanks,
Amit Chaudhary
Sadly your code didn't work either it give me this error:
And on the Stack trace it says Class.TestAutoConvert.leadTest: line 20, column 1
Thanks Again!
Then try below test class Let us know if this will help you
Thank you so much, you're awesome! Now Im going to work on the best practices of Tests