Dynamically adding a text value in picklist field

  • 2
  • Question
  • Updated 2 months ago
  • In Progress
I want to dynamically add a text value in my existing picklist and save that value so that it is visible fro everyone. Can someone suggest me what should I do?

I have a text field PassingValues and a picklist field Preferresd Category.The user should be able to add a new value in Picklist and the value should be saved and visible to everyone.
Photo of Ankita

Ankita

  • 100 Points 100 badge 2x thumb

Posted 3 months ago

  • 2
Photo of Matt Brown

Matt Brown, Official Rep

  • 950 Points 500 badge 2x thumb
Hi Ankita, 

If you save a value to a picklist field via an action or by overriding the metadata for that field in the model, it will save it back as a new value by default. However, it will be listed as an Inactive Value in the Salesforce field settings. Setting it to active would have to be done via custom code, probably on the backend via Apex, since it's an admin setting and would probably have to run outside permissions. 

Does that help? Thanks!

Matt
Photo of Arne-Per Heurberg

Arne-Per Heurberg

  • 1,844 Points 1k badge 2x thumb
Hi Matt, are you thinking using MetadataAPI? What would be your suggestion? I have a use case for it. Thanks
Photo of Chandra V

Chandra V, Champion

  • 7,400 Points 5k badge 2x thumb
We done dynamic picklists - mostly when we need the picklist values to be based on more then one thing, so a standard Salesforce dependent picklist won't work because it isn't logical enough.

To do this, we create a snippet called picklistOption and on the field you want to adjust, change the render type to "Custom run a snippet" and select your picklistOption snippet.

Here is a sample picklist value changer.  This one is based on the user picking a Fruit Type, and the Color picklist (which has the render snippet on it) auto adjusts based on the type of fruit selected.

This is just an example.  :)  Our real ones have fields pulled off other models to drive logic.

var $ = skuid.$,
                    field = arguments[0],
                    value = skuid.utils.decodeHTML(arguments[1]);
                
                // Run the standard picklist renderer for the given mode
                skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);
                
                if (field.mode == 'edit') {
                    var select = field.element.find('select');
                    
                    if (select.length) {
                    
                        // Remove unwanted entries, we are removing all existing values and recreating them for each row
                        $.each(select.children('option'),function(){
                            if ($(this).val() !== value  && $(this).val() !== '--None--' && value !== '--None--') {
                                $(this).remove();
                            }
                        });
               
                          var fruitType = field.model.getFieldValue(field.row,'Type__c',true);
                
                        // Add new entries
                        if (value !== '--None--' && (value)) {
                            select.append($('<option value="">--None--</option>'));
                        }
                                if (fruitType == 'Apple') {
                                        select.append($('<option>Red</option>'));
                                        select.append($('<option>Green</option>'));
                                   
                                }
                                else if (fruitType == 'Orange') {
                                    select.append($('<option>Orange</option>'));
                                }
                                else if (fruitType == 'Banana') {
                                    select.append($('<option>Yellow</option>'));
                            select.append($('<option>Green</option>'));
                                }
                        else {
                            //add common picklist values here
                            select.append($('<option value="">--None--</option>'));
                        }
                    }
                }