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
Reshmi SmijuReshmi Smiju 

Pls help me on this Trigger

Hi,
This trigger is used to find the duplicates while entering the new contacts.

trigger dupcon on Contact (before insert, before update){
for (Contact c :trigger.new) {
//c.adderror('Hello');
if (c.FirstName != null && c.LastName!= null && c.BirthDate!= null) {
c.adderror('Hello');
List <Contact> dupcon = [SELECT Id FROM Contact WHERE FirstName = :c.FirstName AND LastName = :c.LastName ];
if (dupcon.size() >0 ) {
String Errmsg = 'ERROR';
Errmsg += 'Record Id'+dupcon[0].Id;
c.adderror(Errmsg);
}
}
}
}
 Seems like the If condition doesnt work. I tried to add an errormessage just before the if statement. Then "hello" appears along  with an Error msg saying "Contacts not associated with accounts are private and cannot be viewed by other users or included in reports."

Please help me on this . Thanks Much in Advance
Best Answer chosen by Reshmi Smiju
SS KarthickSS Karthick
@Reshmi Smiju,
There is it will hit the soql limit if you insert more than 100 records via data loader.
otherwise it will work if you remove the addError('hello') 


Thanks 
Karthick

 

All Answers

SS KarthickSS Karthick
Reshmi SmijuReshmi Smiju

Hi Karthik,

I am referring the code from David Site only :). I just tried to implement the same .. But the If statement is not working. . Thats te reason, I just posted for the help.

SS KarthickSS Karthick
@Reshmi Smiju,
Try below code
trigger dupcon on Contact (before insert, before update){
    Set<String> names=new Set<String>();
    for(Contact con:[select LastName from Contact where Id!=null]){
        names.add(con.LastName);
    }
    for(Contact c:trigger.new){
        if(names.contains(c.LastName))
            c.addError('Contact LastName already exists'); 
    }   
}


Please mark this post as solved so that it benifit others. 

Thanks 
Karthick
 
Reshmi SmijuReshmi Smiju
Hai Karthick,
Thanks for the new code. But one Question. What's  the  problem with other code. Why that Contat Visibility error is happening
SS KarthickSS Karthick
@Reshmi Smiju,
There is it will hit the soql limit if you insert more than 100 records via data loader.
otherwise it will work if you remove the addError('hello') 


Thanks 
Karthick

 
This was selected as the best answer