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
ExploreForceExploreForce 

Create a Map of related lists

Hi,

I have  Parent obj:Products__c  and child obj:Imagaes__c.
     
       Map<Id,list<Imagaes__c>> mapofImagaes=new Map<Id,list<Imagaes__c>>();
       /* list<Imagaes__c> lstImages = new list<Imagaes__c>();
        for(Products__c xPrd:listRauPrds1)
         {
           // lstImages.clear();
           for(Imagaes__c rImg:xPrd.Images__r)
           {
                   //create a map
                   lstImages.add(rImg);
                   mapofImagaes.put(xPrd.id, lstImages);
         
                
           }
        
           
          
          }*/

In the above code, I m trying to create a map which holds first three images of Product1..so on.
Basically,I have to display three images of Product in my email template.
Pls can anyone suggest, how to create MAP ?So, that I can fetch map and display Images.
Balaji BondarBalaji Bondar
Please try below code :
 
Map<Id,list<Imagaes__c>> mapofImagaes=new Map<Id,list<Imagaes__c>>();
list<Imagaes__c> lstImages = new list<Imagaes__c>();
for(Products__c xPrd:listRauPrds1){
	lstImages.clear();
	for count = 0;
	for(Imagaes__c rImg:xPrd.Images__r){
	//create a map
		if(count <3){
			lstImages.add(rImg);
			count ++ ;
		}
		
	}
	mapofImagaes.put(xPrd.id, lstImages);
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
SFDC coderSFDC coder
hi ExploreForce,
Since you have implemented parent child relationsip,i think that you can construct your map like this,
 
Map<Id,list<Imagaes__c>> mapofImagaes=new Map<Id,list<Imagaes__c>>();

//Query on child object
for(Images__c img:[select products__c,id from Images__c])
{
        //Populates the map when a new image for the same product is found 
      if(mapofImagaes.containskey(img.products__c))
      {
             list<Imagaes__c> lstImages = mapofImagaes.get(img.products__c);
             lstImages.add(img);
             mapofImagaes.put(img.products__c,lstImages );
      }

      //Populates the map with new product and images
      else
     {
             mapofImagaes.put(img.products__c,new List<Images__c>{img});
     } 
}
      in this way u'll get a map of products and its related images as list

Product1 KEY--->Image1,Image2 VALUE

thanks