-2

I am trying to have a class with map as return type. I am not sure how to return a custom map. Here is my code:

public static Map<String,Integer,Integer,Integer> getMdus() {
   Map<String,Integer,Integer,Integer> mdus = new Map<String,Integer,Integer,Integer>();
   mdus.put('bldg',activeUnits.size(),neverUnits.size(),coldUnits.size());
   return mdus;
}

How do I make this above code work?

  • What are you trying to do? This syntax is incorrect, but you have an x-y problem. We need to know what you're trying to do so we can help you. Please edit your question to describe what you're doing. – sfdcfox Feb 7 at 4:45
  • I am trying to pass a string with a few integer values to the helper file. In the above each row will have a string and few a integer values that is associated with that string. – Sumchans Feb 7 at 4:51
  • You will have to create a Wrapper class since a map can only contain one value for the corresponding key. The 3 integer values should be part of a single object (the wrapper class). – Aayush K Feb 7 at 5:00
  • @Aayush could you provide me with an example – Sumchans Feb 7 at 5:02
  • Added example as an answer below. – Aayush K Feb 7 at 5:10
2

You can create a wrapper object to encapsulate multiple values within a single object.

Define a wrapper class.

public class UnitSizeWrapper{
    public Integer activeUnitSize {get; set;}
    public Integer neverUnitSize {get; set;}
    public Integer coldUnitSize {get; set;}
}

Use the wrapper class in your method.

public static Map<String,UnitSizeWrapper> getMdus() {
   Map<String,UnitSizeWrapper> mdus = new Map<String,UnitSizeWrapper>();

   UnitSizeWrapper wrapper = new UnitSizeWrapper();
   wrapper.activeUnitSize = activeUnits.size();
   wrapper.neverUnitSize = neverUnits.size();
   wrapper.coldUnitSize = coldUnits.size();

   mdus.put('bldg', wrapper);
   return mdus;
}
  • Thanks Aayush, that worked! – Sumchans Feb 8 at 4:41
0

Everything is fine except that you are using the map syntax incorrect. Please take a look at this link - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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