3

I want to build simple search component which can be configured for any Sobject, The code works fine for known standard sObject attributes like name, id, but there are other attributes which may vary for different object.

I'm wondering since lightning does client side rendering since its JS/HTML, I tried to access object properties as acc[0], acc[1], assuming 0 is Id, 1 is Name, but it didn't work. What is the alternate work around for dynamically accessing object properties?

Component code is below at high level,

<aura:attribute name="sobject" type="String" default="account"/>    <aura:attribute name="query" type="String" default="Select Id, Name From Account"/>
    <aura:attribute name="fields" type="String[]" default="['Id', 'Name']"/>
    <form>
        <ui:inputText class="form-control" label="Search" aura:id="searchField" value="" placeholder="enter search string"/>
        <ui:button label="Submit" press="{!c.searchSObjects}"/>
    </form>
    <ul class="list-group">
        <aura:iteration var="sobj" items="{!v.sobjects}">
            <li class="list-group-item"><a aura:id="sobjid" href="{!'/' + sobj.id}">{!sobj[0]} +", "+{!sobj[1]} </a></li>   
        </aura:iteration>
    </ul>

App config code

<aura:application >
    <link href='/resource/bootstrap/' rel="stylesheet"/>
    <h2>Account Search</h2>
    <DynamicSobjectSearch sobject="Account" query="Select Id, Name From Account" />
</aura:application>

Thanks

1

I strongly suspect that you're going to need to use JS remoting with an Aura enabled Apex controller to handle your queries for you as @PeterKnolle did in a blog Post titled Lightning Component Autocomplete. To make a controller aura enabled, you simply add @AuraEnabled annotation to any methods being called by your Aura components/applications.

You might want to also read a Developer Blog post by @DanielPeter titled Navigating to Reports & Records Using Lightning Component Events which references the former post.

3
  • the question is more about accessing dynamic properties of sobject returned by lightning controller, i'm using annotation already. – realnumber Aug 7 '15 at 20:35
  • If you're looking for dynamic properties, you'll need to use schema describe calls to get the field properties for each sObject. That's discussed in the VF Reference if I'm not mistaken. – crmprogdev Aug 7 '15 at 20:48
  • any object comes back as js object, so its nothing to do with server side. – realnumber Aug 7 '15 at 22:03

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.