Skip to content

Instantly share code, notes, and snippets.

@msrivastav13
Created June 12, 2014 19:21
Show Gist options
  • Save msrivastav13/f0a8dd9c812a1d94b138 to your computer and use it in GitHub Desktop.
Save msrivastav13/f0a8dd9c812a1d94b138 to your computer and use it in GitHub Desktop.
public class ParentChildMapHelper{
public static Map<String,List<sobject>> buildGeneralisedMap(String fieldname,List<Sobject> lstsobject){
Map<String,List<sobject>> mapIdbySobject=new Map<String,List<sobject>>();
for(Sobject s:lstsobject){
if (mapIdbySobject.containsKey((String)s.get(fieldname))){
mapIdbySobject.get((String)s.get(fieldname)).add(s); //tricky part .Here map.get(key) is returning list and we are adding contacts to the list
} else{
List <sobject> lstsobjects = new List <sobject> (); //Initialize list as no key is found before and first time we get key
lstsobjects.add(s);
mapIdbySobject.put((String)s.get(fieldname), lstsobjects);
}
}
return mapIdbySobject;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment