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
cmarzillicmarzilli 

Accessing a field name or label

I am trying to write some trigger code and I need to access the field names of some of our custom fields.  I have a custom field named "Users" on the account level, and I want to create a string that contains the actual name of the field if it changed. However I don't know how to access a field's name (custom or otherwise).  I tried Trigger.new.Users__C.toLabel But this did not work.  Is this possible?
 
Here is my trigger code:
 

if (Trigger.old.Users__C != Trigger.new.Users__C)

{

String whatChanged = 'Changed '+ Trigger.new.Users__C.toLabel +' from '+ Trigger.old.Users__C + ' to ' + Trigger.new.Users__C;

}

 

mtbclimbermtbclimber
You can not access describe information from Apex Code currently.

If you want to present an error to the user you could add an error in this fashion which will show in the expected location on the edit page:

Trigger.new.Users__c.addError('You can't change the value for this field.');
CodeTalkerCodeTalker
I noticed the date of this post and was wondering...

Are we now able to access field names/labels in apex code?

I want to create a visual force page that mimics the views page.
I  want to put the field names into a select box. If the field name is selected
then I can pass it to a method in the page's controller to dynamically create
the where part of the query.

Thank you in advance!