I'm a new developer and would appreciate any help with this question.

I am trying to pre-populate a lightning:select field based on a variable in the URL.

I am able to dynamically generate the list of options for the field, but I can't seem to get it to populate with the variable from the URL.

I am using a wrapper class to get both the list of options and my chosen option in the same call - class adapated from here: http://sfdcmonkey.com/2017/07/06/use-wrapper-class-lightning-component/

Here is my code:

Lightning Component:

    <aura:attribute name="variableURL"            type="String" default="Nothing"/>
    <aura:attribute name="srvList"                type="Object"/> 

    <aura:handler name="init"    value="{!this}"   action="{!c.onInit}" /> 

    <fieldset class="slds-box slds-m-top_small ">
        <legend class="slds-text-heading_medium">Provide us with some extra details</legend>

        <div class="slds-form-element__row">
            <div class="slds-form-element slds-size__2-of-2 slds-medium-size__1-of-2">
                <lightning:select aura:id="formField" name="formField" label="Service Area" required="true" value="{!v.variableURL}">
                    <option value="">- Not Specified -</option>
                    <aura:iteration items="{!v.srvList.lstServices}" var="ans">
                        <option value="{!ans.Name}" text="{!ans.Name}" selected="{!ans.Name == v.srvList.selectServ.Name}"></option>




    onInit : function(component, event, helper) {

       var action = component.get("c.initMethod");
       var variableURL = component.get("v.variableURL");

           variableURL : variableURL

       action.setCallback(this, function(a) {

         var state = a.getState();
         console.log("state: ",state);

         if (state === 'SUCCESS'){

             //set response value in wrapperList attribute on component.
             var response = a.getReturnValue();
             console.log("response: ",response);






Apex Class:

public class SADC_Contact_Us {

           public static wrapperClass initMethod(String variableURL){
             //create a wrapper class object and set the wrapper class @AuraEnabled properties and return it to the lightning component.
               wrapperClass returnwrapperClass = new  wrapperClass ();        
               returnwrapperClass.lstServices = [SELECT Name from Website_Service__c WHERE Service_Level__c =1 LIMIT 25];
                returnwrapperClass.selectServ = [SELECT Name from Website_Service__c WHERE Service_Level__c =1 AND Name=:variableURL LIMIT 1];
              return returnwrapperClass;    

       //wrapper or Inner class with @AuraEnabled {get;set;} properties*    
        public class wrapperClass{
                @AuraEnabled public List<Website_Service__c> lstServices{get;set;}
                @AuraEnabled public List<Website_Service__c> selectServ {get;set;}

So the aura:iteration on items="{!v.srvList.lstServices}" works fine, but this part does not load anything:

selected="{!ans.Name == v.srvList.selectServ.Name}

Can anyone advise how to get this selected part working?


That is because you are assigning List<Website_Service__c> to selectServ and when you try to get value on it like selectServ.Name, it will be undefined. You should rather get it from a particular record. I see that you are are fetching single element LIMIT 1. You can use below:

selected="{!ans.Name == v.srvList.selectServ[0].Name}

This will fetch the Name of 1st element (0th index).

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.