function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jennifer Dos Reis ICOJennifer Dos Reis ICO 

What is the child Relationship (of the owner relationship) from Users to a custom Object ?

I would like to query all users with their related list of a custom object (By the owner relationship). Because I would like to count how many records belongs to each user. For example:
 
[ SELECT Name, (SELECT Count() FROM relationshipName) FROM CustomObject ]

But I don't know which is the relationshipName that i should use. I don't want to create a new lookup field, I would like to use the owner relationship for this. Is it possible ? If not, is there a better way to achieve my purpose?  
Becka DBecka D
'OwnerId' should be what you need.
Jennifer Dos Reis ICOJennifer Dos Reis ICO

Sorry I made a mistake in the code example. It Should be like this : 

[SELECT id, (SELECT Count() FROM relationshipName)
 FROM User 
 WHERE Role = 'Ejecutivo aprobador' ];

I have tried 

[SELECT id, (SELECT id FROM Preaperturas__r)
 FROM User 
 WHERE Role = 'Ejecutivo aprobador' ];


And

[SELECT id, (SELECT id FROM ownerid)
 FROM User 
 WHERE Role = 'Ejecutivo aprobador' ];
But neither one worked. It says to me: "Didn't understand relationship 'ownerid' in FROM part of query call."
GauravGargGauravGarg

Hi Jennifer,

Relationship Name is a api name that will uniquely define the Relationship between two objects. 

For Eg, Account s parent of Contact, then Contact must have one lookup on Account object which can be directly accessible as accountId in Contact query. 
But if we need to query Contact from Account object, we need RelationshipName. 

To check the relationship between two object, please follow below steps:
1.Setup --> User object
2. Open custom / standard lookup relationship field.
3. Here you find "Child Relationship Name". (attached screenshot)
User-added image

Please use this value in your query. 

For more info on Relationship, please read this article. https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_understanding.htm

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.

If you still face issues, do let me know or contact me on below details.

Thanks,

Gaurav
Email: gauravgarg.nmims@gmail.com

Jennifer Dos Reis ICOJennifer Dos Reis ICO

Thanks @GauravGarg. If I go for example to the Contact fields, in the lookup field of the Account I can see that the relationship name is Contacts. (This is a standard lookup field). 

User-added image

But if I go to the owner lookup field I get this:

User-added image

There is not the relationship name that I need.  

John DshaelyJohn Dshaely
is there any abusive relationships (https://isittrue.com/) example you got so far. I need it too. BTW good work its working fine on my side.