0

To do a Join From List to a String separated with commas we need to do the following:

String someStringVar = String.join(SomeList, ',');

But If I want to make a Join with Comma and Quotes?

For Example :

 List<String> StringList = new List<String>{'1','2','3'};

and the ouput should be :

'ag03','ag05','ag06','ag07','ag08','ag09','ag10','ex01','ex02','hu01'

as one String.

I'm asking that because I want to make a query on multi picklist.

****EDIT : Base on the Answer of @Mariia Illarionova ****

For querying a multi picklist field :

 String StringVar= '\'' + String.join(asd, '\',\'') + '\'';
    List<Object__c> inti = [SELECT Id,Agriculture__c FROM Interest__c WHERE Active__c = true AND  Agriculture__c INCLUDES (:StringVar)];

Which does not work

But this is Work:

List<Object__c> inti = [SELECT Id,Agriculture__c FROM Interest__c WHERE Active__c = true AND  Agriculture__c INCLUDES ('ag03','ag05','ag06','ag07','ag08','ag09','ag10','ex01','ex02','hu01')]; 

Can anyone tell me why please?

  • Your edit should probably be its own question – Derek F Jun 18 '19 at 12:41
5

That's quite a simple solution for this (also with join as you stated):

List<String> stringList = new List<String>{'1','2','3'};

String result = '\'' + String.join(stringList, '\',\'') + '\'';

system.debug(result);

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.