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
S_BatmanS_Batman 

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: abcinc.com

IF
Name: Clark Kent
Account: (blank)
Email Domain: abcinc.com

Then for Contact Clark Kent then Account = Wayne Enterprise.

 
~Onkar~Onkar
Hi 

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 : trigger.new){
        if(Con.AccountId == null && con.Email_Domain__c != null){
           emailDomain.add(con.Email_Domain__c);
        }
    }
    
    for(contact ac : [Select id,Name,AccountId,Email_Domain__c from Contact where Email_Domain__c in : emailDomain]){
        accountMap.put(ac.Email_Domain__c,ac.AccountId);
    }
    
    for(Contact con : Trigger.New){
        if((Con.AccountId == null) && (Con.Email_Domain__c != null)){
            Con.AccountId = accountMap.get(Con.Email_Domain__c);
        }
    }
}

~Thanks,
Onkar Kumar
S_BatmanS_Batman
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 salesforce.com 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)

():--