0

I am pretty new to the APEX/Triggers so any help is appreciated;

I have a custom field on the Contact object called 'Email Domain'. I am adding new Contacts that don't have Account Name.

What I would like to do is, when the Email Domain on one Contact matches another, the Account Name should match as well.

For Example I have:

Name:John Wayne
Account Name: Wayne Enterprise Email Domain: abcinc.com

IF Name: Peter Don Account Name: (blank) Email Domain: abcinc.com

Then for Contact Peter Don then Account Name = Wayne Enterprise.

1

This can get you started, then once you have a more specific question we can help you better

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_HelloWorld.htm

Trailhead also has many tutorials

https://developer.salesforce.com/trailhead/en/apex_triggers/apex_triggers_intro

0

This piece of code works fine for me. You can Use this code :

trigger populateAccountNameOnEmaildomain on Contact (after insert,after update) {
    map<string,Id> map1 = new map<string,Id>();
         List<Contact> contList3 = new List<Contact>();
         Set<String> emailList = new Set<String>();
         for(Contact con : trigger.new) {
             if(con.Emaildomain__c != null && con.AccountId == null) {
                 contList3.add(con);
                 emailList.add(con.Emaildomain__c);
             }
         }
        List<Contact> contList = [SELECT Emaildomain__c,AccountId FROM Contact WHERE Emaildomain__c IN : emailList AND AccountId != NULL];
        for(Contact con:contList){
            map1.put(con.Emaildomain__c,con.AccountId);
    }
         List<Contact> newConList = new List<Contact>();
            for(Contact con:contList3){
                if(map1.get(con.Emaildomain__c) != NULL) {
                    Contact con1 = new Contact(Id = con.Id);
                    con1.AccountId= map1.get(con.Emaildomain__c);
                    newConList.add(con1);
                }

            }                   
            update newConList;
}

Regards,

Ajay

  • This works well on the Sandbox, but in Production i am getting the following error when I do a Insert using Data Loader or Import Wizard populateAccountNameOnEmaildomain: execution of AfterInsert 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. – B.Wayne Apr 13 '16 at 21:38
  • Hi Wayne, I think you are trying to update too much Accounts (more than 100000), so the error is coming. To avoid this error you have to use Batch Class. – Ajay Dubedi Apr 20 '16 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.