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
Rafi RafiRafi Rafi 

Associating contact with account

Hi,
I have a requirement I don't know how to get it, Q)ABC containers require the ability to automatically associate a contact created in their salesforce instance with the respective account based on the email domain specified in the primary email address of the contact. The association should happen in real-time as soon as a contact record is created within the system. 1)Develop the necessary piece of apex code to implement a solution 2)necessary test code(with 90% of code coverage)
Thanks in advance,
Rafi
Best Answer chosen by Rafi Rafi
ANUTEJANUTEJ (Salesforce Developers) 
Hi Rafi,

Please try the below snippet.
 
trigger auto_Account_assignment on Contact(before insert) {
    Set<String> emailDomains = new Set<String>();

    for (Contact l : Trigger.New) {
        if (l.Email == null) {
            continue;
        }   

        emailDomains.add(l.Email.split('@').get(1));
    }

    emailDomains.remove(null);
           
    if (!emailDomains.isEmpty()) {
        Map<String, Account> emailDomainToAccountMap = new Map<String, Account>();

        for (Account a : [ 
            select Name,
                Id
            from Account
            where Name in :emailDomains
        ]) {
            emailDomainToAccountMap.put(a.Name, a);
        }   

        for (Contact l : Trigger.New) {
            if (l.Email == null) {
                continue;
            }

            String domain = l.Email.split('@').get(1);

            if (!emailDomainToAccountMap.containsKey(domain)) {
                continue;
            }

            l.AccountId= emailDomainToAccountMap.get(domain).Id;
        }
    }
}
 
@isTest
private class AccountTestClass {
 static testMethod void validateAId() {
Account a= new Account();
a.name='test';
//I am adding only the name but please add all the required fields so that above account record is properly inserted
insert a;
Contact tempc = new contact();
tempc.LastName='TestContact';
tempc.email='primary@test.com';
insert tempc;
Id AI= a.id;
Contact check=[select id,accountid from contact where id in :AI];
system.assert(check.AccountId,AI);
 }
}

Please be noted that the above is a sample snippet and you need to make changes accordingly.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Rafi,

Please try the below snippet.
 
trigger auto_Account_assignment on Contact(before insert) {
    Set<String> emailDomains = new Set<String>();

    for (Contact l : Trigger.New) {
        if (l.Email == null) {
            continue;
        }   

        emailDomains.add(l.Email.split('@').get(1));
    }

    emailDomains.remove(null);
           
    if (!emailDomains.isEmpty()) {
        Map<String, Account> emailDomainToAccountMap = new Map<String, Account>();

        for (Account a : [ 
            select Name,
                Id
            from Account
            where Name in :emailDomains
        ]) {
            emailDomainToAccountMap.put(a.Name, a);
        }   

        for (Contact l : Trigger.New) {
            if (l.Email == null) {
                continue;
            }

            String domain = l.Email.split('@').get(1);

            if (!emailDomainToAccountMap.containsKey(domain)) {
                continue;
            }

            l.AccountId= emailDomainToAccountMap.get(domain).Id;
        }
    }
}
 
@isTest
private class AccountTestClass {
 static testMethod void validateAId() {
Account a= new Account();
a.name='test';
//I am adding only the name but please add all the required fields so that above account record is properly inserted
insert a;
Contact tempc = new contact();
tempc.LastName='TestContact';
tempc.email='primary@test.com';
insert tempc;
Id AI= a.id;
Contact check=[select id,accountid from contact where id in :AI];
system.assert(check.AccountId,AI);
 }
}

Please be noted that the above is a sample snippet and you need to make changes accordingly.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
James Taylor 63James Taylor 63
This is a excellent , would you be involved in doing an interview about just how you designed it? If so e-mail me! (https://paintitems.com/)