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
piyushgupta20141.3916098907207268E12piyushgupta20141.3916098907207268E12 

Apex triggers

Hii 


I want to restrict user to prevent duplicate email ids.When I am entering email id like abc@gmail.com which is already present in database of product.
.

trigger trgr on Product__c (before insert)  {

//Take a List & Map

list<string> emailAddresses= new list<string>();
map<string,id> existingEmails= new map<string,id>();


//For all product that are being inserted
for(Product__c ptc: trigger.new) {

//If the Product have a n Email ID Field value then add that into the Email Address List.
if(ptc.Dealer_Email_id__c!=null) {
  emailAddresses.add(ptc.Dealer_Email_id__c);
}
}

//Extract all the products in a list which have the Delar Email in the above list.
list<Product__c> pclst = new list<Product__c> ([select id,Dealer_Email_id__c from Product__c where Dealer_Email_id__c IN: emailAddresses ]);


// And for all those newly inserted products . insert them in a map to check if the email is existed in the repository or not , if there is match then give error Duplicate not allowd.
for(Product__c pt: trigger.new) {
existingEmails.put(pt.Dealer_Email_id__c,pt.id);
}

for(Product__c ptc: trigger.new)
{
if(existingEmails.get(ptc.Dealer_Email_id__c)!=null)
{
ptc.addError('Duplicates not allowed');

}

}


}
piyushgupta20141.3916098907207268E12piyushgupta20141.3916098907207268E12
Sorry for incomplete questions, after implementing this trigger record is still get updated.
So what is the complete code for this. 
Siddhant IndraSiddhant Indra
Following code is working for insertion as well as updating existing record

trigger Book_FindDuplicateEmail on indra_sid__Book__c (before insert, before update) {
//Take a List & Map
    set<string> emailAddresses= new set<string>();
    map<string,id> existingEmails= new map<string,id>();

//For all product that are being inserted
    for(indra_sid__Book__c ptc: listOfNew) {
        //If the Product have a n Email ID Field value then add that into the Email Address List.
        if(ptc.Dealer_Email_id__c!=null) {
            emailAddresses.add(ptc.Dealer_Email_id__c);
        }
    }
   
    //Extract all the products in a list which have the Delar Email in the above list.
    list<indra_sid__Book__c> pclst = new list<indra_sid__Book__c> ([select id,Dealer_Email_id__c from indra_sid__Book__c where Dealer_Email_id__c IN: emailAddresses ]);
   
    for(indra_sid__Book__c newRecord : listOfNew){
        for(indra_sid__Book__c alreadyExist : pclst){
            if(alreadyExist.indra_sid__Dealer_Email_id__c == newRecord.indra_sid__Dealer_Email_id__c)
                newRecord.addError('Email already Exist');
        }
    }
}

Thanks
Siddhant
NK@BITNK@BIT
It would be like this..

trigger trgr on Product__c (before insert, before update)  {


//Extract all the products in a list which have the Delar Email in the above list.
list<Product__c> pclst = [select id,Dealer_Email_id__c from Product__c ];

for(Product__c p : pclst)
{
        for(Product__c ptc: trigger.new)
        {
                 if(ptc.Dealer_Email_id__c == p.Dealer_Email_id__c)
                 {
                             ptc.addError('Records Already Exist');
                  }
         }
}
}