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
ruchika Nayyarruchika Nayyar 

is this code is right??

public class contactpopulate {
    public void onbeforeinsert(list<contact>TriggerNew)
        {
          onduplicateEmailandphone(TriggerNew);
        }
   void onduplicateEmailandphone(list<Contact>TriggerNew)
   {
        set<string> Set1= New set<string>();
        set<string> Set2= New set<string>();
    
        For(contact con:TriggerNew)
        {
            if(con.Phone!=null)
            {
                Set1.add(con.Phone);
            }
                if(con.Email!=null)
                {
                    Set2.add(con.Email);
                }
            }
        map<id,contact> map1= New map<id,contact>();
        map<id,contact> map2= New map<id,contact>();
        for(contact obj:[select id,name,Phone,Email from contact where Phone in: Set1 OR Email in: Set2])
        {
            map1.put(obj.phone,obj);
            map2.put(obj.Email,obj);
            
        }
    for(contact con:TriggerNew)
    {
        if(con.Phone !=null && map1.get(con.Phone)!=null)
           {
               con.adderror('duplicate phone found please enter new phone');
               
           }
        if(con.Email!=null&& map2.get(con.Email)!=null)
           {
               con.adderror('duplicate email found please enter new email');
           }
    }
    }
}
hitesh90hitesh90
Hello Ruchika,

There is one issue in your code. which is in map. you have to take map<String, Contact> instead of map<Id, Contact>.because Email and Phone of contact is type of String not an ID. see below updated code.

Apex Class:
public class contactpopulate {
    public void onbeforeinsert(list<contact>TriggerNew){
        onduplicateEmailandphone(TriggerNew);
    }
    void onduplicateEmailandphone(list<Contact>TriggerNew){
        set<string> Set1= New set<string>();
        set<string> Set2= New set<string>();
        For(contact con:TriggerNew){
            if(con.Phone!=null){
                Set1.add(con.Phone);
            }
            if(con.Email!=null){
                Set2.add(con.Email);
            }
        }
        map<String,contact> map1= New map<String,contact>();
        map<String,contact> map2= New map<String,contact>();
        for(contact obj:[select id,name,Phone,Email from contact where Phone in: Set1 OR Email in: Set2]){
            map1.put(obj.phone,obj);
            map2.put(obj.Email,obj);            
        }
        for(contact con:TriggerNew){
            if(con.Phone !=null && map1.get(con.Phone)!=null){
                con.adderror('duplicate phone found please enter new phone');            
            }
            if(con.Email!=null&& map2.get(con.Email)!=null){
                con.adderror('duplicate email found please enter new email');
            }
        }
    }
}

Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
ruchika Nayyarruchika Nayyar
sir

the below code is save
but the problem is while making new record of contact it shows  the error
Review all error messages below to correct your data.
Apex trigger myruchika.CopyfieldTrigger caused an unexpected exception, contact your administrator: myruchika.CopyfieldTrigger: execution of BeforeInsert caused by: line 2, column 42: trigger body is invalid and failed recompilation: Invalid type: CopyfieldTriggerhandler1
hitesh90hitesh90
You have to recompile your trigger code. Edit your trigger and Save again.
Ajay mishraAjay mishra
Hi Ruchika,

As the error shared by you. In error it is specificaly mentioned  CopyfieldTriggerhandler1 Class is invalid.

Which means it is not in your Org.

Remove the CopyfieldTriggerhandler1 from your trigger it will run properly.

Thanks,
Ajay Mishra
Email: mishraajay.m1@gmail.com
ruchika Nayyarruchika Nayyar
thanks ajay mishra and hitesh
i have completed the code.