You need to sign in to do that
Don't have an account?
Chad Ritchie
WritingTestClass
Pretty new to writing test classes, was looking for help for this trigger:
trigger SumPositions on Investor__c (before insert, before update, before delete) {
for (Investor__c record : Trigger.new) {
record.Sum_of_Positions__c = null;
}
for(AggregateResult result: [SELECT SEI_Investor_Id__c, SUM(Position__c.Balance__c)
FROM Position__c WHERE SEI_Investor_Id__c
IN :Trigger.newMap.keyset() GROUP BY SEI_Investor_Id__c]) {
Trigger.newMap.get(result.get('SEI_Investor_Id__c')).Sum_of_Positions__c = (Decimal)result.get('expr0');
}
}
trigger SumPositions on Investor__c (before insert, before update, before delete) {
for (Investor__c record : Trigger.new) {
record.Sum_of_Positions__c = null;
}
for(AggregateResult result: [SELECT SEI_Investor_Id__c, SUM(Position__c.Balance__c)
FROM Position__c WHERE SEI_Investor_Id__c
IN :Trigger.newMap.keyset() GROUP BY SEI_Investor_Id__c]) {
Trigger.newMap.get(result.get('SEI_Investor_Id__c')).Sum_of_Positions__c = (Decimal)result.get('expr0');
}
}
<pre>
trigger SumPositions on Investor__c ( before insert, before update, before delete )
{
List<Investor__c> records = Trigger.isDelete ? Trigger.old : Trigger.new;
Map<Id,Investor__c> recordsMap = Trigger.isDelete ? Trigger.oldMap : Trigger.newMap;
for ( Investor__c record : records )
{
record.Sum_of_Positions__c = null;
}
for ( AggregateResult result :
[ SELECT SEI_Investor_Id__c, SUM(Balance__c) sumPositions
FROM Position__c
WHERE SEI_Investor_Id__c IN :records
GROUP BY SEI_Investor_Id__c
]
)
{
recordsMap.get( (Id) result.get( 'SEI_Investor_Id__c' ) ).Sum_of_Positions__c =
(Decimal) result.get( 'sumPositions' );
}
}
</pre>
All Answers
Basically, your test class should do the following
1. create and insert Investor records (check the Sum of Positions, is it zero as expected?)
2. create and insert Positions
3. update your investor records (check their sum of Positions, are they updated?)
That pretty much sums it up (no pun intended). I'm happy to write out some actual code if that's what you're looking for.
Trigger SumP on Position__c (after insert, after update, after delete) {
Set<Id> investorIds = new Set<Id>();
List<Investor__c> investorsToUpdate = new List<Investor__c>();
if (!Trigger.isDelete) {
for (Position__c p : Trigger.new) {
investorIds.add(p.SEI_Investor_Id__c);
}
}
if (Trigger.isUpdate || Trigger.isDelete) {
for (Position__c p : Trigger.old) {
investorIds.add(p.SEI_Investor_Id__c);
}
}
Map<id, Investor__c> popMap = new Map<id,Investor__c>([select id, Sum_of_Positions__c from Investor__c where id IN :investorIds]);
List<AggregateResult> ars = [SELECT SEI_Investor_Id__c, count(Id) FROM Position__c WHERE SEI_Investor_Id__c IN :investorIds GROUP BY SEI_Investor_Id__c];
for (AggregateResult ar : ars) {
popMap.get((id)ar.get('SEI_Investor_Id__c')).Sum_of_Positions__c = (Decimal)ar.get('expr0');
investorsToUpdate.add(popMap.get((id)ar.get('SEI_Investor_Id__c')));
}
update investorsToUpdate;
}
Make sure you have added all required field on insert of position reocrds and check the debug log. You could find the issue.
<pre>
trigger SumPositions on Investor__c ( before insert, before update, before delete )
{
List<Investor__c> records = Trigger.isDelete ? Trigger.old : Trigger.new;
Map<Id,Investor__c> recordsMap = Trigger.isDelete ? Trigger.oldMap : Trigger.newMap;
for ( Investor__c record : records )
{
record.Sum_of_Positions__c = null;
}
for ( AggregateResult result :
[ SELECT SEI_Investor_Id__c, SUM(Balance__c) sumPositions
FROM Position__c
WHERE SEI_Investor_Id__c IN :records
GROUP BY SEI_Investor_Id__c
]
)
{
recordsMap.get( (Id) result.get( 'SEI_Investor_Id__c' ) ).Sum_of_Positions__c =
(Decimal) result.get( 'sumPositions' );
}
}
</pre>