2

I built a lightning component where multiple fields with type picklist needs to be shown in the UI. I added a javascript function to fetch the picklist values and also built a generic method where it can accept any picklist values of any objects. But I dont know how to pass the sobject param to javascript function for different fields with type picklist. I want all the picklist fields to be loaded on doInit.Any idea/help please?

Apex Controller:

@AuraEnabled
    public static List<String> fetchPicklistValues(){
        List<String> options = new List<String>();
        Schema.DescribeFieldResult fieldResult = Lead.LeadSource.getDescribe();
        List<Schema.PicklistEntry> picklistEntries = fieldResult.getPicklistValues();
        for(Schema.PicklistEntry pick : picklistEntries){
            options.add(pick.getLabel());
        }
        return options;
    }

Lightning Component:

<div>
  <ui:inputSelect class="slds-input" aura:id="InputSelection" value="{!v.Lead.LeadSource}" required="true"/>
  <ui:inputSelect class="slds-input" aura:id="InputSelection" value="{!v.Lead.Type__c}" required="true"/>
</div>

Javascript Controller:

doInit : function(component,event,helper){

        var action = component.get("c.fetchPicklistValues");
        var inputSelection = component.find("InputSelection");
        var opts = [];
        action.setCallback(this,function(a){
            for(var i = 0; i < a.getReturnValue().length; i++){
                opts.push({
                            "class" : "optionClass",
                            "label" :  a.getReturnValue()[i],
                            "value" :  a.getReturnValue()[i]
                            });
            }
            inputSelection.set("v.options",opts);
        });

        //adds the server-side action to the queue        
        $A.enqueueAction(action);
    }
  • 3
    Your question is too vast, if you add some code, it would help community members in answering. – Raul Feb 12 '17 at 13:25
  • @rahul-sharma Updated my post with code. Here in the above how to handle multiple picklist fields? I can change my apex method to hold sobj and field as params. From component to javascript controller how to pass the params for those two picklist fields? – ləːnə Feb 12 '17 at 13:55
  • we have to come up with your design problems...just first think about getting two picklist fields with only one apex action cal...we have to use map for thisl....then we can do generic one... – user533 Feb 19 '17 at 8:58

Your Answer

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

Browse other questions tagged or ask your own question.