I am building a dynamic SOQL from a JSON file. As of now, I am able to build SOQL for direct parent and direct child objects dynamically. However, I am stuck at a complex/deep relationship.

E.g. object->Parent->child->so on. 
  or object->Child->parent->so on.

I am okay with 2/3 level relationship(not expecting too deep). Is there way to achieve it dynamically(other than creating a recursive function, I am avoiding since I need to put some conditions on SOQL call, hence it will hard to keep track of conditions,which needs to be applied on that final(nested) object).


  • 1
    can you provide your current SOQL and explain a bit more what you want to achieve? Do you want to get the child objects or a dynamic list of standard/custom field to query? Do you have had a look on sub queries like developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/…?
    – Uwe Heim
    May 7 '18 at 10:52
  • Cdr- Opportunity, Account and Contacts object. Opportunity is the center object for me. As of now, I am able to access account object(as it is parent object) . But in this case, Contact is not directly accessible with Opportunity(but from account it is accessible, since it is child of Account). I am aware of querying parent/child objects however finding solution for 2-3 level relationships dynamically.Since I will not be knowing which object is related to which of sub-object present in given JSON
    – Ysr Shk
    May 7 '18 at 11:08
  • So is the overall problem that your JSON provides (say) two object names and you have to dynamically find a relationship path between them, possibly through a junction object?
    – David Reed
    May 7 '18 at 13:14
  • right. I need to verify some records, so need to find related records only. Therefore need to find relationship(through any related object).
    – Ysr Shk
    May 7 '18 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.