I'm working with some complicated queries, and I need to send over http, the sObject in turn, but I can't make queries with more than 1 level of depth in the SOQL, so I am working with separated queries, then composing the sObjects, (or at least that is what I try), so I can created a full json
for when I send the http request
, but It raises an exception
like this
System.SObjectException: Invalid field Object_A for Object_B
May be I'm doing it wrong, but I am trying to add the field as if it where a regular map or sobject
// meta is a metadata object with a fixed query string
// sidWhere is a string with the ID and the quotation marks just to append to the query
string soql_main = meta.SOQL_Fields__c + ' WHERE Id = ' + sidWhere;
sObject[] join1;
string soql_join_1 = meta.SOQL_Join_1__c + sidWhere;
string sObjTypeName1;
join_1 = database.query(soql_join_1);
sObjTypeName1 = join_1.getSObjectType().getDescribe().getName();
main.put(sObjTypeName1, join_1); // Here it throws the exception
So, what would be a better way to do this composition I say, It's just adding one field
to the main
sobject, that contains the list of sobjects from the query in soql_join_1
.
Any Ideas?
Edit
Let's say I have an ObjectB that It's related to ObjectA via a lookup, that means ObjectB has a field pointing to ObjectA.
Now I want to get Object A and all the records that point to it of the ObjectB, but ObjectB also have records pointing to it of the type ObjectC, So I need to pull ObjectA withid = sid
, and this will pull up all records ObjectB and does will pull their corresponding ObjectC'sThe problem is that, when I try to make a Double nested query from the ObjectA table, turns out to be unallowed, so I did a query to pull up, Exclusivly ObjectA fields, and another one different that gets a list of all the ObjectB objects, that relate to ObjectA, and their children. NOW I want to manipulate the sOjbectA so It contains the list of ObjectB's that came out from a different query. Because I need to send it outside as a Json.
Best Answer
I think what you're asking is, given a variable
objectA
of typeObject_A__c
andobjectB
of typeObject_B__c
, which have a relationship to one another, how to reconstruct the object network in memory as if it had issued from a SOQL query. (Even if, as it sounds, you could not actually issue a SOQL query for an object network this complex).When you query a relationship in SOQL - let's say that
Object_B__c
has a lookup relationship toObject_A__c
calledParent__c
- you do it from child to parent like this:An
Object_B__c
instance coming from this query will haveParent__c
populated (assuming it's got the relationship established) with anId
value, and it will also have the propertyParent__r
populated, with an sObject instance of typeObject_A__c
.We can synthesize that structure using the method
putSobject()
:If we want the lookup field populated too, we'd have to also issue
Note that the field name you supply to
putSobject()
is the relationship form. For custom objects, it's postfixed with__r
(__c
is an Id field). For standard objects, it's the name of the field withoutId
suffixed, so forContact.AccountId
you would do