5

I have a custom object (Team_Assignments__c) with a lookup relation (Team_Member__c) that references the User object. Team_Member__c should be the name of a user. However, my SOQL query for the Team_Member_c returns gibberish (ex: 0051a000000bSMsAAM). I was wondering why this is and how I could fix this?

VF Page:

<apex:pageBlock mode="maindetail">
    <apex:pageBlockSection columns="1">
         <apex:pageBlockTable value="{!team}" var="user">
            <apex:column >                 
                <apex:outputText value="{!user.Team_Member__c}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:pageBlock> 

SOQL Statement:

Select Id, Name, Team_Member__c 
From Team_Assignment__c]
1
  • Team_Member__c is a lookup field and will store Id of the user linked not name.
    – San
    Jul 1, 2015 at 19:04

3 Answers 3

4

Replacing:

<apex:outputText value="{!user.Team_Member__c}"/>

with:

<apex:outputField value="{!user.Team_Member__c}"/>

will render the names of the objects that are referenced and also links to those objects without the query needing changing. The apex:inputField and apex:outputField components embody common platform patterns (such as this one) and so are often best to try first.

5
  • I thought that too but I experimented through the UI and still got just the Id still.
    – Adrian Larson
    Jul 1, 2015 at 18:45
  • @AdrianLarson I recollect getting the ID value in the past but at least in the simple apex:pageBlockTable test code I tried today it worked OK. API version dependent maybe?
    – Keith C
    Jul 1, 2015 at 18:50
  • Ah didn't try it in a pageBlockTable. It doesn't work if you just dump the outputField on the page.
    – Adrian Larson
    Jul 1, 2015 at 18:55
  • I guess I should do some more testing on where this works and where it doesn't.
    – Adrian Larson
    Jul 1, 2015 at 18:57
  • 1
    @AdrianLarson The good thing is you are aware of both patterns so if apex:outputField doesn't work you have the apex:outputLink pattern at your fingertips.
    – Keith C
    Jul 1, 2015 at 19:27
2

It's not gibberish, it's an Id, which is the type of data Salesforce stores for all Lookup or Master Detail relationships. If you want a linked name, you will need something more like the below. I would change the var to assignment to be less confusing.

You can actually just switch from outputText to outputField because you are using a reference Id. But if you are doing this using a record's own Id, you may need to construct the linked name yourself, which would look like:

<apex:outputLink value="{!$Action.User.View, user.Id}">
    {!user.Name}
</apex:outputLink>
7
  • -1 Sorry, Adrian, but you never need to use that awful construct if you bind to a lookup field directly. See this code: public class oneaccount { public Contact getContact() { return [SELECT AccountId FROM Contact WHERE AccountId != NULL LIMIT 1]; } } Page: <apex:page controller="oneaccount"> <apex:outputField value="{!contact.accountid}" /> </apex:page>.
    – sfdcfox
    Jul 1, 2015 at 19:15
  • I guess maybe it's just when you're using Id, which is what I tested and why I changed my initial answer away from that.
    – Adrian Larson
    Jul 1, 2015 at 19:16
  • Not saying I disagree with the -1 (should have just left my original answer) but it's not that horrific, it's only one tag with one inner text element.
    – Adrian Larson
    Jul 1, 2015 at 19:27
  • My primary problem with this answer, the reason for -1, is you state something that's absolutely false (that you can use outputField because you're in a pageBlockTable). You can, in fact, use apex:outputField anywhere that you're using a real lookup field, and have it render correctly. In fact, I'd like proof that there's a place where it doesn't work, because I've never seen it fail.
    – sfdcfox
    Jul 1, 2015 at 19:46
  • <apex:page standardController="Account"><apex:outputField value="{!Account.Id}" /></apex:page>
    – Adrian Larson
    Jul 1, 2015 at 20:04
0

Team_Member__c will only return a reference id to that record. To access the fields such as Name properly you will want to display Team_Member__r.Name.

This will delve into the record itself to return a value and not just the id on the Team_Assignment__c object

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .