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
SoozeeSoozee 

How to retrieve SOQL results as comma separated string?

Hello,

I am trying to extract a list of ids from a SOQL query as a comma separated string so that I can then pass the string to another method which will create a PDF of the records.  The results should look like this:  X2VV0000000DxXxxxx,X2VV0000000DxXxXXX,X2VV0000000XxXtXXX

 

Since result.getParameters().put() is expecting two strings, the Apex won't compile.  I get:  Save error: Incompatible value type LIST<Letter__c> for MAP<String,String>   


Here is the code:

 

    public PageReference generateBatchPDF() {
    	
    	
        String letterIDs = ApexPages.CurrentPage().getParameters().get('letterIDs');
        System.Debug('DEBUG: String letterIDs = '+letterIDs);
       
        String[] listofids = letterIds.split(',');
        List<Letter__c> printletters = [select id from Letter__c where format__c != 'email only' and id IN :listofids];
        
        System.Debug('DEBUG: List printletters = '+printletters);
        
        PageReference result=new PageReference('/apex/EOTLBatchPDF');
        
        result.getParameters().put('id', printletters);
       
        return result;
    }

 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

You may have wanted to do this, not sure:

 

String params = '';

 

for(integer x =0;x<printletters.size();x++){

   if(x>0) params +=',';  

       params += printletters[x].id;

}

 

result.getParameters().put('id', params);

All Answers

Starz26Starz26

just use:

 

 

result.getParameters().put('id', printletters[0].id);

 

or itenerate over the list if you are searching for a record to use

 

Starz26Starz26

You may have wanted to do this, not sure:

 

String params = '';

 

for(integer x =0;x<printletters.size();x++){

   if(x>0) params +=',';  

       params += printletters[x].id;

}

 

result.getParameters().put('id', params);
This was selected as the best answer
SoozeeSoozee

Thank you!  That worked beautifully!