Free computer code on screen

Retrieve the values of Object and Field Labels using API Names.

This scenario frequently arises when you possess the API Name but require the current labels for your object or field, whether it’s custom or standard. While you usually have this information in advance, there are instances where it becomes necessary when dynamically composing your Apex.

Presented here is a straightforward method that takes API Names as input in the form of strings and returns the corresponding labels. Feel free to customize it according to your requirements:

public static void getLabelsfromAPINames(String ObjectApi, String FieldApi){
    if(!String.IsBlank(ObjectApi) && !String.IsBlank(FieldApi)){
      system.debug('Object : '+(String)Schema.getGlobalDescribe().get(ObjectApi).getDescribe().getLabel()+', Field : '+ (String)Schema.getGlobalDescribe().get(ObjectApi).getDescribe().fields.getMap().get(FieldApi).getDescribe().getLabel()+' \n';

// Usage
getLabelsfromAPINames('Account', 'Name');