• piyushgupta20141.3916098907207268E12
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
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');

}

}


}
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');

}

}


}