1

I'm getting an error when I supply the list property acc.Contacts to the aura iteration component.

<aura:application>
    <aura:attribute name="acc" type="Account" 
        default="{ 
            'sobjectType': 'Account',
            'Contacts': [{'sobjectType': 'Contact', 'Name': 'test'}]
        }"
   />

    <aura:iteration items="{!v.acc.Contacts}" var="contact">
        <span>
            {!contact.Name}
        </span>
    </aura:iteration>
</aura:application>

Result: "Something has gone wrong. [NoErrorObjectAvailable] Aura.loadComponent(): Failed to initialize application."

Is iterating over a list property not supported?

  • do you want to iterate a nested property (Contacts) in an object? – Praveen Jul 12 '16 at 18:03
  • @Praveen yes I want to iterate over acc.Contacts – bafuda Jul 12 '16 at 18:10
  • I contacted Salesforce support and they're looking into the issue. Their comment is: "This occurs because It appears that the default attribute on aura:attribute is creating a String value for non-String Collection types. As another test, modifying the code to use a String Collection still including the default="" will work as expected. <aura:attribute name="recentAccounts" type="String[]" default="" /> Works." – bafuda Jul 27 '16 at 17:39
1

Not sure what your trying to do, but type='Account' maps to an sObject Account instance, and clearly it does not contains Contacts field/property.So changing the type to Map makes it to work.

<aura:attribute name="acc" type="Map" 
    default="{ 
    'sobjectType': 'Account',
        'Contacts': [{'sobjectType': 'Contact', 'Name': 'test'}]
    }"
/>
  • Using a map is great workaround thanks. I'm trying to iterate over the Contacts property. This is a valid property of an account object (this is valid Apex): List<Contact> contacts = [select (select Id from Contacts) from account limit 1].Contacts; – bafuda Jul 12 '16 at 18:21
  • But it doesn't directly belong to Account object like other fields Name,Email,...so on. For ex: This is valid Account a = new Account(Name = 'test'); not this Account a = new Account(Contacts = new Contact[]{new Contact(LastName='test')}); – Praveen Jul 12 '16 at 18:33
  • Because Contacts it is an special non-writable property returned by the SOQL query only, as far as i know. – Praveen Jul 12 '16 at 18:38
  • I think it is still a property of an account object, it's just not a property that can be modified or set. Maybe it's just not readable in this context. That should be called out in the documentation though I think. – bafuda Jul 12 '16 at 18:39
1

The work around I'm currently using is to assign acc.Contacts to a component attribute

<aura:application>
    <aura:attribute name="acc" type="Account" 
        default="{ 
            'sobjectType': 'Account',
            'Contacts': [{'sobjectType': 'Contact', 'Name': 'test'}]
        }"
    />
    <aura:attribute name="contacts" type="Contact[]" 
        default="{!v.acc.Contacts}"
    />

    <aura:iteration items="{!v.contacts}" var="contact">
        <span>
            {!contact.Name}
        </span>
    </aura:iteration>
</aura:application>

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.