3

This is my Code in apex, But its showing Error-AuraEnabled methods do not support the return type of Map<String, Set<String>>

I am not getting what's wrong in my code and why map<string, set<string>> not working, while Map<string, list<string>> working fine.

public class testmap {
    @AuraEnabled
   Public static Map<string,set<string>> getmap() {
        Map<string,set<string>> accountmap=new Map<string,set<string>>();
        set<string> valueset=new set<string>();
        
        valueset.add('Value1');
        accountmap.put('Key1',valueset);
        system.debug('Maptest'+accountmap);
        return accountmap;
   }
}
12
  • Aura methods need to be static.
    – Phil W
    Sep 5, 2020 at 6:58
  • 1
    See this same problem exists:@Arpit public class testmap { @AuraEnabled Public static Map<string,set<string>> getmap() { Map<string,set<string>> accountmap=new Map<string,set<string>>(); set<string> valueset=new set<string>(); valueset.add('Value1'); accountmap.put('Key1',valueset); system.debug('Maptest'+accountmap); return accountmap; } }
    – Prachi
    Sep 5, 2020 at 7:05
  • 3
    looks like Set in retrun is still not supported: blog.texei.com/… Sep 5, 2020 at 7:36
  • 1
    The blog posting Tushar shared earlier in the comments does indicate you cannot use Set, which is interesting.
    – Phil W
    Sep 5, 2020 at 9:49
  • 1
    Yes, it is Interesting @Phil W, and thanks Tushar for sharing this useful Blog
    – Prachi
    Sep 5, 2020 at 11:23

1 Answer 1

3

This error happens because, somewhere along the way, Set is not supported sent to the client, so rather than break things, they simply don't allow that return type for now. I presume that salesforce.com has walled off this return type until the conversion on the client matches, rather than implicitly converting it to an Array, which could break their plans for a future change when Set is properly supported (imagine your code one day breaks because the client's type changed from Array to Set). The obvious solution, for now, is to return a List instead. You can use a helper method for this:

static Map<String, List<String>> convertToMapList(Map<String, Set<String>> source) {
  Map<String, List<String>> result = new Map<String, List<String>>();
  for(String key: source.keySet()) {
    result.put(key, new List<String>(source.get(key)));
  }
  return result;
}

This stopgap will allow you to fairly easily switch back to Map<String, Set<String>> when/if this data type becomes supported, and also avoids a total rewrite of your code.

You must log in to answer this question.

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