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

Comma separated string for use in SOQL

in the code below the dp.CS_LocationsCSV__c is comma selerated string of account numbers  ie  123,145,16,143  and i need to use them in the soql querey  SELECT ,source_key__c  FROM ERP_Data__c where source_key__c = :csv   this works fine with one account number but i may have up to 50, how can i get them into a useable set for the querey?

trigger CS_links on Distributor_Program__c (after update) {


// get a list of all Distributor_Program__c to evaluate

Set<String> csv = new Set<String>();
Set<Id> changedcsv = new Set<Id>();

for (Distributor_Program__c  dp{

    String oldcsv = Trigger.oldMap.get(dp.Id).CS_LocationsCSV__c;
    String newcsv = dp.CS_LocationsCSV__c;

    if (oldcsv != newcsv){

// delete all exsiting Participating_Branch_Locations__c if CS_LocationsCSV__c has changed

list <Participating_Branch_Locations__c> pb = [select id from Participating_Branch_Locations__c where Distributor_Program__c in :changedcsv];
delete pb;

// find the accounts to link to the Distributor_Program__c where the CS_LocationsCSV__c has changed

map<string , account> accids = new map<string , account> ();

list<ERP_Data__c> erps = [SELECT ,source_key__c  FROM ERP_Data__c where source_key__c = :csv];  // todo use the csv string 

for (ERP_Data__c erp :erps){
    accids.put(erp.source_key__c  , erp.Account__r );

//create the Participating_Branch_Locations__c and link to the account and Distributor_Program__c 

list<Participating_Branch_Locations__c> npb = new list<Participating_Branch_Locations__c> ();
        for (Distributor_Program__c  dp{

        account a = accids.get(dp.CS_LocationsCSV__c);
            if (a != null){
                for (erp_data__c acc : erps) {
                Participating_Branch_Locations__c apbl = new Participating_Branch_Locations__c();
                apbl.Account__c =;
                apbl.Distributor_Program__c =;
                apbl.key_id__c = + '' + ;

        insert npb;

Deepak Kumar ShyoranDeepak Kumar Shyoran
For that you need to convert your CSV values into a List<String> and need to use this list in your SOQL filter 
For ex :
String csv = 123,234,5445,5655,5565,5553 ;
List<String> csvList = csv.spit(',') ;
[SELECT ,source_key__c  FROM ERP_Data__c where source_key__c in :csv ]

Blessy Voola 4Blessy Voola 4
Hi Hammy,

Try using split method of String class. It returns list

if (oldcsv != newcsv){
String csvlist=dp.CS_LocationsCSV__c;
List<String> csv= csvlist.split(',');

list<ERP_Data__c> erps = [SELECT ,source_key__c  FROM ERP_Data__c where source_key__c in :csv];