| 1 |
public class testBlockDuplicatesLeadTrigger { |
| 2 |
|
| 3 |
static testMethod void testDuplicateTrigger(){ |
| 4 |
|
| 5 |
Lead[] l1 =new Lead[]{ |
| 6 |
new Lead( Email='[email protected]', LastName='Simpson', Company='fox' ) |
| 7 |
}; |
| 8 |
insert l1; // add a known lead |
| 9 |
|
| 10 |
Lead[] l2 =new Lead[]{ |
| 11 |
new Lead( Email='[email protected]', LastName='Simpson', Company='fox' ) |
| 12 |
}; |
| 13 |
// try to add a matching lead |
| 14 |
try { insert l2; } catch ( System.DmlException e) { |
| 15 |
system.assert(e.getMessage().contains('first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A lead with this email address already exists'), |
| 16 |
e.getMessage()); |
| 17 |
} |
| 18 |
|
| 19 |
// test duplicates in the same batch |
| 20 |
Lead[] l3 =new Lead[]{ |
| 21 |
new Lead( Email='[email protected]', LastName='Simpson', Company='fox' ), |
| 22 |
new Lead( Email='[email protected]', LastName='Simpson', Company='fox' ) |
| 23 |
}; |
| 24 |
try { insert l3; } catch ( System.DmlException e) { |
| 25 |
system.assert(e.getMessage().contains('first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Another new lead has the same email'), |
| 26 |
e.getMessage()); |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
// test update also |
| 31 |
Lead[] lup = new Lead[]{ |
| 32 |
new Lead( Email='[email protected]', LastName='Simpson', Company='fox' ) |
| 33 |
}; |
| 34 |
insert lup; |
| 35 |
Lead marge = [ select id,Email from lead where Email = '[email protected]' limit 1]; |
| 36 |
system.assert(marge!=null); |
| 37 |
marge.Email = '[email protected]'; |
| 38 |
|
| 39 |
try { update marge; } catch ( System.DmlException e) { |
| 40 |
system.assert(e.getMessage().contains('irst error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A lead with this email address already exists'), |
| 41 |
e.getMessage()); |
| 42 |
} |
| 43 |
} |
| 44 |
} |