function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SKT CloudComputingSKT CloudComputing 

Issue with trigger.

I am writing a trigger which will check and prevent creation of  duplicate contatcs.
It's working fine for email but it's not checking Name .

trigger toCheckDuplicateRecord on Contact(before update,before insert) {

 List<Contact> Contactlst= new List<Contact>();
 Contactlst= [select Name,Email from Contact];
 for(Contact conobj:Contactlst)
 {
 for(Contact con:trigger.new){
    if(conobj.Email==con.Email || conobj.Name==con.Name)
    con.adderror('You can not enter this contact');
 }
 }
}

Please suggest.
 
R Z KhanR Z Khan
Hi,

yuo need ot bulkify your code. Also you cannot query all the contacts, you might hit the SOQL limit. Try the following code
 
Map<String, Contact> emailToContact = new Map<String, Contact>();
Map<String, Contact> nameToContact = new Map<String, Contact>();

List<Contact> contacts = [SELECT Name, Email from COntact where Name IN :nameToContact.keyset() OR Email IN :emailToContact];

for(Contact cnt : contacts){
    if(emailToContact.get(cnt.Email) != null){
     emailToContact.get(cnt.Email).addError('text');
}
if(nameToContact.get(cnt.Name) != null){
     nameToContact.get(cnt.Name).addError('text');
}

}

 
Pankaj_GanwaniPankaj_Ganwani
Hi,

Since Name is the combination of FirstName and LastName, so this cannot be retrieved in before insert event. Use below mentioned line to obtain the name of trigger.new contacts and then do comparison:
String conName = con.FirstName!=NULL ? con.FirstName.trim()+' '+con.LastName.trim() : con.LastName;
SKT CloudComputingSKT CloudComputing
Thanks R Z Khan.

I got below eeror when I used the same code-

Compile Error: unexpected token: Map at line 1 column 0
R Z KhanR Z Khan
Oh i forgot to populate hte map. and yes, Pankaj is right the Name is not availabel in before insert. try the follwoing code and let me knwo if it works for you
trigger Contact on Contact(before insert, before update){

Map<String, Contact> emailToContact = new Map<String, Contact>();
Map<String, Contact> nameToContact = new Map<String, Contact>();

for(Contact cnt:trigger.new){
String name = cnt.LastName;
if(cnt.FirstName != null){
 name = cnt.FirstName +' ' +name;
}
 emailToContact.put(cnt.Email, cnt);
nameToContact.put(name, cnt);
}

List<Contact> contacts = [SELECT FirstName, LastName, Email from Contact where Name IN :nameToContact.keyset() OR Email IN :emailToContact];


for(Contact cnt : contacts){

    if(emailToContact.get(cnt.Email) != null){

     emailToContact.get(cnt.Email).addError('text');

}

String name = cnt.LastName;
if(cnt.FirstName != null){
 name = cnt.FirstName +' ' +name;
}

if(nameToContact.get(name) != null){

     nameToContact.get(name).addError('text');

}
}
}
SKT CloudComputingSKT CloudComputing
Thanks RZ Khan.

I modified my trigger as belkow and it works fine now-

trigger toCheckDuplicateRecord on Contact(before insert) {

 List<Contact> Contactlst= new List<Contact>();
 Contactlst= [select LastName,Email from Contact];
 for(Contact conobj:Contactlst)
 {
 for(Contact con:trigger.new){
  if((conobj.Email==con.Email && con.email!=NULL)||conobj.LastName==con.LastName ){
    con.adderror('You can not enter this contact');
  }
 }
 }
}
R Z KhanR Z Khan
Hi SkT,

1. your trigger needs to handle update as well as insert. 
2. You cannot query all of the contacts in your org, once you push it to production it will break. 
3. Was you requirement that 2 contacts can't have the same lastname or they can't have the same full name? right now you are onyl checking lastname.

Try the code i posted in my previous reply. It should work for oyu