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());
}
apex trigger soql roll-up-summary
add a comment |
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());
}
apex trigger soql roll-up-summary
2
you could toss this trigger and use DLRS, a point and click tool
– cropredy
48 mins ago
add a comment |
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());
}
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 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
add a comment |
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
add a comment |
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.
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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 45 mins ago
David Reed
26.5k51745
26.5k51745
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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 32 mins ago
Pranay Jaiswal
11.3k31951
11.3k31951
add a comment |
add a comment |
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
48 mins ago