function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Starz26Starz26 

Use keySet where a List is needed

Greetings,

 

Just curious if there is a one line way to declare a list and addAll values from a set to that list.

 

I currently have:

 

ID[] toDel = New ID[]{};
            toDel.addAll(PVtoExternalIDs.keySet());
            database.delete(toDel);

 

 

I would like to convert the keySet to a list and not have the other tow lines. Kind of like:

 

database.delete(New ID[]{map.keySet()});

 

but that does not work obviously....

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Unfortunately there isn't - set isn't supported as a parameter to the list constructor, so you have to instantiate then add all.  If the map values are the sobjects to delete you can pass that as the parameter to the delete call.

All Answers

devendra dhakadevendra dhaka

Is it necessary to be on liner ??

 

Otherwise the approach is very simple. Iterate over the set and copy all the elements in the list.

 

And then perform the delete operation.

bob_buzzardbob_buzzard

Unfortunately there isn't - set isn't supported as a parameter to the list constructor, so you have to instantiate then add all.  If the map values are the sobjects to delete you can pass that as the parameter to the delete call.

This was selected as the best answer
Starz26Starz26

Thanks to all who responded.

 

1. I have a Map that uses an external ID (not unique) as the Key and the ID of the record as the Values as Set<ID>

2. I need to find all records in the keySet() that have the ID in the values() (Set<ID>);

 

Since I cannot use a Set in database.delete, I need it in a list. Would have been nice not to have to itenerate over the set...just adds more code.

 

Since it is not possible, moving on.....