Activity › Forums › Salesforce® Discussions › How can we convert a Set to Set in SFDC?
Tagged: ID, Salesforce Apex, Salesforce SOQL, SET, String
-
How can we convert a Set to Set in SFDC?
Posted by Danna on March 16, 2016 at 7:30 AMI want to convert a set of IDs to a set of strings. What would be the best method to go about it? I am hesitant to use loops to bruteforce it.
shariq replied 7 years, 9 months ago 5 Members · 4 Replies -
4 Replies
-
There are three main ways you can do it
1.
`public static Setfirstway(Set input)
{
return (Set)JSON.deserialize(JSON.serialize(input), Set .class);
}`2.
`public static Setsecondway(Set input)
{
return new Set(String.join(new List (input), ‘,’).split(‘,’));
}`3.
`public static Setthirdway(Set input)
{
return new Set((List )new List (input));
}`
- [adinserter block='9']
-
Hey @dannaray
I have collected some code to do so :
Setids = new Set {‘001C000001DgWjE’,’001C000001DgWjD’};
// Here’s the one line!
SetidStrs = (Set )JSON.deserialize(JSON.serialize(ids), Set .class);
System.debug(‘idStrings=’ + idStrs);I didn’t tried but it seems it will help you.
-
Hi Danna
Please try this
Set<Id> setofId = new Set<Id>{‘001C000001DgWjE’,’001C000001DgWjD’};
Set<String> setofString = (Set<String>)JSON.deserialize(JSON.serialize(ids), Set<String>.class);
System.debug(‘setofString=’ + setofString);Thanks
-
Hi,
Set<Id> idSet – your Id set
Serialize this set – this will convert this set into json string.
Now just deserialize it into Set<String >class, deserialize method convert the json string into set<String>which is assigned to set of string variable which done below –
Set<String> stringSet= (Set<String>)JSON.deserialize(JSON.serialize(idSet ), Set<String>.class);
Hope this helps.
Log In to reply.