0

I'm using the following trigger in order to assign contact to the relevant account based on the email domain. This trigger affects every contact which is created in the system.

I'd like to add a condition in which only contacts which were created automatically (There is a custom field named "AutoCreated" equals True) will be assign to the relevant account.

   trigger AssociateContact on Contact (before insert) 
    {
        List<String> contactEmaildomains = new List<String>();
        for(Contact contact:Trigger.new)

       {
            contactEmaildomains.add('%'+contact.Domain__c+'%');
        }
        System.debug(contactEmaildomains);
        List<Account> accounts = [SELECT 
                Id, Domain__c 
             FROM 
                Account
             WHERE 
                Domain__c LIKE :contactEmaildomains];
        System.debug(accounts);

        Map<String, Id> domains = new Map<String, Id>();

        for(Account record: accounts) 
        {
            for(String dom:record.Domain__c.Split(','))
                domains.put(dom, record.Id);
        }
        for(Contact record: Trigger.new) 
        {
            if(domains.get(record.Domain__c) != null) 
            {
                record.AccountId = domains.get(record.Domain__c);
            } 
    }

Can someone help me with this?

Thanks in advance!

One more issue with this trigger - the code coverage is 0% even though I have the following test class :

@isTest
private class AssociateContactTest 
{
    @isTest static void TestOne()
    {
        List<Account> accList=new List<Account>();
        Integer count=500;
        for(Integer i=1;i<=count;i++)
        {
            accList.add(new Account(Name='My Account '+i,Domain__c='synebo'+i+'.com,gmail'+i+'.com'));
        }
        insert accList;
        List<Contact> contList=new List<Contact>();
        for(Integer i=1;i<=count;i++)
        {
            contList.add(new Contact(Email='contact@gmail'+i+'.com',LastName='Contacter '+i));
        }
        insert contList;
        List<Contact> res=[Select Id,AccountId From Contact Where Id IN :contList ORDER BY Id];
        for(Integer i=0;i<count;i++)
        {

            System.assertEquals(accList[i].Id, res[i].AccountId);
        }        
    }
}

1 Answer 1

4

In the first for loop, you can put an if condition:

 for(Contact contact:Trigger.new)

           {
                if(contact.AutoCreated)
                  contactEmaildomains.add('%'+contact.Domain__c+'%');
            }

For test class, Change the account to:

 accList.add(new Account(Name='My Account '+i,Domain__c='gmail'+i+'.com'));

And see if it works.

5
  • @Shaliesh Would you be able to help me with the Test Class? The issue description was added to the question above. Jan 20, 2016 at 12:35
  • Please see the updated answer. @LihodRachmilevitch Jan 20, 2016 at 12:41
  • The Domain__c field on the contact object is a formula field (MID(Email,FIND('@', Email, 1)+1, (LEN(Email) - FIND('@', Email, 1)+1))). Therefore I received an error "Field is not writeable: Contact.Domain__c" Jan 20, 2016 at 12:57
  • I have updated the answer. Please check. Jan 20, 2016 at 13:19
  • still 0% coverage - weird :( Jan 20, 2016 at 13:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .