This is My Trigger for finding duplicate for Phone And MobilePhone
trigger duplicate_mobile on Contact (before insert, before update) { Map<String,Contact> mapContact = new Map<String,Contact>(); for(Contact cont :Trigger.new){ if((cont.phone != null ) && (System.Trigger.isInsert) ||(cont.phone != System.trigger.oldmap.get(cont.Id).phone)){ mapcontact.put(cont.phone,cont); } if((cont.MobilePhone!= null) && (system.trigger.isInsert) ||(cont.MobilePhone!= system.trigger.oldmap.get(cont.id).MobilePhone)){ mapcontact.put(cont.MobilePhone,cont); } } for(contact cont :[select phone from contact where phone in:mapcontact.keyset()]){ contact cc = mapcontact.get(cont.phone); cc.phone.adderror('error'); } for(contact con :[select MobilePhone from contact where MobilePhone in:mapcontact.keyset()]){ contact ccc = mapcontact.get(con.MobilePhone); ccc.MobilePhone.adderror('error already no is saved'); } } This is my unit test
@isTest //test methos for duplicate_mobile for contact private class test_duplicate_mobile{ static testmethod void dup_phone(){ contact contw = new contact (); contw.lastname = 'tester'; contw.phone = '9999999999'; contw.mobilephone = '8888888888'; insert contw; contact contr = new contact(); contr.lastname='as'; contr.phone = '7777777777'; contr.mobilephone='0000000000'; insert contr; contw.phone = '11111111111'; contw.mobilephone = '8888888887'; update contw; } } I am getting 66% code coverage because it is not entering for loops this is snapshot of code coverage
