0

I have a requirement where I am fetching fields of SObject and displaying them on UI. I want to change the label of ID to ID(Master Record). Please help me how to do it. Below I'm attaching my UI snip and apex code.

enter image description here

.CONTROLLER

Map<String, Schema.SObjectField> objectFields = new Map<String, Schema.SObjectField>();
        List<String> fieldNames = new List<String>();
        Schema.SObjectType convertType = Schema.getGlobalDescribe().get(ObjectName);
        Schema.DescribeSObjectResult r = convertType.getDescribe();    
        objectFields = Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap();
        if(objectName == 'Account' || objectName == 'Opportunity' || objectName == 'Case'){
            for(String fieldName: objectFields.keySet()){
                       fieldNames.add(objectFields.get(fieldName).getDescribe().getName());
            } 
        }
1

Unfortunately, you cannot change Labels for standard fields, but I guess you can create a Map<String, String> and use it instead

Map<String, String> standardFieldMap = new Map<String, String> {
'Id' => 'Id (Master Record)'
};
Map<String, String> fieldLabelMap = new Map<String, String>();

Map<String, Schema.SObjectField> objectFields = new Map<String, Schema.SObjectField>();
        List<String> fieldNames = new List<String>();
        Schema.SObjectType convertType = Schema.getGlobalDescribe().get(ObjectName);
        Schema.DescribeSObjectResult r = convertType.getDescribe();    
        objectFields = Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap();
        if(objectName == 'Account' || objectName == 'Opportunity' || objectName == 'Case'){
            for(String fieldName: objectFields.keySet()){
                       String fieldLabel = objectFields.get(fieldName).getDescribe().getLabel();
                       if (standardFieldMap.containsKey(fieldLabel)) {
                          fieldLabel = standardFieldMap.containsKey(fieldLabel);
                       }
                       String fieldName = objectFields.get(fieldName).getDescribe().getName();
                       fieldLabelMap.put(fieldName, fieldLabel);
                       fieldNames.add(fieldName);
            } 
        }
2

First of all, if you do a .getName() that will give you the Name (api) of the field, not the label.

As you can see in your screenshot, you get things like LastName instead of Last Name (with a space)

If you want the real label, you should be using .getLabel() in combination with the .getName() (to be able to map the field to its label).

If you use the standard capabilities of the framework to display the labels based on the api name, and you get things like Account ID and want to change it to something else, just follow the path

Setup -> User Interface -> Rename Tabs and Labels -> (Edit) Accounts -> Next

Then search for Account ID, change its value to ID (Master Record) and Save

In theory, the .getLabel() should return the correct value now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.