11

How to convert Map to List, here is what I have tried but getting the error:

List<Product2> productList = new List<Product2>();
productList.addAll(mapProducts.values());

Error:

> Incompatible argument type List<ProductWrapper> for All method on
> List<Product2>

Map:

Map<Id, ProductWrapper> mapProducts = new Map<Id, ProductWrapper>();

for (Product2 p : /*list of records*/) 
{        
    if(p.Asset_Line_items__r.isEmpty()){
      mapProducts.put(p.id, new ProductWrapper(p,new Asset_Line_Item__c(Quantity__c = 0))); 
    }
    else {            
      for(asset_line_items__c b : p.asset_line_items__r) {
          ProductWrapper pw = new ProductWrapper(p,b); 
          mapProducts.put(p.Id, pw);    
         }  
      }   
}

Wrapper class:

public class ProductWrapper {

    public Product2 product {get;set;}
    public asset_line_items__c ali {get;set;}    

    public ProductWrapper(Product2 p, asset_line_items__c ali) {
        product = p;
        this.ali = ali;
    }
}

1 Answer 1

13

The map's value (ProductWrapper) is incompatible with the list's value (Product2). You'd have to extract the values manually:

for(ProductWrapper wrapper: mapProducts.values()) {
    productList.add(wrapper.product);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .