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
Sonu06Sonu06 

Apex triggerss

Hey guys , I am beginner to salesforce, My requirement is to write the Apex trigger,
-- Suppose I have 1 account Name X and It has 2 contacts, so for the same account X , the email of the Contacts should not be same , if same It should show error. Suppose 1 contact email is abc@g.com and 2 contact email is xyz @g.com , this should be accepted. if both contact has same email address then it should not be accepted.

But  if have another account Name Y and it has 2 contact, and if email of 1 of the contact is abc@g.com  which is the email of  contact of account  X then it should be accepted, 

So please suggest me the apex trigger for the above requirement.
Note- Please dont share any link for this,, if possible give me explanation with example 

Thank you
Best Answer chosen by Sonu06
Deepali KulshresthaDeepali Kulshrestha
Hi Sonal,

Greetings to you!

I have updated this trigger as you asked using Map. I have created a map of accountId Vs List Of previous contact email. Please mark as Best Answer to help others too.
 
Trigger ContactTrigger on Contact (before insert,before update) {
    if(Trigger.isbefore){
        if(Trigger.isinsert ||Trigger.isupdate){
            Set<Id> accountIdSet = new Set<Id>();
            List<Contact> contactList = new List<Contact>();
            Map<Id,List<String>> accountIdVsConEmail = new Map<Id,List<String>>();
            for(Contact ContactObj: Trigger.new) {
                if(ContactObj.AccountId != Null)
                    accountIdSet.add(ContactObj.AccountId);
            }
            contactList = [Select Id,AccountId,Email From Contact Where AccountId In :accountIdSet Limit 50000];
            
            for(Contact Obj: contactList){
                if(!accountIdVsConEmail.containsKey(Obj.AccountId)){
                    accountIdVsConEmail.put(Obj.AccountId, new List<String>());
                }
                accountIdVsConEmail.get(Obj.AccountId).add(Obj.Email);
            }
            
            
            for(Contact ContactObj2: Trigger.new){
                if(accountIdVsConEmail.containsKey(ContactObj2.AccountId)){
                    if(accountIdVsConEmail.get(ContactObj2.AccountId).size() > 0 && accountIdVsConEmail.get(ContactObj2.AccountId).contains(ContactObj2.Email)){
                        ContactObj2.adderror('Duplicate Email, Please Use Other Email') ;
                    }
                }
            }
        }  
    }
    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Sonal,

Greetings to you!

I have written a trigger as per your requirement. You can use this code. I have check all others contact that have lookup with the same account and if it have then I match the email and if they have the same email then I added error in that contact.
 
Trigger ContactTrigger on Contact (before insert,before update) {
    if(Trigger.isbefore){
        if(Trigger.isinsert ||Trigger.isupdate){
            Set<Id> accountIdSet = new Set<Id>();
            List<Contact> contactList = new List<Contact>();
            for(Contact ContactObj: Trigger.new) {
                if(ContactObj.AccountId != Null)
                    accountIdSet.add(ContactObj.AccountId);
            }
            contactList = [Select Id,AccountId,Email From Contact Where AccountId In :accountIdSet Limit 50000];
            for(Contact ContactObj2: Trigger.new){
                Boolean isDuplicateEmail = false;
                for(Contact oldContactObj: contactList){
                    if(ContactObj2.AccountId == oldContactObj.AccountId && ContactObj2.Email == oldContactObj.Email){
                        isDuplicateEmail = true;
                    }
                }
                if(isDuplicateEmail){
                    ContactObj2.adderror('Duplicate Email, Please Use Other Email') ;
                }
            }
        }  
    }
    
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
Sonu06Sonu06
Hi Deepali , thank you for your response, You have written one for loop under the another for loop, don't you think so i may hit governer limit?? , if 
you can rewrite this code using Map, it will be very helpful me.
Thank you
Deepali KulshresthaDeepali Kulshrestha
Hi Sonal,

Greetings to you!

I have updated this trigger as you asked using Map. I have created a map of accountId Vs List Of previous contact email. Please mark as Best Answer to help others too.
 
Trigger ContactTrigger on Contact (before insert,before update) {
    if(Trigger.isbefore){
        if(Trigger.isinsert ||Trigger.isupdate){
            Set<Id> accountIdSet = new Set<Id>();
            List<Contact> contactList = new List<Contact>();
            Map<Id,List<String>> accountIdVsConEmail = new Map<Id,List<String>>();
            for(Contact ContactObj: Trigger.new) {
                if(ContactObj.AccountId != Null)
                    accountIdSet.add(ContactObj.AccountId);
            }
            contactList = [Select Id,AccountId,Email From Contact Where AccountId In :accountIdSet Limit 50000];
            
            for(Contact Obj: contactList){
                if(!accountIdVsConEmail.containsKey(Obj.AccountId)){
                    accountIdVsConEmail.put(Obj.AccountId, new List<String>());
                }
                accountIdVsConEmail.get(Obj.AccountId).add(Obj.Email);
            }
            
            
            for(Contact ContactObj2: Trigger.new){
                if(accountIdVsConEmail.containsKey(ContactObj2.AccountId)){
                    if(accountIdVsConEmail.get(ContactObj2.AccountId).size() > 0 && accountIdVsConEmail.get(ContactObj2.AccountId).contains(ContactObj2.Email)){
                        ContactObj2.adderror('Duplicate Email, Please Use Other Email') ;
                    }
                }
            }
        }  
    }
    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
This was selected as the best answer