+ Start a Discussion
RP SinghRP Singh 

Create a Formula Field named Domain on Contact Object

1. Create a Formula Field named Domain on Contact Object, This should give me the Domain from Email Field on Contact Object. For Example: - If I save the Contact with Email as testuser@gmail.com, then formula field of Domain should have value “gmail”
2. Add a text Field at Account Level named as Contact Unique Domains
3. Create a trigger on Contact, Which handles Insert / Update / Delete / Undelete scenario. Trigger should get the Account Id from the Contact and then read all the related contacts from that Account and then Store the unique domains to the Account Level. For Example If I have below 3 contacts
Email - testuser1@gmail.com and Domain - gmail
Email - testuser2@yahoo.com and Domain as yahoo
Email - testuser3@gmail.com and Domain as gmail
So there are two unique domain gmail and yahoo, hence my Account text field will be populated with value gmail;yahoo
Best Answer chosen by RP Singh
Prakhar Saxena 19Prakhar Saxena 19
Hi RP,

Create a formula field named 'Domain' on the Contact object and use the following formula:
 
MID( Email , FIND("@", Email )+1 ,  FIND(".", Email, FIND("@", Email)) - FIND("@", Email)-1 )

This will extract the domain from the Email field and store it in the Domain field.

Next, write the following trigger on the Contact object:
 
trigger AddDomain on Contact (after insert, after update, after delete, after undelete) {

    AddDomainHandler handler = new AddDomainHandler();
    
    if(Trigger.isAfter){
        handler.addDomain(Trigger.new);
    }
}



public class AddDomainHandler {
    
    public void addDomain(List<Contact> contacts){
        
        String contactUniqueDomains = '';
        List<String> listOfContactUniqueDomains = new List<String>();
        
        if(contacts[0].AccountId!=null){
            List<Contact> listOfContacts = [SELECT Id, Domain__c, AccountId FROM Contact where AccountId =: contacts[0].AccountId]; 
            
            for(Contact c : listOfContacts){
                if(!listOfContactUniqueDomains.contains(c.Domain__c)){
                    contactUniqueDomains += c.Domain__c + ';'; 
                }
                listOfContactUniqueDomains.add(c.Domain__c);
            }
            
            contactUniqueDomains = contactUniqueDomains.removeEnd(';');
            
            Account acc = new Account(Id = listOfContacts[0].AccountId,
                                      Contact_Unique_Domains__c = contactUniqueDomains);
            update acc;
        }
    }
}

The trigger queries all the contacts with fields Id, Domain and AccountId whose AccountId matches the triggerd Contact. It then extracts the Domain of each such contact and adds it to a string contactUniqueDomains.

The Account's Contact_Unique_Domains__c field is updated with the contactUniqueDomains value.

(In case, you are triggering multiple contacts at a time, just add a for loop and iterate through each and every Contact triggered instead of using contacts[0] ).

Regards,
Prakhar

All Answers

Prakhar Saxena 19Prakhar Saxena 19
Hi RP,

Create a formula field named 'Domain' on the Contact object and use the following formula:
 
MID( Email , FIND("@", Email )+1 ,  FIND(".", Email, FIND("@", Email)) - FIND("@", Email)-1 )

This will extract the domain from the Email field and store it in the Domain field.

Next, write the following trigger on the Contact object:
 
trigger AddDomain on Contact (after insert, after update, after delete, after undelete) {

    AddDomainHandler handler = new AddDomainHandler();
    
    if(Trigger.isAfter){
        handler.addDomain(Trigger.new);
    }
}



public class AddDomainHandler {
    
    public void addDomain(List<Contact> contacts){
        
        String contactUniqueDomains = '';
        List<String> listOfContactUniqueDomains = new List<String>();
        
        if(contacts[0].AccountId!=null){
            List<Contact> listOfContacts = [SELECT Id, Domain__c, AccountId FROM Contact where AccountId =: contacts[0].AccountId]; 
            
            for(Contact c : listOfContacts){
                if(!listOfContactUniqueDomains.contains(c.Domain__c)){
                    contactUniqueDomains += c.Domain__c + ';'; 
                }
                listOfContactUniqueDomains.add(c.Domain__c);
            }
            
            contactUniqueDomains = contactUniqueDomains.removeEnd(';');
            
            Account acc = new Account(Id = listOfContacts[0].AccountId,
                                      Contact_Unique_Domains__c = contactUniqueDomains);
            update acc;
        }
    }
}

The trigger queries all the contacts with fields Id, Domain and AccountId whose AccountId matches the triggerd Contact. It then extracts the Domain of each such contact and adds it to a string contactUniqueDomains.

The Account's Contact_Unique_Domains__c field is updated with the contactUniqueDomains value.

(In case, you are triggering multiple contacts at a time, just add a for loop and iterate through each and every Contact triggered instead of using contacts[0] ).

Regards,
Prakhar
This was selected as the best answer
RP SinghRP Singh
Hey thank you so much...
Prakhar Saxena 19Prakhar Saxena 19
Hey, if this helped you, please mark it as the Best Answer and give it a like.

Thanks.
RP SinghRP Singh
Sure it's good. I have another question too.