+ Start a Discussion
john8407john8407 

Trigger to update contact lookup field on account when contact is created with a type.

We have two contact lookup fields on the account, one is called finance contact 1 and other is finance contact 2.  When a user creates an account, they have to create 2 contacts.  After creating the contacts, they have to edit the account and do a lookup to put one of the contacts in finance contact 1 and the other in finance contact 2.  I added a contact type field on the contact object with values of finance contact 1 and finance contact 2.  Is there a way that when a user creates a contact and selects the type of finance contact 1, it will automatically insert that contact name into the Finance Contact 1 lookup field on the account.  And same for the 2nd finance contact?

DharmeshDharmesh

you can do something like this.

 

trigger UpdateAccount on Contact (before update) {
    
    List<Account> accs= [select id,finance_contact__c from Account];
    for(Contact c : Trigger.new){
        System.debug('-------name----:'+c.Department);
        if(c.Department=='Finance' ){
         System.debug('-------');
         for(Account acc : accs){
             acc.finance_contact__c = c.id;
         }
        }
    }
    update accs;
}

 

john8407john8407

I'm trying to use this but get an error when updating the contact.  Any Ideas?

 

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)

Trigger.UpdateAccount: line 3, column 5