Created
June 12, 2014 19:21
-
-
Save msrivastav13/f0a8dd9c812a1d94b138 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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