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
SilverSurferSilverSurfer 

How to compare email Ids(field type is Email) in salesforce?

I want to compare email ids from a single list.

 

Basically, I am aiming to find duplicate email ids from a list. So I am running 2 for loops on a single list and trying to compare the email fields using the loop variables.

 

P.S. The email is an Email field type and not a string. Please reply asap. Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

While iterating forloop

I compre first and second element, once elmet pass the criteria, I remove it from original list

 

here is code

 

List <ContactCustom2__c> emailList2 = new List<ContactCustom2__c>();
List <ContactCustom__c> emailList = [select Name, email__c from ContactCustom__c];
if( emailList!=null &&  emailList.size()>0)
  {
     for(integer i=0;i<emailList.size();i++)
        {
              ContactCustom__c cObj=emailList.get(i);
               for(integer j=1;j<emailList.size();j++)    
                  {
                    ContactCustom__c dObj=emailList.get(j); 
                    if(cObj.email __c.equals(dObj.email __c))
                    {
                     emailList2.add(New ContactCustom2__c(Name=cObj.Name,email__c=cObj.email__c));
				emailList.remove(i);
                    }
                   }
         }
         insert  emailList2;
}

 

Hop this will help you

 

If it works for you mark it as accepted

Thanks,  

Bala

All Answers

b-Forceb-Force

you can use Set collection of Apex

Set<Email>  sEmail=new Set<Email>();

 

it will autometically store unique  entries only

 

else post your code here.

 

 

Hope this will help you,

 

Thanks,

Bala

SilverSurferSilverSurfer

Hi Bala,

 

Thanks for the quick reply. Following is the code where I have 2 objects ContactCustom and ContactCustom2 where I am iterating through the list of ContactCustom and want to populate the other object(ContactCustom2) with the duplicate records from the first.

 

The earlier suggestion for using sets is also useful for me in some other scenario.

 

List <ContactCustom2__c> emailList2 = new List<ContactCustom2__c>();
        List <ContactCustom__c> emailList = [select Name, email__c from ContactCustom__c];
        if( emailList!=null &&  emailList.size()>0)
            {
               for(integer i=0;i<emailList.size();i++)
                   {
                       ContactCustom__c cObj=emailList.get(i);
                        for(integer j=0;j<emailList.size();j++)    
                           {
                              ContactCustom__c dObj=emailList.get(j);                               
                              if(cObj.email __c.equals(dObj.email __c))
                                   {
                                        emailList2.add(New ContactCustom2__c(Name=cObj.Name,email__c=cObj.email__c));
                                   }
             
                           }
                  }
                  insert  emailList2;
            }   

 

 

The purpose is to find the duplicate email ids and display to the user for further processing. This is a requirement and also note  that I cannot write a trigger.

b-Forceb-Force

While iterating forloop

I compre first and second element, once elmet pass the criteria, I remove it from original list

 

here is code

 

List <ContactCustom2__c> emailList2 = new List<ContactCustom2__c>();
List <ContactCustom__c> emailList = [select Name, email__c from ContactCustom__c];
if( emailList!=null &&  emailList.size()>0)
  {
     for(integer i=0;i<emailList.size();i++)
        {
              ContactCustom__c cObj=emailList.get(i);
               for(integer j=1;j<emailList.size();j++)    
                  {
                    ContactCustom__c dObj=emailList.get(j); 
                    if(cObj.email __c.equals(dObj.email __c))
                    {
                     emailList2.add(New ContactCustom2__c(Name=cObj.Name,email__c=cObj.email__c));
				emailList.remove(i);
                    }
                   }
         }
         insert  emailList2;
}

 

Hop this will help you

 

If it works for you mark it as accepted

Thanks,  

Bala

This was selected as the best answer
SilverSurferSilverSurfer

Hi Balasaheb,

 

Thanks for the reply. I have got the solution. Just to keep you noted with regards to your earlier reply,

Set<Email> sEmail = new Set<Email>();

is not correct as Email is not identifieed as a datatype.

 

Glad to know that you too are from Pune as me. Thanks