How to define a map with id and list of records of an object

  • I have a custom object called entity_c, this has a field called product which is a lookup to product. There could be many records in entity_c for a particular product.

    I need to store it in a map with productId as the key and all the records for that product in a list.

    Here is what i have till now

      Map<id,List<Entity__c>> mapProductEntity = new    Map<id,List<Entity__c>>();
      for(Entity__c et : [SELECT name,Field1__c, Product__c, Field2__c FROM Entity__c WHERE Product__c IN : Items ORDER BY Product__c ]{
            if (mapProductEntity.containsKey(et.Product__c)){
                mapProductEntity.get(et.Product__c).add(new Entity__c(    field__c = et.field1__c,product__c = et.product__c, field2__c = et.value__c ));
            }
            else{
          // Not sure how to add it for first time      
                 //mapProductEntity.put(et.Product__c, lsEntit.add(new Entitlement__c(   field1__c = et.field1__c,product__c = et.product__c, field2__c = et.value__c )));
            }
    

    Is this a right approach? Is there a simpler way to do it?

  • techtrekker

    techtrekker Correct answer

    9 years ago

    Yes it seems alright to do it like you have, To add for the first time

    mapProductEntity.put(et.Product__c, new List <Entitlement__c> { et });

    Even where you're adding it above you don't need to reconstruct Entitlement__c as that is already your loop variable.

    So even above you can use

    if (mapProductEntity.containsKey(et.Product__c))
                {
    mapProductEntity.get(et.Product__c). add(et);
    

    Cheers. Coding on an iPhone is not easy ;) , I have to actually type it as & lt;

    there's no easy access to backticks? Or however you call these... just below Esc key on the normal keyboard. HTC for the win :P

    I guess it's not so much the iPhone as its the StackExchange mobile site which doesn't let you format your answers as code or not. And doesn't escape the greater than and less than within your answer. Are you saying there is a way to escape it?

    When i try to use if (mapProductEntity.containsKey(et.Product_c)) { mapProductEntity.get(et.Product_c). add(et); i get an error Save error: SObject constructor must use name=value pairs

    @techtrekker Next time you're on full-sized keyboard click the help icon in upper right of the editor. To indent a block of code you just need 4 spaces in front of each line, there's really nothing more that the editor icon does. And for `marking something as code in middle of sentence` wrap it in backticks. I'll edit your answer so you can hopefully see it in edit history (as soon as somebody approves it that is ;))

    Just double checking but the error is on the map.get().add line or elsewhere in the code. Can you update your question with the new code as an Edit. Thx

    @techtrekker i figured it out.. it was a typo from my end

    Thought as much ;) cool

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM