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

Adding new Contacts based on custom fields

I have a bunch of new Contacts without an Account name field.  However, I have a custom field on the Contact Level called email Domain; how can I create a WF or trigger, where if the custom field Email Domain matches then the Account Name should be the same as well.

For Example
I have: 

Name:Bruce Wayne                                       
Account: Wayne Enterprise
Email Domain:

Name: Clark Kent
Account: (blank)
Email Domain:

Then for Contact Clark Kent then Account = Wayne Enterprise.


Try to use this.
trigger updateAccount on Contact (before insert,before update) {
    Set<String> emailDomain = new Set<String>();
    Map<String,Id> accountMap = new Map<String,Id>();
    for(Contact con :{
        if(Con.AccountId == null && con.Email_Domain__c != null){
    for(contact ac : [Select id,Name,AccountId,Email_Domain__c from Contact where Email_Domain__c in : emailDomain]){
    for(Contact con : Trigger.New){
        if((Con.AccountId == null) && (Con.Email_Domain__c != null)){
            Con.AccountId = accountMap.get(Con.Email_Domain__c);

Onkar Kumar
This code works in the Sandbox, but in Production, when I upload Contact through the Import Data Wizard or Data Loader, I am getting this error:

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY:updateAccount: execution of BeforeInsert

caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact about custom indexing.
Even if a field is indexed a filter might still not be selective when:
1. The filter value includes null (for instance binding with a list that contains null)
2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times)