0

Is it possible to get sObjectDescribe in Lightning Components? In my case I need to have all possible values of picklist-field, but filter out the list of options conditionally.

2

You can do the describe call in the apex class and filter out the value as you want and return those value to lightning controller:

Adding a snap of the apex class method and lightning controller:

Add the following in the Apex class:

public class picklistWrapper {
    @AuraEnabled
    public String strValue {get;set;}
    @AuraEnabled
    public String strLabel {get;set;}

    public picklistWrapper(String strValue, String strLabel) {
        this.strValue = strValue;
        this.strLabel = strLabel;
    }
}

@AuraEnabled
public static List<picklistWrapper> getPicklistValues() {

    List<picklistWrapper> lstPWrapper = new List<picklistWrapper>();
    Schema.DescribeFieldResult fieldResult = Account.BillingStateCode.getDescribe();

    for(Schema.PicklistEntry f : fieldResult.getPicklistValues()) {

        //write your logic to filter the values here
        lstPWrapper.add(new picklistWrapper(f.getValue(), f.getLabel()));
    }
    return lstPWrapper
}

Add the following to the lightning controller:

getInitialData : function(component, event, helper) {
    var action = component.get("c.getPicklistValues");

    action.setCallback(this, function(response){
        var state = response.getState();
        if(state === "SUCCESS") {
            var result = response.getReturnValue();
            console.log('=====result====',result);//you will get picklist values here
        }
    })
    $A.enqueueAction(action);
}
  • I definitely can, but want to simplify it and do not perform a call to server for this data – Yury Bondarau Jun 22 '17 at 7:57
  • I don't think there is a way to do that in the lightning component itself – SFDC Learner Jun 22 '17 at 8:02
  • Currently you must fetch this metadata via apex. I suggest you use a storable action though. It's otherwise not available on the client. – Kevin Venkiteswaran Jun 23 '17 at 15:30

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.