+ Start a Discussion
luck.sfdcluck.sfdc 

trigger

i have a custom field in account object called count.it should display the number contacts related to that account....like if we added one contact to the account count should be increased or if contact is deleted contact should be decreased..
how to write a trigger to this...

 

 

Thanks in advance

 

Best Answer chosen by Admin (Salesforce Developers) 
Shiv ShankarShiv Shankar

Just put your custom field(Which is on Account Object to count No Of Contact) name on place of ContactCount__c

trigger countContactUnderAccount on contact (after insert, after update, after Delete){
    Set<Id> accountIds = new Set<Id>();
    if(Trigger.isInsert || Trigger.isUpdate){
        for(Contact con : trigger.new){
            accountIds.add(con.AccountId);
        }
    } else{
        for(Contact con : trigger.old){
            accountIds.add(con.AccountId);
        }
    }    
    Map<Id, Integer> accountIdToTotalContact = new Map<Id, Integer>();    
    AggregateResult[] groupResults = [select AccountId, Count(id) totalContacts FROM CONTACT WHERE AccountId in : accountIds GROUP BY AccountId];
    List<Account> accountListToUpdate = new List<Account>();
    system.debug('AggregateResult When No Record of Contact for Account : --'+groupResults);
    
    for(AggregateResult agrRes : groupResults){
        accountIdToTotalContact.put((Id)agrRes.get('AccountId'),(Integer) agrRes.get('totalContacts'));
    }
    
    for(Account act : [select id, ContactCount__c From Account WHERE id in : accountIds ]){
        act.ContactCount__c = accountIdToTotalContact.get(act.id);
        accountListToUpdate.add(act);
    }
    update accountListToUpdate;

}

 

All Answers

Vinit_KumarVinit_Kumar

Luck,

 

Basically you are looking for Roll up summary Trigger,please go through the below link for the same :-

 

http://www.anthonyvictorio.com/salesforce/roll-up-summary-trigger/

Shiv ShankarShiv Shankar

Just put your custom field(Which is on Account Object to count No Of Contact) name on place of ContactCount__c

trigger countContactUnderAccount on contact (after insert, after update, after Delete){
    Set<Id> accountIds = new Set<Id>();
    if(Trigger.isInsert || Trigger.isUpdate){
        for(Contact con : trigger.new){
            accountIds.add(con.AccountId);
        }
    } else{
        for(Contact con : trigger.old){
            accountIds.add(con.AccountId);
        }
    }    
    Map<Id, Integer> accountIdToTotalContact = new Map<Id, Integer>();    
    AggregateResult[] groupResults = [select AccountId, Count(id) totalContacts FROM CONTACT WHERE AccountId in : accountIds GROUP BY AccountId];
    List<Account> accountListToUpdate = new List<Account>();
    system.debug('AggregateResult When No Record of Contact for Account : --'+groupResults);
    
    for(AggregateResult agrRes : groupResults){
        accountIdToTotalContact.put((Id)agrRes.get('AccountId'),(Integer) agrRes.get('totalContacts'));
    }
    
    for(Account act : [select id, ContactCount__c From Account WHERE id in : accountIds ]){
        act.ContactCount__c = accountIdToTotalContact.get(act.id);
        accountListToUpdate.add(act);
    }
    update accountListToUpdate;

}

 

This was selected as the best answer
luck.sfdcluck.sfdc

Thank you so much ...

if u put some comment lines that makes so much helpful to us...

anyway that's good solution .once again thanks brother

luck.sfdcluck.sfdc

can u provide test class for this