Assuming you want the original object returning together with its related children the code below should provide a starting point. Note that to call the code you will need to include authentication information. The code assumes you will pass the object id in the URL e.g.:
/services/apexrest/xyz/a00i000000AnlOC.json
The code:
@RestResource(urlMapping='/xyz/*')
global with sharing class ParentRest {
@HttpPost
global static SObject doPost() {
Id parentId = extractId(RestContext.request.requestURI);
// Fire triggers by updating
update parentId.getSObjectType().newSObject(parentId);
// Query parent and children in one go so all serialized
return query(parentId);
}
private static SObject query(Id parentId) {
// Query all parent fields
DescribeSObjectResult pd = parentId.getSObjectType().getDescribe();
String soql = 'select ' + csvs(pd.fields.getMap().keySet());
for (ChildRelationship cr : pd.getChildRelationships()) {
if (String.valueOf(cr.getChildSObject()).endsWithIgnoreCase('__c')) {
// Query all child fields of each child
DescribeSObjectResult cd = cr.getChildSObject().getDescribe();
soql += ', (select ' + csvs(cd.fields.getMap().keySet())
+ ' from ' + cr.getRelationshipName() + ')';
}
}
soql += ' from ' + parentId.getSObjectType();
soql += ' where Id = :parentId';
System.debug('>>> soql=' + soql);
return Database.query(soql);
}
private static String csvs(Set<String> strings) {
return String.join(new List<String>(strings), ',');
}
private static Id extractId(String uri) {
String prefix = '/xyz/';
Integer s = uri.indexOf(prefix);
if (s != -1) {
s += prefix.length();
Integer f = uri.indexOf('.', s);
if (f == -1) {
f = uri.length();
}
return (Id) uri.substring(s, f);
}
return null;
}
}
An example of the sort of JSON returned:
{
"attributes":{
"type":"Parent__c",
"url":"/services/data/v29.0/sobjects/Parent__c/a00i000000AnlOCAAZ"
},
"Name":"Parent 1",
"LastViewedDate":"2013-11-12T21:43:10.000+0000",
"LastReferencedDate":"2013-11-12T21:43:10.000+0000",
"LastModifiedById":"005i0000001PYqgAAG",
"SystemModstamp":"2013-11-12T21:43:10.000+0000",
"CreatedById":"005i0000001PYqgAAG",
"OwnerId":"005i0000001PYqgAAG",
"CreatedDate":"2013-11-12T21:25:43.000+0000",
"LastModifiedDate":"2013-11-12T21:43:10.000+0000",
"IsDeleted":false,
"Id":"a00i000000AnlOCAAZ",
"Children__r":{
"totalSize":3,
"done":true,
"records":[
{
"attributes":{
"type":"Child__c",
"url":"/services/data/v29.0/sobjects/Child__c/a01i0000007AfA9AAK"
},
"Name":"Child 2",
"LastModifiedById":"005i0000001PYqgAAG",
"SystemModstamp":"2013-11-12T21:26:13.000+0000",
"CreatedById":"005i0000001PYqgAAG",
"CreatedDate":"2013-11-12T21:26:13.000+0000",
"LastModifiedDate":"2013-11-12T21:26:13.000+0000",
"IsDeleted":false,
"Id":"a01i0000007AfA9AAK",
"Parent__c":"a00i000000AnlOCAAZ"
},
{
"attributes":{
"type":"Child__c",
"url":"/services/data/v29.0/sobjects/Child__c/a01i0000007AfAmAAK"
},
"Name":"Child 3",
"LastModifiedById":"005i0000001PYqgAAG",
"SystemModstamp":"2013-11-12T21:26:22.000+0000",
"CreatedById":"005i0000001PYqgAAG",
"CreatedDate":"2013-11-12T21:26:22.000+0000",
"LastModifiedDate":"2013-11-12T21:26:22.000+0000",
"IsDeleted":false,
"Id":"a01i0000007AfAmAAK",
"Parent__c":"a00i000000AnlOCAAZ"
},
{
"attributes":{
"type":"Child__c",
"url":"/services/data/v29.0/sobjects/Child__c/a01i0000007AfAhAAK"
},
"Name":"Child 1",
"LastModifiedById":"005i0000001PYqgAAG",
"SystemModstamp":"2013-11-12T21:25:57.000+0000",
"CreatedById":"005i0000001PYqgAAG",
"CreatedDate":"2013-11-12T21:25:57.000+0000",
"LastModifiedDate":"2013-11-12T21:25:57.000+0000",
"IsDeleted":false,
"Id":"a01i0000007AfAhAAK",
"Parent__c":"a00i000000AnlOCAAZ"
}
]
}
}
You can filter out fields as you build the query to simplify the response. If you want more control over the response structure, you can create your own bean classes and populate them from the query result.