function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TanyrilTanyril 

Help with a test class

I've put together a trigger to roll up some asset amounts from contacts to our accounts, however, I can't figure out for the life of me how I would write a test class in this situation, can anyone help me? I'm no apex pro.

 

Also- the fields being summed and sent to the account object are actually rollup fields from a child object of contact. When these fields are updated will they cause the trigger to fire? There's one of these triggers for each of our different asset amounts (8 in total).

 

Thanks for your help!

 

trigger SumBalanced on Contact (after insert,after update,after delete)
{
    Set<Id> accountIdset = new Set<Id>();
   
    for (Contact con:trigger.new)
    {
       accountIdset.add(con.AccountId);
    }
    Map<Id,Account> AccountMap = new Map<Id,Account>([Select Id,Balanced__c from Account where Id in :accountIdset]);
    for(AggregateResult res : [SELECT AccountId, SUM(Balanced__c) cnt FROM Contact where AccountId in : accountIdset GROUP BY ROLLUP(AccountId)]){
        if(res.get('AccountId') <> null){
            AccountMap.get((ID)res.get('AccountId')).Balanced__c = (Decimal)res.get('cnt');
        }
    }
    update AccountMap.values();
	}

 

Best Answer chosen by Admin (Salesforce Developers) 
TanyrilTanyril

Never mind, I got it. Thanks!