0

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 with id = sid, and this will pull up all records ObjectB and does will pull their corresponding ObjectC's

The 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.

1 Answer 1

1

I think what you're asking is, given a variable objectA of type Object_A__c and objectB of type Object_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 to Object_A__c called Parent__c - you do it from child to parent like this:

SELECT Id, Name, Parent__c, Parent__r.Name, Parent__r.Some_Other_Field__c
FROM Object_B__c
WHERE some_criteria_are_true

An Object_B__c instance coming from this query will have Parent__c populated (assuming it's got the relationship established) with an Id value, and it will also have the property Parent__r populated, with an sObject instance of type Object_A__c.

We can synthesize that structure using the method putSobject():

 Object_A__c objectA = [SELECT .... FROM Object_A__c WHERE ...];
 Object_B__c objectB = [SELECT .... FROM Object_B__c WHERE ...];

 objectB.putSobject('Parent__r', objectA);

If we want the lookup field populated too, we'd have to also issue

objectB.put('Parent__c', objectA.Id);

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 without Id suffixed, so for Contact.AccountId you would do

someContact.putSobject('Account', someAccount);
3
  • I'll Update the question, one minute.
    – ekiim
    Commented Dec 4, 2018 at 23:34
  • I just Updated!
    – ekiim
    Commented Dec 4, 2018 at 23:40
  • @ekiim Thanks, I will come back in a few and see if I can be more specific.
    – David Reed
    Commented Dec 4, 2018 at 23:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .