Find the API Name from the Field Label

I recently had a use case where I was going to take a drop down value and use that to pull a value from a separate lookup object. I would have “Hourly” and my logic would return “Hourly__c” which I could then use dynamic SOQL and sObjects to do some magic. I did not want to use hardcoded values or a custom setting lookup table and I thought it would be easy to query the metadata…which turned out to be strangely harder than I thought it would be so here is a working solution:

There are a lot of examples of getting the Label from the API name but not really the other way around.

Leave a Reply

Your email address will not be published. Required fields are marked *