Activity Forums Salesforce® Discussions How can we convert a Set to Set in SFDC?

  • Portia

    Member
    March 17, 2016 at 8:51 am

    There are three main ways you can do it
    1.
    `public static Set firstway(Set input)
    {
    return (Set)JSON.deserialize(JSON.serialize(input), Set.class);
    }`

    2.
    `public static Set secondway(Set input)
    {
    return new Set(String.join(new List(input), ',').split(','));
    }`

    3.
    `public static Set thirdway(Set input)
    {
    return new Set((List)new List(input));
    }`

  • Amit

    Member
    March 29, 2016 at 2:39 pm

    Hey @dannaray
    I have collected some code to do so :
    Set ids = new Set{'001C000001DgWjE','001C000001DgWjD'};
    // Here's the one line!
    Set idStrs = (Set)JSON.deserialize(JSON.serialize(ids), Set.class);
    System.debug('idStrings=' + idStrs);

    I didn't tried but it seems it will help you.

  • Parul

    Member
    September 6, 2018 at 6:23 am

    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

  • shariq

    Member
    September 15, 2018 at 2:01 am

    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.

Popular Salesforce Blogs