FOR Loop NOT being executed w/ NO errors





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}






up vote
1
down vote

favorite












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());

}









share|improve this question




















  • 2




    you could toss this trigger and use DLRS, a point and click tool
    – cropredy
    48 mins ago

















up vote
1
down vote

favorite












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());

}









share|improve this question




















  • 2




    you could toss this trigger and use DLRS, a point and click tool
    – cropredy
    48 mins ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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());

}









share|improve this question















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());

}






apex trigger soql roll-up-summary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 47 mins ago









Pranay Jaiswal

11.3k31951




11.3k31951










asked 51 mins ago









paulK

537




537








  • 2




    you could toss this trigger and use DLRS, a point and click tool
    – cropredy
    48 mins ago














  • 2




    you could toss this trigger and use DLRS, a point and click tool
    – cropredy
    48 mins ago








2




2




you could toss this trigger and use DLRS, a point and click tool
– cropredy
48 mins ago




you could toss this trigger and use DLRS, a point and click tool
– cropredy
48 mins ago










2 Answers
2






active

oldest

votes

















up vote
4
down vote













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.






share|improve this answer




























    up vote
    1
    down vote













    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






    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "459"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f240882%2ffor-loop-not-being-executed-w-no-errors%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote













      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.






      share|improve this answer

























        up vote
        4
        down vote













        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.






        share|improve this answer























          up vote
          4
          down vote










          up vote
          4
          down vote









          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 45 mins ago









          David Reed

          26.5k51745




          26.5k51745
























              up vote
              1
              down vote













              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






              share|improve this answer

























                up vote
                1
                down vote













                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






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  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






                  share|improve this answer












                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 32 mins ago









                  Pranay Jaiswal

                  11.3k31951




                  11.3k31951






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f240882%2ffor-loop-not-being-executed-w-no-errors%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga