2

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.

  • Why would the translation require multiple if-else statements? You could create a Custom Label with "---None---" in it and reference that label just once. Add as many translations to that label as you need. – Charles T Jul 22 '18 at 3:20
  • Yes Charles, I had done this Yesterday. Thanks for your response. – Narendra Nimmana Jul 22 '18 at 13:01
1

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

| improve this answer | |
  • Thanks Harmer for your time, I have couple of questions 1) How does default value effects in the lightning. 2) If i add '--None--' to picklist values, the label and value would be '--None--' instead of only Label. But we need value as blank. How can it be achieved? – Narendra Nimmana Jul 21 '18 at 8:58
  • Ok, this is a trick, but you can use a zero-width space as the api name of the ---None--- field... get the space here: codepen.io/chriscoyier/pen/iLKwm... I've updated the question. – Caspar Harmer Jul 21 '18 at 22:28

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.