+ Start a Discussion
louisa barrett 7louisa barrett 7 

SOQL to return user id and list of custom object

Hi,

I would like to return a user ID and a list of custom objects that the user is the owner of using an inner select, but I don't know where to find the child relationship name for the custom object.

example:
List<User> owners = [SELECT ID, Name, 
                             (SELECT ID, Name, OwnerID, Month_Number__c
                              FROM Custom_Object__c)
                             FROM User
                             WHERE OwnerID IN : ownerIDs];

I basically want to end up with a map of <ID, List<Custom_Object__c>>(), where the ID is the user ID and the list is all the custom object records that the user is the owner of.
Normally I would just look on the object and get the child relationship name from there, but the user object does not show it's related objects.
Is this query possible from the user table?

Thanks
Raj VakatiRaj Vakati
What is the relationship you are having  now  and can u give the API names ?
louisa barrett 7louisa barrett 7
This is my actual query

List<User> owners = [SELECT ID, Name, (SELECT ID, Name, OwnerID, Closed_Amount__c, Month_Number__c FROM Sales_Target__c)          FROM User
WHERE OwnerID IN : ownerIDs];

The error I get is that the relationship Sales_Targets__c is not understood, which I understand why, but I don't know where to get the relationship name from as it is not listed on the user object.
The custom object is named Sales_Target__c, with a plural name of Sales_Targets__c.
The ownerID field is the standard field that is created on a custom object.

Many thanks
Raj VakatiRaj Vakati
try this .. you need to __r in relations 
 
List<User> owners = [SELECT ID, Name, (SELECT ID, Name, OwnerID, Closed_Amount__c, Month_Number__c FROM Sales_Target__r)          FROM User
WHERE OwnerID IN : ownerIDs];

 
Alain CabonAlain Cabon
The existing Relationship Names can be found with this code in the Anonymous Window (CTRL+E) (Developer console) but the children relationships on "owner Id" don't have relationship names (value "null"). They are implicit relationships but not queryable as children objects as you try to do.
Schema.DescribeSObjectResult R = User.SObjectType.getDescribe();
List<Schema.ChildRelationship> LC = R.getChildRelationships(); 
for (Schema.ChildRelationship C:LC) {
    if (C.getRelationshipName() != null) {
          system.debug('RelationshipName: '+ C.getRelationshipName()
          		+ '; Field: ' +  C.getField()  
          		+ '; ChildSObject: ' +  C.getChildSObject()           
       			+ '; isCascadeDelete: ' + C.isCascadeDelete()  
       			+ '; isRestrictedDelete: ' +   C.isRestrictedDelete());
    }  
}

On the other hand, you can get all the Child SObject by filtering if (C.getField().getDescribe().getName() == 'OwnerId') {
 
Schema.DescribeSObjectResult R = User.SObjectType.getDescribe();
List<Schema.ChildRelationship> LC = R.getChildRelationships(); 
for (Schema.ChildRelationship C:LC) {
    if (C.getField().getDescribe().getName() == 'OwnerId') {
          system.debug('RelationshipName: '+ C.getRelationshipName()
          		+ '; Field: ' +  C.getField()  
          		+ '; ChildSObject: ' +  C.getChildSObject()           
       			+ '; isCascadeDelete: ' + C.isCascadeDelete()  
       			+ '; isRestrictedDelete: ' +   C.isRestrictedDelete());
    }  
}