I have a method return type of map which I need to convert to list<Order.Product>.Here Order is main class and ProductOrder is inner class which has variables.

I have assigned the map to list in below way in test class but I am getting error as Illegal assignment from Map<Id,String> to List<Order.ProductOrder>

    List<Order.ProductOrder> ordList = OrderService.build(oIds);

Here OrderService classname and build method name which has return type Map. How to resolve this issue

  • If you're doing this in a test class, can you refer to the production class which is doing this work as well to see how the conversion is being performed there? – Mark Pond Oct 23 '17 at 19:18

In general, to convert maps to lists, you would use the map's values method:

Map<String, Integer> integersByString = new Map<String, Integer>{'a' => 1, 'b' => 2};
List<Integers> integers = integersByString.values();

If you want the keys, you need to get the key set:

Set<String> keys = integersByString.keySet();

now you can convert those to a list:

List<String> strings = new List<String>(keys);

But in your case these won't work since your map contains other types. You probably need to change the returntype of your build() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.