I am new to salesforce and to coding in general and I am struggling to understand how to correctly write this class / SOQL query. Any advice or guidance would be appreciated.

I need to specifically grab any related object id after taking in a custom object id. I put a generalization at the bottom of what I have tried.

public static List<Id> getIds (String Id){
     SELECT id
     FROM relatedCustomObj1__c, relatedCustomObj2__c, relatedCustomObj3__c
     WHERE mainCustomObj__c.id = :Id


I have also tried

SELECT relatedCustomObj1__c, relatedCustomObj2__c, relatedCustomObj3__c
FROM mainCustomObj__c
WHERE mainCustomObj__c.id = :Id
  • 1
    Hi, welcome to SFSE. Please take a tour and read How to Ask. The simple answer is - you can't query from two objects. However it's possible if they are connected with a relationship. But it looks like you lack of basic knowledge of Salesforce SOQL syntax and I recommend you to look at Trailhead: Developer Beginner Trail (especially at Apex Basics & Database module). It'd be great if you could also get familiar with SOQL Reference. – Przemysław Tamoń Mar 27 at 15:03
  • They are connected through a relationship. The main custom object has a relationship with the related custom objects – Dani Mar 27 at 15:07
  • 4
    Does this answer your question? Querying from Multiple Objects – Przemysław Tamoń Mar 27 at 15:18
  • Is it possible to take in my custom object Id and query all related object Ids? – Dani Mar 27 at 16:36

Based on the question and the comment "is it possible to take in my custom object Id and query all related object Ids" and you want the IDs from three different types of related object like shown in the question, you cannot do this in a single line of code. However, you could do something like:

public static List<Id> getIds(String id) {
    MainCustomObject__c mainObject = [
        SELECT Id,
               (SELECT Id FROM RelatedCustomObj1__r), 
               (SELECT Id FROM RelatedCustomObj2__r), 
               (SELECT Id FROM RelatedCustomObj3__r)
        FROM MainCustomObject__c
        WHERE Id = :id

    List<Id> ids = new List<Id>();

    ids.addAll(new Map<Id, RelatedCustomObj1__c>(main.RelatedCustomObj1__r).keySet());
    ids.addAll(new Map<Id, RelatedCustomObj2__c>(main.RelatedCustomObj2__r).keySet());
    ids.addAll(new Map<Id, RelatedCustomObj3__c>(main.RelatedCustomObj3__r).keySet());

    return ids;

I have made several assumptions here:

  1. Your main object type has relationship names to the three types of related custom objects that are RelatedCustomObj*__r. You need to look what you made the relationship name on the lookup fields back to main object from the related objects and use these with "__r" appended.
  2. You won't have too many instances of any of these related objects otherwise you might hit governor limits.
  3. You really want to mix the IDs of different types of object into a single list.
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.