I write the trigger to check duplicate phone on lead object;
Code as:
trigger DPhone on lead(before insert,before update)
{
List<Lead> Leadlist =new list<Lead>();
for(lead l:trigger.new)
{
If(l.Phone!=null)
Leadlist = [select id from Lead where Phone=:l.Phone];
If (Leadlist.size()>0)
l.Phone.adderror('Another record has same Phone No.');
}
}
When I create the record its work fine for me . but when i edit the same record and make change in any field, trigger again fire and give me error duplicate phone.
Best Answer
There are two issues with your code.
It is throwing error on update for other field as well because you are not checking update of phone field only so solution for that problem is we will check phone field explicitly from Trigger.oldmap (it contains previous value)