+ Start a Discussion
Devendra NataniDevendra Natani 

Create Dynamic Picklists for Lightning App Builder Components

Hello,

We can add a datasource onto the attribute in the design resource, like this:
<design:attribute name="Name" datasource="value1,value2,value3" />

How can I use a dynamic datasource instead of hardcoding the values.

Thanks,

Devendra 

Rodney Farach 5Rodney Farach 5
I have the same problem and there isn't a lot of documentation about it. :(
Thanks,
Rodney
Rodney Farach 5Rodney Farach 5
Also I tried to put some public property in server side and set the datasource value like datasource="{!publicProperty}" but doesn't work.
Yash Mansingh 5Yash Mansingh 5
You have to bind an attribute using an APEX controller and then using a Component controller you can control the attributes. Example:
<aura:component controller="DisplayCaseController" implements="force:appHostable">
    <aura:attribute name="record" type="Case[]"/>
    <ui:inputNumber label="Case ID" aura:id="CaseID"/><br/><br/>
	<ui:button label="Get Case" press="{ !c.getCaseFromId }"/><br/><br/>
	<aura:iteration var="c" items="{!v.record}">
         <p>{!c.Subject} : {!c.Description} : {!c.Status}</p>
    </aura:iteration>
</aura:component>


APEX Controller
 
public class DisplayCaseController {

    @AuraEnabled
    public static Case getCaseFromId(Id caseID) {
        if(caseID == null) {
            return [SELECT ID, Subject, Description, STATUS from Case LIMIT 1];
        }
        
        List<Case> cases = [ SELECT Id, Subject, Description, Status from CASE where ID = :caseID ];
        
        if(cases.size() == 0) {
            return [SELECT ID, Subject, Description, STATUS from Case LIMIT 1];
        } else {
            return cases[0];
        }        
    }
    
    
}

Component Controller
({
	getCaseFromId : function(component) {
		var caseID = component.find("CaseID").get("v.value");
        var action = component.get("c.getCaseFromId");
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.record", response.getReturnValue());
            }
        });
		$A.enqueueAction(action);
	}
})



 
Peter Kamp1Peter Kamp1
I have tried this: datasource="{!v.ObjectProperty}" but that does not do it.

Hope someone can help.

We are talking about the attribute in the design resource: <design:attribute name="Name" datasource="{!v.ObjectProperty}" />
 
VarunCVarunC
Did you find any solution yet?
SenthilKumar mariappanSenthilKumar mariappan
Hi My example is working fine for picklist..

my component:

<aura:handler name="init" action="{!c.myAction}" value="{!this}" />
     <aura:attribute name="Network" type="String[]"/>
 <ui:inputSelect label="Network" class="slds-input"
labelClass="slds-form-element__label" >
            <aura:iteration items="{!v.Network}" var="s"> 
                <ui:inputSelectOption text="{!s}"/> 
            </aura:iteration>            
        </ui:inputSelect> 

my controller:
myAction : function(component, event) {
        
        var action = component.get("c.getPickListValues");
        action.setParams({ obj:"Deal_Request__c",str:"Network_Platform__c"});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var stringItems = response.getReturnValue();
                component.set("v.Network", stringItems); 
            }
        });
        $A.enqueueAction(action);
    } 

my class:

public class objlist_1_cmp_ctrl {

@AuraEnabled
public static List<String> getPickListValues(String obj, String str) {
String[] options = new String[]{}; 
//Schema.DescribeFieldResult ccity = Schema.getGlobalDescribe().get(str1).getDescribe().fields.getMap().get(str).getDescribe(); 
//Schema.DescribeFieldResult ccity = Schema.sObjectType.contact.fields.status__c.getSObjectField().getDescribe();
Schema.DescribeFieldResult plistvalues = Schema.getGlobalDescribe().get(obj).getDescribe().fields.getMap().get(str).getDescribe();
    
    options.add('--None--');
for(PicklistEntry ent:plistvalues.getpicklistvalues())
{
options.add(ent.getLabel());
}
return options;
}
  
}

Thanks,
Senthil Kumar
Tatiana CarpiucTatiana Carpiuc
Hi,
I creaded a dynamic picklist in design resources and it works for me. This is my code:

- design file:
<design:component>
    <design:attribute name="sObjectName" label="Object Name" datasource="apex://DesignResourcePickList" description="" />   
</design:component>

- apex class: 

global class DesignResourcePickList extends VisualEditor.DynamicPickList { 
    global override VisualEditor.DataRow getDefaultValue(){
        VisualEditor.DataRow defaultValue = new VisualEditor.DataRow('Tree', 'Tree__c');
        return defaultValue;
    }
    global override VisualEditor.DynamicPickListRows getValues() {
        List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();        
        VisualEditor.DynamicPickListRows  myValues = new VisualEditor.DynamicPickListRows();

        for(Schema.SObjectType objectInstance : gd){
            myValues.addRow(new VisualEditor.DataRow(objectInstance.getDescribe().getLabel(), objectInstance.getDescribe().getName()));
        }
        return myValues;
    }
}

The documentation I used is: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_config_for_app_builder_dynamic_picklists.htm
Avish Samdani 5Avish Samdani 5
Hi,

for dynamic datasource instead of hardcoding the values please refer this url.

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_config_for_app_builder_dynamic_picklists.htm

Thanks,
Avish