0

So I get the error "component.find.(...).get is not a function". Through research I found out that this is because iteration creates multiple instances of aura:id. I've tried to use event.getSource() but the event is in accordion and the values are in iteration. How do i fix this?

Aura

<aura:attribute name="searchResult" type="List" description="use for store and display account list return from server"/>
<aura:attribute name="searchString" type="String" description="use for store user search input"/>     
<aura:attribute name="Message" type="boolean" default="false" description="use for display no record found message"/>     
<aura:attribute name="id" type="object"/>

<aura:attribute name="isDisabled1" type="Boolean" default="true"/>
<aura:attribute name="isDisabled2" type="Boolean" default="true"/>
<aura:attribute name="isDisabled3" type="Boolean" default="true"/>
<aura:attribute name="isDisabled4" type="Boolean" default="true"/>
<aura:attribute name="isDisabled5" type="Boolean" default="true"/>

  <div onkeyup="{! c.handleKeyUp }">
        <lightning:input
            value="{!v.searchString}"
            aura:id="searchField"
            placeholder="Search Property Name"
            required="true"
            type="search"
        />
    </div>

<table class="slds-table slds-table_bordered slds-table_striped slds-table_fixed-layout">

       <lightning:accordion activeSectionName="A" onsectiontoggle="{!c.grayButtons}">
                <aura:iteration items="{!v.searchResult}" var="property">  
                 <lightning:accordionSection name="propertyName" label="{!property.Name}">
        <!--section body-->
                  <tr>
                    <td>
                        <lightning:button
                            aura:id="ten"
                            label="10%"
                            iconName="utility:open"
                            iconPosition="left"
                            variant="neutral"
                            type="submit"
                            onclick="{!c.ten}"
                            value="{!property.Name}"
                            disabled="{!v.isDisabled1}"
                          /></td>
</tr>
</lightning:accordionsection>
</lightning:accordion>
</table>

Controller

handleKeyUp: function (component, event, helper) 
    {
        var isEnterKey = event.keyCode === 13;
        if (isEnterKey) 
        {  
        var searchField = component.find('searchField');
        var isValueMissing = searchField.get('v.validity').valueMissing;
        // if value is missing show error message and focus on field

            if(isValueMissing) 
              { 
                searchField.showHelpMessageIfInvalid();
                searchField.focus();
              }
            else
              {
                // else call helper function 
                helper.SearchHelper(component, event);

              }
        } 
}, 

 grayButtons: function(component, event, helper)
    {
        helper.ten(component, event);
        helper.twentyFive(component,event);
        helper.fiftyBelow(component, event);
        helper.fiftyAbove(component, event);
        helper.All(component,event);
    },

Helper

 SearchHelper: function(component, event) 
    {
        var action = component.get('c.searchName');
        action.setParams({'searchString': component.get("v.searchString")});
        // Set up the callback
       action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
                // if storeResponse size is 0 ,display no record found message on screen.
                if (storeResponse.length == 0) {
                    component.set("v.Message", true);
                } else {
                    component.set("v.Message", false);
                }

                // set searchResult list with return value from server.
                component.set("v.searchResult", storeResponse);
            }
        });
        $A.enqueueAction(action);       
      },

 ten: function(component, event)
    {
        var action = component.get("c.tenPercent");
        var myId = component.find("ten").get("v.value");
        action.setParams({"ten" : myId});
        action.setCallback(this, function(response)
         {
           var state = response.getState();
           if(state === "SUCCESS")              
           {
            var storeResponse = response.getReturnValue();
               if (response.getReturnValue().length != 0) {
                    component.set("v.isDisabled1", "false");
                } else {
                    component.set("v.isDisabled1", "true");

                }

            }
       });
        $A.enqueueAction(action);       
      },

Apex

@AuraEnabled   
        public static List <Property__c> searchName(String searchString) 
        {
            String searching = searchString + '%';
            String var = '012d0000000Szws';
                List <Property__c> returnList = new List <Property__c> ();
                List <Property__c> lstOfProperties = [SELECT name, id FROM Property__c 
Where Name LIKE: searching order by Name LIMIT 10];

           for (Property__c acc: lstOfProperties) 
           {
             returnList.add(acc);
           }
             return returnList;     
        }
public static List <Opportunity> tenPercent(String ten) 
        {
            List <opportunity> hipdeals = new List<opportunity>();  
            String var = '012d0000000Szws';
            String otherattr = ten;
            hipdeals = [select name, Id from opportunity where name=:otherattr];
            return hipdeals;
        }
  • Have you tried: let theSearchString = component.get("v.searchString"); and then passing the parameter action.setParams({'searchString': theSearchString}); ? – Carlos Naranjo Aug 18 at 19:53
  • Deleted my answer as requirement is not clear. What is implemented till now is partly correct but need to understand the requirements correctly. The reason you are getting error is because there will be many components with aura Id ten. – salesforce-sas Aug 19 at 14:09
  • @CarlosNaranjo I haven't tried that, would that go in the constructor? – Tyler Aug 19 at 14:20
  • How is handleKeyUp invoked, I don't see that in CMP file? – salesforce-sas Aug 19 at 14:42
  • From what i've read you can't get the aura:id of something inside an iteration. The fix is to use event.getSource() but when I do this I get "undefined" instead of the name of the property. – Tyler Aug 19 at 16:33
0

I used activeSectionName to get the active section and was able to find the property name this way. I then could pass that iterations variable to my helper and run the query with that as a parameter. Hope this helps someone.

 <lightning:accordion aura:id="accordion" onsectiontoggle="{!c.grayButtons}">
           <aura:iteration items="{!v.searchResult}" var="property">

            //you can pass your accordionSection name variable to the c.grayButtons  
          <lightning:accordionSection name="{!property.Name}" label="{!property.Name}">


ten: function(component, event)
    {
        alert("helper 1");
        var action = component.get("c.tenPercent");

      //with this line of code here. Active section name isn't listed anywhere thanks to the great
documentation by salesforce but this will actually get the variable of the active section
        var myId = component.find("accordion").get("v.activeSectionName");

        action.setParams({"ten" : myId});
        action.setCallback(this, function(response)
         {
           var state = response.getState();
           if(state === "SUCCESS")              
           {
            var storeResponse = response.getReturnValue();
               if (response.getReturnValue().length != 0) {
                    component.set("v.isDisabled1", "false");
                } else {
                    component.set("v.isDisabled1", "true");

                }

           } else {
                    alert("Unknown error");
                }
       });
        $A.enqueueAction(action);       
      },

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.