2

The trigger is firing, there are no errors, and it is running through all of the code, except the FOR loop w/ the SOQL query.

THE QUESTION Why is the trigger not jumping into the FOR loop?

THE TRIGGER

trigger LookupRollup on Child__c (after insert, after update, after delete, after undelete) {

    // List of parent record ids to update
    Set<Id> parentIds = new Set<Id>();

    // In-memory copy of parent records
    Map<Id, Opportunity> parentRecords = new Map<Id, Opportunity>();

    // Gather the list of ID values to query on
    for (Child__c c : Trigger.isDelete?Trigger.old:Trigger.new) {
        parentIds.add(c.Opportunity__c); 
    }

    // Avoid null ID values
    parentIds.remove(null);

    // Create in-memory copy of parents
    for (Id parentId:parentIds) {
        parentRecords.put(parentId, new Opportunity (Id = parentId, Child_Rollup__c = 0));    
    }

    // Query all children for all parents, update Rollup Field value
    for (Child__c c : [select id, Dollar__c, Opportunity__c
                       from Child__c
                       where id in :parentIds]) {

                parentRecords.get(c.Opportunity__c).Child_Rollup__c += c.Dollar__c;
    }

    // Commit changes to the database
    Database.update(parentRecords.values());

}
1
  • 2
    you could toss this trigger and use DLRS, a point and click tool – cropredy Nov 28 '18 at 20:55
6

Because you're querying against Id with a Set of Ids of the wrong object. The query result is empty, so the for loop does not execute - it has nothing to iterate over.

parentIds contains Opportunity Ids:

    parentIds.add(c.Opportunity__c); 

Your query is

select id, Dollar__c, Opportunity__c
                   from Child__c
                   where id in :parentIds

No Child__c record's Id is in parentIds.

Instead, it appears that you want to query for Child__c records

WHERE Opportunity__c IN :parentIds

As cropredy points out in a comment, you can save yourself from reinventing the wheel here by simply using Declarative Lookup Rollup Summaries.

3
  • Thanks, David! That was a painfully simple solution. I guess I just needed a second pair of eyes. The solution was to change the SOQL query to the following: WHERE Opportunity__c IN :parentIds – paulK Nov 28 '18 at 21:52
  • 1
    @paulK Glad to get a direct solution but I would recommend you take a good look at Pranay's answer as well. I prefer the aggregate query approach. – David Reed Nov 28 '18 at 21:55
  • Will do. It seems more optimized for sure. – paulK Nov 28 '18 at 23:14
2

Alternatively you can use an aggregate function, This will be usefull if you have more than few hundred childs per parent, and you can run into SOQL rows limits if that happens.

trigger LookupRollup on Child__c (after insert, after update, after delete, after undelete) {

    // List of parent record ids to update
    Set<Id> parentIds = new Set<Id>();

    // In-memory copy of parent records
    Map<Id, Opportunity> parentRecords = new Map<Id, Opportunity>();

    // Gather the list of ID values to query on
    for (Child__c c : Trigger.isDelete?Trigger.old:Trigger.new) {
        parentIds.add(c.Opportunity__c); 
    }



    AggregateResult[] groupedResults =[SELECT Opportunity__c,sum(Dollar__c) FROM Child__c 
                                        WHERE Opportunity__c in :parentIds GROUP BY Opportunity__c];

    for(AggregateResult ar: groupedResults){
        parentRecords.get(ar.get('Opportunity__c')).Child_Rollup__c = ar.get('expr0); ;
    }

    // Commit changes to the database
    Database.update(parentRecords.values());

}

Src : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.