0

Hi I'm new to Salesforce Please Help me the Following SOQL Avoid in FOR loops.

Issue: When We Update Read__c (Checkbox) Field in Book sObject and In Cases Update (Recent_Record__c) field.

Thanks in Advance Manu

Helper Class

public class RecentReadInCase{
public static case cas;
Public static void mostRecentRead (List<Book__c> bkList){
List<Case> caslst = [SELECT CaseNumber,Recent_Record__c,(select name,Read__c from  book__r) FROM Case  where Recent_Record__c = false];
 if(caslst != null){
      for(Book__c b : bkList){ 
            cas = [select id,casenumber,Recent_Record__c from case where id =: b.Case_Number__c limit 1];
            if(cas != null)                  
            cas.Recent_Record__c = true;                    
      }        
  update(cas);
}
}
}

Trigger

trigger MostRecentFlagCaseTrig on Book__c (After insert,After Update) {
if(trigger.isAfter){
    if(trigger.isInsert || trigger.isUpdate){
        MostRecentReadInCase.mostRecentRead(trigger.new); 
    }
}
}

3 Answers 3

0
public class MostRecentReadInCase{
// You want it on update too, right?
// Making it a map instead of list for easier look up.
public static Map<ID, case> parentBk = new Map<ID, case>(); 
public static List<Id> listIds = new List<Id>();

public static void mostRecentRecord(list<Book__c> bklist){
 for(Book__c childObj : bklist){
listIds.add(childObj.Case_Number__c );
}
parentOpps = new Map<Id,case>([select CaseNumber,Recent_Record__c,(select name,Is_Read__c from  book__r) FROM Case WHERE ID IN :listIds]);
for(Book__c bk1 : bklist){
 case myParentbk = parentOpps.get(ems.Case_Number__c );
 myParentOpp.Recent_Record__c=bk1.Is_Read__c;
}

update parentbk.values();
} //to allow code formatting
}
3

i think that your better solution is dont use a query

public static void mostRecentRead (List<Book__c> bkList){

    Case[] updateCases = new Case[]{};

    for(Book__c b : bkList){

        if(String.isNotBlank(b.Case_Number__c)){

            updateCases.add( new Case(
                Id = b.Case_Number__c, 
                Recent_Record__c = true
            ));
        }
    }

    update updateCases;
}

Can you try this?

Best Regards.

0

You should make a list of Case, query everything before the for-loop and then use only the one you need in that loop. Example:

List<Id> bookCaseNrIds = new List<Id>();
//add all Case_Number__c from list of books into here
List<Case> someList = [select id,casenumber,Recent_Record__c from case where id IN bookCaseNrIds];
//rest of the code
    for(Book__c b : bkList){
      cas = null;
      for (Case c : someList) {
        if (c.Id == b.Case_Number__c) cas = c;
      }
      if(cas != null) cas.Recent_Record__c = true;
//rest of the code

Like that you will avoid any queries in your loop

1
  • Hi Novarg thanks for Responding. This Issue was Cases into a Map<Id,Case> which we can do directly from the SOQL query to avoid post-query processing. Please Help me. Thanks...
    – Manu17
    Commented Dec 23, 2014 at 10:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .