Tuesday, 10 September 2019

Convert Set to String


Option 1:

Set<Id> accId = new Set<Id>();
for(Account acc : [SELECT Id, Name FROM Account LIMIT 10]) {
accId.add(acc.Id);
}
system.debug(accId);
String str = ''+accId;
system.debug(str);




Drawback: once the set size increase above option to convert the set to string as it receives i.e.

Option 2:
Set<Id> accId = new Set<Id>();
for(Account acc : [SELECT Id, Name FROM Account LIMIT 100]) {
accId.add(acc.Id);
}
List<Id> strList = new List<Id>(accId);
system.debug('strList: ' + strList);
String str = string.join(strList, ',');
system.debug('String value : ' + str);


output: