I use the SOAP API (WSC, partner wsdl) to describe objects and use the field meta information to dynamically build SOQL queries. Today i observed that DescribeSObjectResult can contain fields of type reference but with an unknown (empty) relationship name.
These reference fields are not usable to build SOQL queries because of the missing relationship name that must be used in a field path.
For Example the description of the User object contains a reference field ‘AccountId’ that contains the correct referenceTo property (‘Account’) but an empty relationshipName.
Why this can happen?
I can imaging that these fields are only used/enabled in some specific cases? The User.AccountId field seems to be only used for Customer Portal users (when communities are enabled in an organization).
A complete list of fields (describeGlobal, developer org) that are of type reference but with an empty relationship name:
User -> DelegatedApproverId User -> AccountId User -> CallCenterId ContentDocument -> ArchivedById ContentDocument -> ParentId FiscalYearSettings -> PeriodId LoginHistory -> UserId Group -> RelatedId CaseShare -> UserOrGroupId RecordType -> BusinessProcessId Campaign -> CampaignMemberRecordTypeId WebLink -> ScontrolId DocumentAttachmentMap -> ParentId DocumentAttachmentMap -> DocumentId Task -> RecurrenceActivityId ContentVersion -> ContentModifiedById CategoryNode -> ParentId ContentWorkspace -> DefaultRecordTypeId LeadShare -> UserOrGroupId AccountPartner -> ReversePartnerId CronTrigger -> OwnerId OpportunityShare -> UserOrGroupId CampaignMemberStatus -> CampaignId ApexTestQueueItem -> ParentJobId ForecastShare -> UserRoleId ForecastShare -> UserOrGroupId UserRecordAccess -> UserId AsyncApexJob -> ParentJobId SetupEntityAccess -> SetupEntityId IdeaComment -> CommunityId UserRole -> ParentRoleId UserRole -> ForecastUserId UserRole -> PortalAccountId UserRole -> PortalAccountOwnerId CaseTeamTemplateMember -> TeamTemplateId CaseTeamTemplateMember -> MemberId CaseTeamTemplateMember -> TeamRoleId AuthProvider -> RegistrationHandlerId AuthProvider -> ExecutionUserId AdditionalNumber -> CallCenterId Event -> RecurrenceActivityId OpportunityPartner -> ReversePartnerId CampaignShare -> UserOrGroupId ContactShare -> UserOrGroupId EmailTemplate -> BrandTemplateId EmailServicesAddress -> RunAsUserId Report -> OwnerId CategoryData -> CategoryNodeId CategoryData -> RelatedSobjectId Partner -> ReversePartnerId AccountShare -> UserOrGroupId UserPreference -> UserId GroupMember -> UserOrGroupId SelfServiceUser -> ContactId EmailServicesFunction -> ApexClassId
I suspect that you can’t query these particular relationships with SOQL. You can see them and the related entity Id. However, you can’t query directly across the join in SOQL.
This is a rather unsatisfactory answer and I’d be more than happy to be proven wrong on this.
I did find some documentation that hinted at this limitation:
…not all parent-child relationships are exposed in SOQL, so to be sure you can query on a parent-child relationship by issuing the appropriate describe call
Source: Identifying Parent and Child Relationships
With custom fields you typically use the
__r suffix to identify the relationship rather than the
__c of a custom field. However, this doesn’t apply for inbuilt fields like