You need to sign in to do that
Don't have an account?
MarkLevy
Code Coverage for Lead Insertion Trigger
I'm trying to create a new trigger before Lead insertion to check for duplicates from Leads or Contacts and prevent insertion. The duplicate check I'm going for is checking if newLeadEmail = existingEmail or if newLeadEmail = existingAltEmail (etc etc) basically preventing duplicates being added if alt email or email exists anywhere between the email or altEmail fields.
I'm getting a 71% code coverage unfortunately and really don't know how to go about making a test case cover more. I've created a test case that checks over some very basic insertion of Leads but it isn't covering my code.
Here are the lines for Lead checking that are not covered (similar lines for contact checking are also not covered):
*existingLeadList queries the database for existing leads that have Email or Alt Email matches
Any ideas on how to make a test case for this? Is there something I'm missing? Shouldn't basic Lead insertion test cases work for this?
Also as a side question: when I add the checking for alt email in my test for inserting duplicate leads it fails (naturally) because this trigger isn't in place yet to make it succeed at failing to insert a duplicate based on newEmail = oldAltEmail, but I shouldn't be publishing a test that fails should I? How do I deploy my trigger if the test I create to test it fails unless the trigger is active?
Any help here is appreciated. I'm a bit stuck at the moment. Thanks!
I'm getting a 71% code coverage unfortunately and really don't know how to go about making a test case cover more. I've created a test case that checks over some very basic insertion of Leads but it isn't covering my code.
Here are the lines for Lead checking that are not covered (similar lines for contact checking are also not covered):
*existingLeadList queries the database for existing leads that have Email or Alt Email matches
for(Lead newLead : Trigger.new){ for(Lead exisitingLead : existingLeadList ){ if((newLead.Email == exisitingLead.Email)||(newLead.Alt_Email__c == exisitingLead.Alt_Email__c)||(newLead.Email==exisitingLead.Alt_Email__c)||(newLead.Alt_Email__c == exisitingLead.Email)){ newLead.addError('Duplicate Lead'); } } }
Any ideas on how to make a test case for this? Is there something I'm missing? Shouldn't basic Lead insertion test cases work for this?
Also as a side question: when I add the checking for alt email in my test for inserting duplicate leads it fails (naturally) because this trigger isn't in place yet to make it succeed at failing to insert a duplicate based on newEmail = oldAltEmail, but I shouldn't be publishing a test that fails should I? How do I deploy my trigger if the test I create to test it fails unless the trigger is active?
Any help here is appreciated. I'm a bit stuck at the moment. Thanks!
All tests relating to insertion of lead will be testing your trigger. That's how it should be. It's testing the system as one, as opposed to individual pieces. If all pieces worked on their own, but none of them worked together, and tests only tested individual pieces, we would never know from unit testing that the project would fail.
Anyways, in your trigger code, you are testing email duplicates, so I would propose something like this ::
Lead testEmailLead = new Lead();
testEmailLead.email = 'testEmail@test.com';
(insert other required fields after this one)
insert testEmailLead;
Lead testDuplicateEmailLead = new Lead();
testDuplicateEmailLead = 'testEmail@test.com';
(insert other required fields after this one)
insert testDuplicateEmailLead;
This will give you the code coverage you need, however, don't forget to actually test to see if the lead is there or not. If you need help with that, let me know.
There are also some problems with your base code, for example, what if someone writes Testemail@test.com, then they write testEmail@test.com. Those would techincally be the same email addresses, but your code would allow it to happen.
Also, don't forget, you can insert more than one record at a time via data loader. In that case, you would be able to insert many leads at the same time with the same email, since your code compares trigger.new to already existing leads. The leads will be inserted 200 at a time. If more than one of those 200 had the same email, it would bypass your code.
All Answers
And the original code I posted is under a check for whethero r not the existingLeadList has a size > 0:
Why would there be no code coverage without existing leads?
Try putting a debug statement to check if there are any existing leads.
The entire point of the test class is to test expected cases, I.E. a case where there is no duplicate, and a case where there is a duplicate. The code portion that is not covered in your test class is the part where there are duplicates.
I was just doing tests like this:
All tests relating to insertion of lead will be testing your trigger. That's how it should be. It's testing the system as one, as opposed to individual pieces. If all pieces worked on their own, but none of them worked together, and tests only tested individual pieces, we would never know from unit testing that the project would fail.
Anyways, in your trigger code, you are testing email duplicates, so I would propose something like this ::
Lead testEmailLead = new Lead();
testEmailLead.email = 'testEmail@test.com';
(insert other required fields after this one)
insert testEmailLead;
Lead testDuplicateEmailLead = new Lead();
testDuplicateEmailLead = 'testEmail@test.com';
(insert other required fields after this one)
insert testDuplicateEmailLead;
This will give you the code coverage you need, however, don't forget to actually test to see if the lead is there or not. If you need help with that, let me know.
There are also some problems with your base code, for example, what if someone writes Testemail@test.com, then they write testEmail@test.com. Those would techincally be the same email addresses, but your code would allow it to happen.
Also, don't forget, you can insert more than one record at a time via data loader. In that case, you would be able to insert many leads at the same time with the same email, since your code compares trigger.new to already existing leads. The leads will be inserted 200 at a time. If more than one of those 200 had the same email, it would bypass your code.
As far as I understand string comparisons in SOQL are case insensitive, is that incorrect? https://developer.salesforce.com/forums/?id=906F0000000B1QkIAK