FOR Loop NOT being executed w/ NO errors
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());
}
apex trigger soql roll-up-summary
add a comment |
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());
}
apex trigger soql roll-up-summary
2
you could toss this trigger and use DLRS, a point and click tool
– cropredy
Nov 28 at 20:55
add a comment |
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());
}
apex trigger soql roll-up-summary
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
apex trigger soql roll-up-summary
edited Nov 28 at 20:57
Pranay Jaiswal
12.3k32251
12.3k32251
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
add a comment |
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
add a comment |
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.
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
add a comment |
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
This is great, Pranay. Thanks!
– paulK
Nov 28 at 23:14
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
answered Nov 28 at 20:59
David Reed
27.6k61746
27.6k61746
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
add a comment |
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
add a comment |
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
This is great, Pranay. Thanks!
– paulK
Nov 28 at 23:14
add a comment |
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
This is great, Pranay. Thanks!
– paulK
Nov 28 at 23:14
add a comment |
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
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
answered Nov 28 at 21:11
Pranay Jaiswal
12.3k32251
12.3k32251
This is great, Pranay. Thanks!
– paulK
Nov 28 at 23:14
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
you could toss this trigger and use DLRS, a point and click tool
– cropredy
Nov 28 at 20:55