8

Let's say I have two picklists bound to force:inputField elements like so:

<force:inputField value='{!v.obj.Database__c}' aura:id='database'/>

<force:inputField value='{!v.obj.Database_Version__c}' aura:id='databaseVersion'/>

The databaseVersion field is dependent on the database field. Regardless of the value present in the database field, databaseVersion is always rendered as disabled. If I do manually enable it,

(e.g. component.find('databaseVersion').get('v.body')[0].set('v.disabled', false); <-- don't know if that's the proper way?)

all of the possible picklist values are present. It doesn't respect the value in the database field.

Is there a supported way to do this using a pair of force:inputField elements at the moment? Any insight is appreciated!

6

Oh wow, perfect timing! <force:inputField> did not even support 'standalone' picklists as of 3 days ago. Salesforce only fixed it recently. Out of curiosity, are you trying this on a Spring '17 prerelease org, or on a production Winter '17 one?

Short answer: no I don't think you can do this with <force:inputField>. Yesterday I tested a very similar scenario and I did not get it working. I found exactly the same issues that you found.

If you're willing to work a bit harder to achieve that functionality, you can do all the wiring and databinding yourself. To make it harder, Salesforce does not expose this information via Apex, so you have to go through workarounds. There are some good answers here.

You can do this with the following elements:

  • A lightning component with 2 <ui:inputSelect> elements to store the picklists. An event handler captures the "change" event in the master picklist and calls a javascript controller to update the dependent picklist.

<aura:component implements="flexipage:availableForAllPageTypes" controller="DependentPicklistController" access="global"> <aura:attribute name="masterValue" type="String" access="global"/> <aura:attribute name="dependantValue" type="String" access="global"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <aura:handler name="change" value="{!v.masterValue}" action="{!c.updateDependentPicklist}"/>
<ui:inputSelect aura:id="Master" label="Master" value="{!v.masterValue}"/> <ui:inputSelect aura:id="Slave" label="Dependant" value="{!v.dependantValue}"/> </aura:component>

  • A lightning controller with the function that handles the event above, and calls a server-side Apex controller to retrieve the values.

({

    doInit : function(cmp) {        
        var action = cmp.get("c.getMasterPicklistValues");
        action.setCallback(this, function(response) {
            if(cmp.isValid() && response.getState() === "SUCCESS") {
                var options = response.getReturnValue();
                console.log (options);
                var picklistOptions = [];
                for (i= 0; i < options.length ; i++)
                {
                    picklistOptions[i] = { label: options[i].Label, value: options[i].Value};
                }
                cmp.find("Master").set("v.options", picklistOptions);
            }
        });
        $A.enqueueAction(action);
    },

    updateDependentPicklist: function (cmp) {
        var masterValue = cmp.get("v.masterValue");
        var action2 = cmp.get("c.getDependentPicklistValues");
        action2.setParams({ "masterValue" : masterValue });
        action2.setCallback(this, function(response) {
            if(cmp.isValid() && response.getState() === "SUCCESS") {
                var options = response.getReturnValue();
                console.log (options);
                var picklistOptions = [];
                for (i= 0; i < options.length ; i++)
                {
                    picklistOptions[i] = { label: options[i].Label, value: options[i].Value};
                }
                cmp.find("Slave").set("v.options", picklistOptions);
            }
        });
        $A.enqueueAction(action2);
    }
})
  • An Apex controller that can give you all the possible values for a dependent picklist based on the value of its master picklist. Unfortunately, I cannot post the code, as it's used in production, but I took inspiration from the latest post in this thread. Note that you can use EncodingUtil for Base64 decoding instead of their handmade solution.

It's a very time-consuming solution for something that should work out-of-the-box, but it's achievable.

PS: if you can, please support this idea to build all the metadata information into Apex: https://success.salesforce.com/ideaView?id=08730000000h1y6AAA

  • Thanks for the concise answer here! The timing is no coincidence as I've been following the releases quite closely haha.. To answer your question, I am doing this in a Spring '17 prerelease org. I figured this was my only option, for the time being at least. And you've got my upvote – Antonio Manente Jan 10 '17 at 21:23
  • 1
    Haha, that makes sense now. Thanks for the upote and glad I could help! – mkorman Jan 10 '17 at 22:59

protected by Community May 30 '18 at 14:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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