0

I have a Map<String, Object> idCollection.

Object accountIdObject = idCollection.get('accountId');

Doing this gets me a single string value. Now I am sure that accountIdObject is a String and have a requirement to add it to a List<String> accountId

Something like this is easily possible in java which would assign the single string to a new list -

List<String> accountId = Arrays.asList((String)accountIdObject);

What would be the best way for such initialization in apex ?

I have tried - List<String> accountId = new List<String>{(String)accountIdObject}; which is giving an exception Illegal assignment from Object to List. Also I am sure that accountIdObject contains a string value which I have confirmed by doing a accountIdObject instanceof String which returned true.

2
  • What else would be in this map? Will the values of the map ever be something other than strings? Is the plan that this List would only have more than one value? Or should it only have one value? Jun 2 at 21:07
  • @Nicholas The map would either return a single string or a list of strings. In case it's a single string, I just want to add it to the accountId list and in case it's a list of strings then simply assign that value to accountId. Something like - List<String> accountId = accountIdObject instanceof String ? new List<String>{(String)accountIdObject} : accountIdObject; But this gives an exception "Illegal assignment from Object to List." Jun 2 at 21:13
1

The long form of what you want looks like:

Object accountIdObject = idCollection.get('accountId');
String[] idValues;
if(accountIdObject instanceOf String) {
  idValues = new List<String> { (String)accountIdObject };
}
if(accountIdObject instanceOf List<String>) {
  idValues = (List<String>)accountIdObject;
}

Given your example code, the condensed version looks like:

List<String> accountId = accountIdObject instanceof String
   ? new List<String>{(String)accountIdObject} 
   : (List<String>)accountIdObject;

When using instanceOf, be aware of the limitations outlined in this post. If it's not a literal List<String>, but some other type that's closely related, you may experience runtime errors.

1
  • The short form is what I was trying but looks like I had missed typecasting in the else part - (List<String>)accountIdObject and kept thinking that something is wrong with the assignment in the if part. Thanks a lot ! Jun 2 at 21:25
0

I'd argue that, based on the name of the map key you're using, you should be generating a List<Id> rather than a List<String>

In either case, initializing a collection (Map, Set, or List) in Apex is possible by using curly braces.

List<Id> accountIdsList = new List<Id>{(Id)accountIdObject};

Multiple values are comma separated

List<String> myStrings = new List<String>{'a', 'b', 'c'};

And Maps in particular use "key => value" syntax

Map<String, Integer> stringToIntMap = new Map<String, Integer>{
    'a' => 1,
    'b' => 2,
    'c' => 3
};
1
  • I am sure that accountIdObject contains a value of type string even though the name suggests otherwise. I have also confirmed that by doing accountIdObject instanceof String which returns true. I am trying List<String> accountId = new List<String>{(String)accountIdObject} is giving an exception "Illegal assignment from Object to List<String>" Jun 2 at 21:03

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.