FOR Loop NOT being executed w/ NO errors





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






up vote
2
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
    Nov 28 at 20:55

















up vote
2
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
    Nov 28 at 20:55













up vote
2
down vote

favorite









up vote
2
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 Nov 28 at 20:57









Pranay Jaiswal

12.1k32251




12.1k32251










asked Nov 28 at 20:52









paulK

637




637








  • 2




    you could toss this trigger and use DLRS, a point and click tool
    – cropredy
    Nov 28 at 20:55














  • 2




    you could toss this trigger and use DLRS, a point and click tool
    – cropredy
    Nov 28 at 20:55








2




2




you could toss this trigger and use DLRS, a point and click tool
– cropredy
Nov 28 at 20:55




you could toss this trigger and use DLRS, a point and click tool
– cropredy
Nov 28 at 20:55










2 Answers
2






active

oldest

votes

















up vote
6
down vote



accepted










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





















  • 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 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 at 21:55










  • Will do. It seems more optimized for sure.
    – paulK
    Nov 28 at 23:14


















up vote
2
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





















  • This is great, Pranay. Thanks!
    – paulK
    Nov 28 at 23:14











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
6
down vote



accepted










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





















  • 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 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 at 21:55










  • Will do. It seems more optimized for sure.
    – paulK
    Nov 28 at 23:14















up vote
6
down vote



accepted










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





















  • 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 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 at 21:55










  • Will do. It seems more optimized for sure.
    – paulK
    Nov 28 at 23:14













up vote
6
down vote



accepted







up vote
6
down vote



accepted






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 Nov 28 at 20:59









David Reed

27.3k61746




27.3k61746












  • 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 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 at 21:55










  • Will do. It seems more optimized for sure.
    – paulK
    Nov 28 at 23:14


















  • 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 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 at 21:55










  • Will do. It seems more optimized for sure.
    – paulK
    Nov 28 at 23:14
















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 at 21:52






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 at 21:52






1




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 at 21:55




@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 at 21:55












Will do. It seems more optimized for sure.
– paulK
Nov 28 at 23:14




Will do. It seems more optimized for sure.
– paulK
Nov 28 at 23:14












up vote
2
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





















  • This is great, Pranay. Thanks!
    – paulK
    Nov 28 at 23:14















up vote
2
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





















  • This is great, Pranay. Thanks!
    – paulK
    Nov 28 at 23:14













up vote
2
down vote










up vote
2
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 Nov 28 at 21:11









Pranay Jaiswal

12.1k32251




12.1k32251












  • This is great, Pranay. Thanks!
    – paulK
    Nov 28 at 23:14


















  • This is great, Pranay. Thanks!
    – paulK
    Nov 28 at 23:14
















This is great, Pranay. Thanks!
– paulK
Nov 28 at 23:14




This is great, Pranay. Thanks!
– paulK
Nov 28 at 23:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Salesforce Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?