Salesforce: QueryResult must start with '{'
The problem isn't actually that you specify Parent__r
. Rather it is that your Parent__r
value specifies Children__r
as a List<SObject>
.
Serialize a query result which includes a Left Outer Join
and look more closely. The child relationship key doesn't point to a List<SObject>
. It points to a Map<String, Object>
. That map must specify both totalSize
and done
in addition to records
.
Won't work:
{
"Children__r": [{
"SomeField": "some value"
}, {
"SomeField": "some value"
}]
}
Will work:
{
"Children__r": {
"totalSize": 123,
"done": true,
"records": [{
"SomeField": "some value"
}, {
"SomeField": "some value"
}
}
}
A little late to the game, but for anybody finding this thread, I ran accross the same error, because the retrieval of a subquery through a @RemoteAction produces the incorrect structure as Adrian states:
{
"Children__r": [{
"SomeField": "some value"
}, {
"SomeField": "some value"
}]
}
Therefore, if you are using this javascript object to Apex for deserialization through another @RemoteAction, you can "rewrite" it using a javascript function before sending it off:
function rewriteSubquery(array) {
if (array && !array.hasOwnProperty('records')) {
var tempArray = array;
array = {
totalSize: tempArray.length,
done: true,
records: tempArray
}
}
return array;
};
As an example, if you have an object stored in Javascript, with the following structure:
parentObject = {
Id: 'a0K0E000002AolHUAS',
Name: 'Test',
SubqueryObjects__r: []
}
You can then call the method like this:
parentObject.SubqueryObjects__r = rewriteSubquery(parentObject.SubqueryObjects__r);
Which will return the structure that you need for correct deserialization:
parentObject = {
Id: 'a0K0E000002AolHUAS',
Name: 'Test',
SubqueryObjects__r: {
totalSize: <array_size>
done: true,
records: []
}
}