I have table
UserPer which contains columns like
What I want to do is like this
Select uid1, (select name from user where id = up.uid1) from UserPer up
How do I achieve this using SOQL?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
Assuming uid1 is a custom field in the UserPer object (so is actually uid__c) you want to do this...
select Id, uid1__r.Name from UserPer
uid1__r represents the relationship between the two objects and (obviously) is specific to SOQL/apex.
It is a regular point of confusion. The __r doesn’t represent the custom field, that is __c. When you create object relationships in SF it automatically creates this ‘relationship pointer’ __r that you can reference to reach across into the related parent record. The tricky bit is that you have to remember the conventions used for how you reference parent records vs child records.
Querying fields from child records is even weirder - it uses the __r as well, but because you are on the parent object and reaching out for the child records (note plural) the convention is that the relationship pointer is pluralized and it is the object name that is used as the relationship name i.e UserPers__r. And then the query is changed so that a looks like a subquery:
select Name, (Name from UserPers__r) from User
Note the pluralized UserPer object and the relationship suffix. If it were a standard object it would be pluralized but without the __r.