You need to sign in to do that
Don't have an account?
Jayaramu T 9
I need to avoid SOQL query inside for loop but it is using for loop reference variable any suggetions please to avoid soql in loop . and what is the use of "system.test.isRunningTest()" in below code
List<CaseMilestone> lp_setmstones = new List<CaseMilestone>();
for (CaseMilestoneSetting__c CMileS : CMileSs){
id mstoneID = CMileS.MilestoneID__c;
// Find all CaseMilestones matching our criteria in our caselist
LIST<CaseMilestone> setmstones;
if(system.test.isRunningTest())
setmstones = Database.query('Select ID,MilestoneTypeID,CompletionDate FROM CaseMilestone WHERE CaseID IN:caselist AND MilestoneTypeID=:mstoneID AND CompletionDate=NULL');
else
setmstones = Database.query('Select ID,MilestoneTypeID,CompletionDate FROM CaseMilestone WHERE '+
CMileS.CaseCriteria__c+
'AND CaseID IN:caselist AND MilestoneTypeID=:mstoneID AND CompletionDate=NULL');
// Update the CompletionDate for each CaseMilesone
for (CaseMilestone updatemstone : setmstones){
updatemstone.CompletionDate= Datetime.now();
}
// Execute the update for all the Milestones in this set
lp_setmstones.addAll(setmstones);
}
for (CaseMilestoneSetting__c CMileS : CMileSs){
id mstoneID = CMileS.MilestoneID__c;
// Find all CaseMilestones matching our criteria in our caselist
LIST<CaseMilestone> setmstones;
if(system.test.isRunningTest())
setmstones = Database.query('Select ID,MilestoneTypeID,CompletionDate FROM CaseMilestone WHERE CaseID IN:caselist AND MilestoneTypeID=:mstoneID AND CompletionDate=NULL');
else
setmstones = Database.query('Select ID,MilestoneTypeID,CompletionDate FROM CaseMilestone WHERE '+
CMileS.CaseCriteria__c+
'AND CaseID IN:caselist AND MilestoneTypeID=:mstoneID AND CompletionDate=NULL');
// Update the CompletionDate for each CaseMilesone
for (CaseMilestone updatemstone : setmstones){
updatemstone.CompletionDate= Datetime.now();
}
// Execute the update for all the Milestones in this set
lp_setmstones.addAll(setmstones);
}
All Answers
You need to use a map of milestoneId and list of cases to avoid SOQL Query inside the for loop.
Loop over caseMilestones related to the case and prepare this map.
At line 14 you have used for loop variable CMileS. could you please rectify the code
Why did you choose to use a different query for the test?
Will the below code work for you?