You need to sign in to do that
Don't have an account?

Help getting SOQL outside of FOR loop
Ok, this is giving me head ache, I have so far been unable to get it correct. The below code works, but I really need to get the SOQL outside of the FOR loop, due to apex govenor limits. Can you assist please?
trigger CaseEquipmentStatus on Case (After Update) {
Set<Id> caseIds = new Set<Id>();
Set<Id> EquipIds = new Set<Id>();
for (Case c: Trigger.New){
if (c.Opportunity__c != null && c.Status == 'Request Completed' && c.Origin == 'PRM' )
caseIds.add(c.Opportunity__c);
}
for (Equipment__c Eq: [select id, Opportunity__c, Statis__c from
Equipment__c where Opportunity__c in :caseIds]) {
if(eq.Statis__c == 'Programming Change Requested'){
eq.Statis__c = 'Active'; }
Update eq;
} }
trigger CaseEquipmentStatus on Case (After Update) {
Set<Id> caseIds = new Set<Id>();
Set<Id> EquipIds = new Set<Id>();
for (Case c: Trigger.New){
if (c.Opportunity__c != null && c.Status == 'Request Completed' && c.Origin == 'PRM' )
caseIds.add(c.Opportunity__c);
}
for (Equipment__c Eq: [select id, Opportunity__c, Statis__c from
Equipment__c where Opportunity__c in :caseIds]) {
if(eq.Statis__c == 'Programming Change Requested'){
eq.Statis__c = 'Active'; }
Update eq;
} }
trigger CaseEquipmentStatus on Case (After Update) {
Set<Id> caseIds = new Set<Id>();
Set<Id> EquipIds = new Set<Id>();
for (Case c: Trigger.New){
if (c.Opportunity__c != null && c.Status == 'Request Completed' && c.Origin == 'PRM' )
caseIds.add(c.Opportunity__c);
}
}
list<Equipment__c> equipment = [select id, Opportunity__c, Statis__c from Equipment__c where Opportunity__c in :caseIds];
for (Equipment__c Eq: equipment) {
if(eq.Statis__c == 'Programming Change Requested'){
eq.Statis__c = 'Active';
}
}
Database.update(equipment);
}
All Answers
trigger CaseEquipmentStatus on Case (After Update) {
Set<Id> caseIds = new Set<Id>();
Set<Id> EquipIds = new Set<Id>();
for (Case c: Trigger.New){
if (c.Opportunity__c != null && c.Status == 'Request Completed' && c.Origin == 'PRM' )
caseIds.add(c.Opportunity__c);
}
}
list<Equipment__c> equipment = [select id, Opportunity__c, Statis__c from Equipment__c where Opportunity__c in :caseIds];
for (Equipment__c Eq: equipment) {
if(eq.Statis__c == 'Programming Change Requested'){
eq.Statis__c = 'Active';
}
}
Database.update(equipment);
}
You're code works great, had an extra "}" on line 8 , I imainge a typo. Thanks again for help!
trigger CaseEquipmentStatus on Case (After Update) {
Set<Id> caseIds = new Set<Id>();
Set<Id> EquipIds = new Set<Id>();
for (Case c: Trigger.New){
if (c.Opportunity__c != null && c.Status == 'Request Completed' && c.Origin == 'PRM' ) {
caseIds.add(c.Opportunity__c);
}
}
//why not just add the Statis__c value into the where clause?
list<Equipment__c> equipment = [select id, Opportunity__c, Statis__c
from Equipment__c
where Opportunity__c in :caseIds AND
Statis__c == 'Programming Change Requested'];
for (Equipment__c eq: equipment) {
eq.Statis__c = 'Active';
}
Database.update(equipment);
}