You need to sign in to do that
Don't have an account?
Manish Kumar 23
Test class for checking the duplicate
Hi All,
I have written a trigger for duplicate lead check.
When I am writting a test class I am getting only 66% of code coverage. Can you please help me
Below is the trigger for your reference.
trigger leadDuplicatePreventer on Lead
(before insert, after update) {
Map<String, Lead> leadMap = new Map<String, Lead>();
for (Lead lead : System.Trigger.new) {
// Make sure we don't treat an email address that
// isn't changing during an update as a duplicate.
if ((lead.Email != null) &&
(System.Trigger.isInsert ||
(lead.Email !=
System.Trigger.oldMap.get(lead.Id).Email))) {
// Make sure another new lead isn't also a duplicate
if (leadMap.containsKey(lead.Email)) {
lead.Email.addError('Another new lead has the '
+ 'same email address.');
} else {
leadMap.put(lead.Email, lead);
}
}
}
// Using a single database query, find all the leads in
// the database that have the same email address as any
// of the leads being inserted or updated.
for (Lead lead : [SELECT Email FROM Lead
WHERE Email IN :leadMap.KeySet()]) {
Lead newLead = leadMap.get(lead.Email);
newLead.Email.addError('A lead with this email '
+ 'address already exists.');
}
}
I have written a trigger for duplicate lead check.
When I am writting a test class I am getting only 66% of code coverage. Can you please help me
Below is the trigger for your reference.
trigger leadDuplicatePreventer on Lead
(before insert, after update) {
Map<String, Lead> leadMap = new Map<String, Lead>();
for (Lead lead : System.Trigger.new) {
// Make sure we don't treat an email address that
// isn't changing during an update as a duplicate.
if ((lead.Email != null) &&
(System.Trigger.isInsert ||
(lead.Email !=
System.Trigger.oldMap.get(lead.Id).Email))) {
// Make sure another new lead isn't also a duplicate
if (leadMap.containsKey(lead.Email)) {
lead.Email.addError('Another new lead has the '
+ 'same email address.');
} else {
leadMap.put(lead.Email, lead);
}
}
}
// Using a single database query, find all the leads in
// the database that have the same email address as any
// of the leads being inserted or updated.
for (Lead lead : [SELECT Email FROM Lead
WHERE Email IN :leadMap.KeySet()]) {
Lead newLead = leadMap.get(lead.Email);
newLead.Email.addError('A lead with this email '
+ 'address already exists.');
}
}
You should use before update event in your trigger.
Try the below code.
This will solve your issue.
Thanks..
All Answers
Check the below link for test class.
http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving
Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 47 column 42
Try this.
But not sure It is showing failed"System.DmlException: Update failed. First exception on row 1 with id 00Q9000000V3ufxEAB; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A lead with this email address already exists.: [Email]"
I made some Changes in the existing code.
Try now.
System.DmlException: Update failed. First exception on row 1 with id 00Q9000000V3uozEAB; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A lead with this email address already exists.: [Email]
You should use before update event in your trigger.
Try the below code.
This will solve your issue.
Thanks..
Thanks for your help and time
Can we use same logic or trigger and test class for duplicate check on contacts also.
Awaiting your inputs
Yes, You can write a new Trigger on contact with the same logic.