I have an object that's pretty extensive (lots of properties and child relationships). I'd like to serialize it. So I am doing the following:

Proposal__c proposal = (Proposal__c) getRecord();
String s = JSON.serialize(proposal);

The output:


And that is all. How can I get the entire object serialized, perhaps even including its child relationships?

  • 1
    Does the object returned by getRecord() populate more fields, or just those three?
    – Mike Chale
    Sep 10 '13 at 17:07

You probably need to query more fields to expose them in your serialization. It would help if you posted your getRecord() code, but the following ought to work:

SObject getRecord()
    List<String> fieldsToQuery = Proposal__c.SObjectType.getDescribe().fields.getMap().values();
    String fields = String.join(fieldsToQuery, ', ');
    String soqlQuery = String.format('SELECT {0} FROM Proposal__c LIMIT 1', new List<String> { fields });
    return Database.query(soqlQuery)

Proposal__c proposal = (Proposal__c) getRecord();
String s = JSON.serialize(proposal);
  • Just add the new Security.stripInaccessible(AccessType.READABLE, new List<Proposal__c>{proposal}).getRecords()[0]; to only return the record with readable fields. Reference here Feb 20 '20 at 13:56

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.