Find your content:

Search form

You are here

Auto population of a field in trigger

 
Share

I want a auto-populate field(No.Of Contacts) on the Account Object, Like when i click on a specific account it should display no.of contacts registered under a single Account. I am very new on Apex and trying to have some clarity so that I can clear my interviews?

Please help

Update: I've written the trigger:

trigger ContactsOnAccount on Contact(after insert, after delete, after undelete, after update) {

    Set aId = new Set();

    if (Trigger.isInsert || Trigger.isUndelete) {

        for (Contact opp: Trigger.New) {
            aId.add(opp.AccountId);
        }

        List acc = [select id, No_of_Contacts_in_SFDC__c from Account where Id in : aId];
        List con = [select id from contact where AccountId in : aId];

        for (Account a: acc) {
            a.No_of_Contacts_in_SFDC__c = con.size();
        }

        update acc;
    }

    if (Trigger.isDelete) {
        for (Contact opp: Trigger.old) {

            aId.add(opp.AccountId);
        }

        List acc = [select id, No_of_Contacts_in_SFDC__c from Account where Id in : aId];
        List con = [select id from contact where AccountId in : aId];

        for (Account a: acc) {

            a.No_of_Contacts_in_SFDC__c = con.size();
        }
        update acc;
    }

    if (Trigger.isUpdate) {

        Set OldAId = new Set();

        for (Contact opp: Trigger.new) {
            if (opp.AccountId != Trigger.oldMap.get(opp.id).AccountId || opp.Primary_Contact__c != Trigger.oldMap.get(opp.id).Primary_Contact__c) aId.add(opp.AccountId);

            OldAId.add(Trigger.oldMap.get(opp.id).AccountId);
        }
        if (!aId.isEmpty()) { //for new Accounts List acc = [select id,No_of_Contacts_in_SFDC__c from Account where Id in:aId]; //For New Account Contacts

            List con = [select id from contact where AccountId in : aId]; /* This is For Old Contacts Count */ //for Old Accounts
            List Oldacc = [select id, No_of_Contacts_in_SFDC__c from Account where Id in : OldAId]; //For Old Account Contacts
            List OldCon = [select id from contact where AccountId in : OldAId];

            //For New Accounts for(Account a : acc){ a.No_of_Contacts_in_SFDC__c=con.size(); } update acc; //For Old Accounts for(Account a : Oldacc){ a.No_of_Contacts_in_SFDC__c=OldCon.size(); }update Oldacc;
        }
    }
}

Attribution to: shivani senapati

Possible Suggestion/Solution #1

It's not hard, but there are a few catches, this makes it good to think about this. Writing code always starts with thinking about what you really want, and what is the best way to get it.

  1. You want to always display on account, how many contacts there are. You'll need a field to do so.
  2. This changes when contacts are created/deleted/re-assigned, so you'll need a trigger on contact, for all dml actions (update,insert,delete,undelete)
  3. no matter what happens, we only want to know how many contacts an account has, this is a single question, and can be answered with a single logical implementation. Don't make it complex by creating much "+1/-1"-if-then-etc logic.
  4. In your code, you'll want to start iterating your contacts, are they created/deleted/undeleted or did their account change ? we'll need to know to what account they belonged or belong now, because for those accounts we'll need to re-count the nr of contacts.
  5. For those accounts, you'll want to do: count(id) from contact grouped by accountid where accountid is in the list of step 4.
  6. after that it's only a matter of querying for those accounts, updating the nr of contacts field with the count result from the aggregated result, and then to update your list of accounts back to the database.

I've tried to answer this without giving you any code, because you'll learn the most by trying to put that together yourself. Do so, and if you get stuck, you can update your question (edit) with the progress you've made, and we'll help you where you get stuck.

Update: In responce to your trigger code itself:

for (Contact opp: Trigger.New) {
            aId.add(opp.AccountId);
}

List acc = [select id, No_of_Contacts_in_SFDC__c from Account where Id in : aId];
List con = [select id from contact where AccountId in : aId];

for (Account a: acc) {
            a.No_of_Contacts_in_SFDC__c = con.size();
}

Do you spot the problem in this code ? You're starting of right, queying all the contacts for all the accounts.

To truly know how many contacts there are PER account, you have to querty them in a group-by soql statement (grouping them by account, and parse the Aggregated result), or group them in apex (for instance in a map> where the map key would be the account id).


Attribution to: Samuel De Rycke
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34148

My Block Status

My Block Content