2
votes

I have a list of sObjects and I'd like to convert all the sObject fields to a string. For instance (this is the print out from a SOQL query),

Custom_sobj__c{
  "serId": 5,
  "value": {
    "Id": "a0FJ0000005zIbwMAE",
    "Contact__r": {
      "serId": 6,
      "value": {
        "Name": "Bob Bobenson",
        "Owner": {
          "serRefId": 4
        },
        "Rule_Class__c": "Class III - Quote\/De (2 more) ...",
        "OwnerId": "005d000000450RiAAI",
        "Id": "003J0000016ZjuCIAS"
      }
    },
    "Contact__c": "003J0000016ZjuCIAS"
  }
}

And I have a list of these objects. I'd like to convert it all to string, so it looks something like this:

...'sobjInstance3{"serid";5,"value":...}','sobjInstance4{"serid";5,"value":...}',...

I have a for loop iterate through the list,

String strSobjects = ' ';   

for(Custom_sobj__c obj : sobjList){
    strSobjects = strSobjects  + ','+String.valueOf(obj);
}

but this only returns the "ID" and "Contact__c". The string is getting the "Name" or "Rule_Class_c" fields.

How can I grab the rest of the data?

1

1 Answers

4
votes

The System.JSON method should do the trick: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_json_overview.htm

Something like:

String strSobjects = ' ';   

for(Custom_sobj__c obj : sobjList){
    strSobjects = strSobjects  + ','+JSON.Serialize(obj);
}