0

I have a requirement on Trigger and scenario is given below

I created a field called amount on Account and contact objects. Later i created a record for account called AAA where amount value is nothing. Under this account record AAA, i created two contacts records named CCC1 and CCC2 with amount value is some x1 and x2 respectively.

After creating this contacts under this account record, i need to update the record AAA amount value with sum of x1 and x2. Like this, if i create one more contact record CCC3(Let say) under AAA, then amount value of account record should be updated with sum of x1+x2+x3. Likewise if i delete one contact record from AAA, then amount for AAA is should be sum of remaining contacts and vice versa for if i update contacts amount values.

To do this, I developed the following trigger.

Trigger Amount_Total_on_Contact on Contact (after insert) {

    List<Account> acc1 = new List<Account>();
List<Account> acc = [select Amount__c,(select AccountId from contacts where AccountId in:Trigger.OldMap.keySet())from account];
AggregateResult results=[select sum(Amount__c)total from Contact where AccountId in:Trigger.newMap.keySet()];
    for(Contact cc: Trigger.New) {
        for(Account a: acc) {
            a.Amount__c = (Decimal)results.get('total');
            acc1.add(a);
        }
    }
    update acc1;
}

But my problem in this trigger is, amount value of account record is not reflecting.

Note: Its is not mandatory for using after insert event. I just took it for checking. we can add other events like update and delete.

Please help me out from this problem.

1

2 Answers 2

1

Well, in both the SOQL in where clause you are matching AccountId with contact ids(Trigger.oldMap.keySet is contact ids, since the trigger is on Contact object).

You should do the following.

First get all accountIds from the contacts.

for(Contact c : Trigger.New)
{
accountIds.add(c.AccountId);
}

Next, query on Contact Object and fetch result by grouping with Account Ids.

for(AggregateResult agr : [select AccountId, Sum(NumberField__c) TotalSum from Contact where accountId IN :accountIds group by AccountId])
{
accountListToUpdate.add(new Account(Id=agr.get('AccountId'), NumberField__c=agr.get('TotalSum')));
}

You might have to typecast the AggregateResult fields based on your field type.

1
0

There is an option to use dlrs https://github.com/afawcett/declarative-lookup-rollup-summaries

If not then try with below code.

first collect all the Account ids and then query on contact

Trigger Amount_Total_on_Contact on Contact (after insert) {

    List<Account> lstAccount = new List<Account>();
    set<Id> setAccountIds = new set<Id>();

    //first iterate over the trigger.new and collect all the account Ids 
    for(contact objContact: Trigger.New)
    {
        if(objContact.AccountId != null)
        {
            setAccountIds.add(objContact.AccountId);
        }
    }
    //now query on contact and get the sum of Amount field and assign to Account list
    for(AggregateResult agResult: [SELECT sum(Amount__c)total, AccountId 
                                     FROM Contact WHERE AccountId IN:setAccountIds 
                                     GROUP BY AccountId])
    {
        lstAccount.add(new Account(Id = (Id) agResult.get('AccountId'), 
                                    Amount__c = (Decimal) agResult.get('total')));
    }
    //update your list
    update lstAccount;
}
2
  • Its hard to make two acceptable answers Thank You ratan
    – KS Kumaar
    Sep 27, 2016 at 9:02
  • 1
    @KSKumaar no problem
    – Ratan Paul
    Sep 27, 2016 at 9:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .