[SalesForce] Translation for ‘–None–‘ value in lightning

I am fetching the Labels & Values of a Picklist by using the following method

public static Map<String,String> getPickListValuesMap(string objName, string fieldName){

    Schema.SObjectType objType = Schema.getGlobalDescribe().get(objName);
    Map<String,String> valuesLabels=new Map<String,String>();

    Map<String, Schema.SObjectField> fieldMap = objType.getDescribe().fields.getMap();
    List<Schema.PicklistEntry> values = fieldMap.get(fieldName).getDescribe().getPicklistValues();
    for(Schema.PicklistEntry ple : values){
        valuesLabels.put(ple.getLabel(), ple.getValue());

    }
    return valuesLabels;
}

As we know getLabel() returns the Label of the picklist value based on user's language. Here my problem is, Schema.PicklistEntry not retuning any label for '–None–'(Neither in English nor in any other language). So i have added '–None–' to the list & displayed on the UI.

So Currently i am doing the translation for '–None–' by putting multiple if else statements in controller.js depending on the current User's language. As the languages are getting added this approach looks weird. So please suggest a better approach.

Best Answer

Update, if you need the field to look blank, you can use a zero width space as the "empty" value:

  • Get the character on your clipboard here
  • In Step 1 below, modify the "API Name" of the picklist value (you'll see it in saved picklist values on the field). Paste the zero width space in as the API name.
  • In Step 2 below, paste that same zero width space into the default value of the field.

This is a hack, technically, but the field LOOKS empty.

Original

Try adding ---None--- as the first picklist value in the actual list of values and then make the first value the default one.

Then you can add all the translations you need.

Step 1: Adding new picklist value

Step 2: Adding default value

Related Topic