+ Start a Discussion

list - convert to string?

is there a way to convert a list of sobjects into a string?  I don't really care about the format, just that the field values can be string searched. Something like this:



List<sobject> pparcels = database.query('select ...');
String relatedMatches = pparcels.toString(); //toString doesn't exist
if (relatedMatches.indexOf(affectedIds[i]) > -1) {




I don't think there is a way to do this in a simple way.

But why are you trying to cast and entire list to a single String?

If it is for a search or something like that you can achieve it by soql or sosl.


Please provide more details about what you are trying to do to see if there is a better way to do this.




I don’t understand the usage of converting list of Sobject to a string. It will be difficult for you to process later.


Salesforce use “;” as separator so you can do as given below :


String strSobjects = null;


for(int i=0;i<lstSobject.size();i++)


                If(strSobjects == null)


                strSobjects = lstSobject[i];




                strSobjects = strSobjects  + “;”+lstSobject[i];





I am trying to do the same but getting an error.


Illegal assignment from SOBJECT:priroty_wise_sla__c to String 


My Apex class is


public class PWS {

List<priroty_wise_sla__c> results;

public priroty_wise_sla__c o{get;set;}
public PWS(ApexPages.StandardController stdcontroller) {

this.o= (priroty_wise_sla__c)stdController.getRecord();


public List<priroty_wise_sla__c> getResults() {
      return results;

  public PageReference dosearch()

 results=[SELECT CUSTOMER_NAME__c,priority__c, Minute__c,hour__c,day__c from priroty_wise_sla__c where priority__c=:o.priority__c and CUSTOMER_NAME__c=:o.CUSTOMER_NAME__c];
 String strSobjects = null;


for(integer i=0;i<results.size();i++)

               strSobjects =results[i];



          strSobjects = strSobjects  + ';'+results[i];


return null;




Suppose :


List<Sobject> scopeAcc = new ......(with values in it ) 


for(sObject so : scopeAcc)
System.debug('Scope check' + so);

String CA_id = so.id;  // assignation


System.debug( 'sObject to String  conversion : '  + CA_id ) ;



Came across this old post while looking for something else...
In case you never found the solution you can now use the JSON serialise method:

string JSONString = JSON.serialize(li_TurnoverTargetstoUpdate);

list<TurnoverTarget__c> li_TurnoverTargetstoUpdate = (list<TurnoverTarget__c>) JSON.deserialize(JSONString, list<TurnoverTarget__c>.class);

Very useful in passing a list of records from a trigger to a future method for updating
Richard DiNardo 6Richard DiNardo 6
Object o = 'some complex object';
String temp = string.valueOf(o);
Julio Lozano 11Julio Lozano 11
The easiest way to achieve this is using the join method from the String class:
List<Integer> li = new List<Integer> {10, 20, 30};
String s = String.join(li, '/');
System.assertEquals('10/20/30', s);