function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bouscalbouscal 

Need help understanding why I get an error when the code exactly matches other examples

Why do I get the error stating "Initial term of field expression must be a concrete SOBject: LIST<Case>?
public class HOL_NA_CaseEscalated {
    public static void processCase(Case[] cs) 
    {
        Map<string,id> rType = new Map<string,id>();
        for (RecordType rt : [SELECT id, developername FROM RecordType WHERE isactive=TRUE and sobjecttype='Case']){
         	rType.put(rt.DeveloperName, rt.Id);  // add key/value pairs to map so we can lookup ID by name
        }
        // List all child cases on the triggering Case
        List<Case> CaseChildren = new List<Case>();
        CaseChildren=[SELECT id,casenumber,parentid,subject FROM Case WHERE parentid=:cs.parentid];  
// ERROR DIRECTS ME TO LINE ABOVE
        
		id rsc = rType.get('Record Type One'); 
        id pdc = rType.get('Record Type Two'); 
            
        for(Case c:cs){
            if (c.recordtypeid==rsc){ // This is a Record Type One Case
 				               
            } else if (c.recordtypeid==pdc){ // This is a Record Type Two Case
                
            } else {
                // do nothing
            }
        }
	}
    
}
The error directs me to the last line above.
 
Best Answer chosen by bouscal
MithunPMithunP
Hi bouscal,

CS is a list, first you shd add case ids to a set and then query for that set of ids. Below is updated sample code.
 
public class HOL_NA_CaseEscalated {
    public static void processCase(Case[] cs) 
    {
        Map<string,id> rType = new Map<string,id>();
         set<id> parentIds = new set<id>();
         for(case css : cs){
          parentIds.add(css.id);
          } 
        for (RecordType rt : [SELECT id, developername FROM RecordType WHERE isactive=TRUE and sobjecttype='Case']){
         	rType.put(rt.DeveloperName, rt.Id);  // add key/value pairs to map so we can lookup ID by name
        }
        // List all child cases on the triggering Case
        List<Case> CaseChildren = new List<Case>();
        CaseChildren=[SELECT id,casenumber,parentid,subject FROM Case WHERE parentid in: parentIds];  
// ERROR DIRECTS ME TO LINE ABOVE
        
		id rsc = rType.get('Record Type One'); 
        id pdc = rType.get('Record Type Two'); 
            
        for(Case c:cs){
            if (c.recordtypeid==rsc){ // This is a Record Type One Case
 				               
            } else if (c.recordtypeid==pdc){ // This is a Record Type Two Case
                
            } else {
                // do nothing
            }
        }
	}
    
}

Best Regards,
Mithun.

All Answers

kevin lamkevin lam
cs is a list of Cases, so you need to loop through those Cases.
MithunPMithunP
Hi bouscal,

CS is a list, first you shd add case ids to a set and then query for that set of ids. Below is updated sample code.
 
public class HOL_NA_CaseEscalated {
    public static void processCase(Case[] cs) 
    {
        Map<string,id> rType = new Map<string,id>();
         set<id> parentIds = new set<id>();
         for(case css : cs){
          parentIds.add(css.id);
          } 
        for (RecordType rt : [SELECT id, developername FROM RecordType WHERE isactive=TRUE and sobjecttype='Case']){
         	rType.put(rt.DeveloperName, rt.Id);  // add key/value pairs to map so we can lookup ID by name
        }
        // List all child cases on the triggering Case
        List<Case> CaseChildren = new List<Case>();
        CaseChildren=[SELECT id,casenumber,parentid,subject FROM Case WHERE parentid in: parentIds];  
// ERROR DIRECTS ME TO LINE ABOVE
        
		id rsc = rType.get('Record Type One'); 
        id pdc = rType.get('Record Type Two'); 
            
        for(Case c:cs){
            if (c.recordtypeid==rsc){ // This is a Record Type One Case
 				               
            } else if (c.recordtypeid==pdc){ // This is a Record Type Two Case
                
            } else {
                // do nothing
            }
        }
	}
    
}

Best Regards,
Mithun.
This was selected as the best answer