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
Platy ITPlaty IT 

Schema.DescribeFieldResult getLabel() for Reference Fields

When I use the getLabel method of a Schema.DescribeFieldResult object (quick example below), it works fine for every field except Reference fields.  For a Reference field, it's returning the Label of that reference's Id field (e.g. Account Id or Owner Id) instead of the label actually used for that Lookup/Master-Detail field (e.g. Account Name or Owner).  Anyone know a workaround for this?

 

String Label = DescField.getLabel();

jbroquistjbroquist

I just tested it to confirm on one of my dev orgs and it appears the getLabel() method is returning the proper label for my master-detail field. Can you perhaps post a code snippet so we can better diagnose your results?

 

Platy ITPlaty IT

Well that gives me hope at least.  I've boiled this down to it's simplest format (my original code has a lot more Ifs and variables involved), but even these 4 lines alone result in the same issue for me- 

Map<String, Schema.SObjectType> GlobalMap = Schema.getGlobalDescribe();
Schema.DescribeSObjectResult obj = GlobalMap.get('Contact').getDescribe();
Schema.DescribeFieldResult TestField = obj.Fields.getMap().get('AccountId').getDescribe();
System.debug('TestField.getLabel()-------------------------------' +TestField.getLabel());

 The results of that debug-

USER_DEBUG|[21]|DEBUG|TestField.getLabel()-------------------------------Account ID

My code is being called from an extension class, but I tried putting that in a trigger to see if that makes any difference and it doesn't.  I also tried to changing the API version of the class (25, 26 and 27), all the same results.  I must be missing something if it works for you though.

jbroquistjbroquist

No now that I see you're trying to do this with the Contact object your results make more sense. In my original test, I was testing with custom objects. After re-testing using the Contact object like in your example, I reproduced the same results as you. It would appear that this is a "bug" with their pre-defined master-detail relationships.

 

I would submit a support ticket to them to find out if this is the intended result with those field types.

Platy ITPlaty IT

Thanks for the sanity check jbroquist.  I just put in a Case through the partner portal and hopefully they don't just automatically close it because they don't support custom code :)

Platy ITPlaty IT

Support has informed me that this is the expected behavior of getLabel for Reference fields.  Certainly not expected by me. I'm searching to find if the actual label of that Reference field is anywhere I can access in the metadata but not having much luck so far.  What's maddening is that it works fine for custom reference fields, it's just standard reference fields (like Contact.AccountId or Account.CreatedBy) this happens to.  Expected or not, that kind of inconsistency seems just wrong to me.

 

If anyone has ever figured this out, I would much appreciate some help- I'll post here if I find the solution.

Platy ITPlaty IT

I am not a happy camper about this.  I put in one case labelling this as a bug and was told no, it's expected behavior.  So I put in another request saying that if this is expected behavior then the documentation for the getLabel method is erroneous because it states "Returns the text label that is displayed next to the field in the Salesforce user interface".  Salesforce support confirms that yes, for standard reference fields that is not what's returned by getLabel but closed the case anyway because I don't have premier support.  I don't expect support for my own code, but you'd think they'd at least fix BUGs in Salesforce?

 

So I've wasted a lot of time creating a workaround for what clearly is a BUG.  Hopefully someone with more clout than I will get stuck dealing with this and actually get support to deal with it.  In the meantime, if anyone else discovers there's no way to access the Label of a standard Reference field in Apex, here's a hacky Visualforce workaround.

 

Quick summary of what this is a workaround for- getting the Label for a standard Reference field (i.e. Contact.AccountId or Account.LastModifiedBy).  The getLabel method of DescribeFieldResult does not work correcly for these fields.  Unfortunately, this is only a workaround for Visualforce, I haven't found one yet for Apex though I'd welcome one because this solution has serious limitations (and it's a ridiculous way to accomplish it).  This really is only needed if you need the Label without a full inputField or outputField, otherwise you can just use those to display the label and field together in a pageBlockSection-

 

<!-- These styles hide the field part of the outputField, so all that's displayed is the Label.  It also removes some of the formatting that comes with the field -->

	<style>

	    div.hideField td.data2Col { display:none; }
		div.hideField td.dataCol { display:none; }
		div.hideField td.labelCol { padding-top:none;font-size:11px;padding-right:0px; }
		div.hideField div.pbSubsection { height:auto;margin-top:0px;padding-right:0px; }
		
		div.hideField {text-align:right;}
	</style>

	<!-- Bizarro workaround, replace "sobj" with your SObject record variable and "fieldname" with the API name of the field-->

    <apex:outputPanel layout="block" styleClass="hideField">
     <apex:pageBlockSection columns="1" collapsible="false" showHeader="false">
     	<apex:outputField value="{!sobj['fieldname']}"/>
     </apex:pageBlockSection>
    </apex:outputPanel>

 

 

 

 

GeekyAkshayGeekyAkshay
This is 2020, Still no workaround for this issue (or expected behavior as per salesforce)?