[SalesForce] How to use and inside an

Here @Peter describes a very cool way to use a <force:inputField ... /> together with

<aura:attribute name="YourObjectType" type="YourObjectType" 
    default="{ sobjectType: 'YourObjectType' }" 
/>

where YourObjectType could be Account for instance.

My js-controller is

({
        init : function(component, event, helper) {
            console.log('init');
            var action = component.get("c.getData");        
            action.setCallback(this, function(response) {
                console.log(response.getReturnValue());
                component.set("v.opportunities", response.getReturnValue().opportunities);
            });
            $A.enqueueAction(action);
        }, 
})

And my apex controller is

public class elfClientOpportunityList { 
    public class data {
        @AuraEnabled public List<Opportunity>       opportunities               { get; set; }
        @AuraEnabled public String                  text2                       = 'text2';
        public data() {
            this.opportunities = new List<Opportunity>();           
        }
    }
    @AuraEnabled public static data     getData() {
        data result = new data();
        result.opportunities = (List<Opportunity>) Database.query( xs.soql(
            ' SELECT *, Account.Name FROM Opportunity '
        ));
        return result; 
    }
}

(Sidenotes: text2 is just a placeholder for now and SELECT * does what it does in SQL using this)

Now I need to use these fields inside an iteration over something like this

<aura:attribute name="opportunities" type="Opportunity[]" />
// ...
<aura:iteration var="item" items="{!v.opportunities}">
    // ...
    <force:inputField value="{!item.YourCustomFieldName__c}"/>
    // ...
<aura:iteration>

What do I need to define at the sobjectType-property in the default attribute?

I've tried this already

    <aura:attribute name="opportunities"    type="Opportunity[]" default="{ sobjectType: 'Opportunity[]' }" />

But as an unfortunate it bring only an not helpful error message

Aura.loadComponent(): Failed to initialize application.
An internal server error has occurred
Error ID: 319439292-6868 (-1241857102)

enter image description here

Using List<Opportunity> does not work either

<aura:attribute name="opportunities"    type="Opportunity[]" default="{ sobjectType: 'List<Opportunity>' }" />

You can't save it, because the API says:

markup://c:elfClientOpportunityList:5,90: ParseError at
[row,col]:[6,90] Message: The value of attribute "default" associated
with an element type "null" must not contain the '<' character.:
Source

Now I'm out of ideas an need to ask for help. Thanks a lot!!

Best Answer

It's about 2 years after I initially touched this issue and today I've found this Known Issue here https://success.salesforce.com/issues_view?id=a1p3A0000008gDzQAI

It confirms my ancient observations from 2015 and confirms also that the Peters fine-grained approach does not work.

For me the recommended workaround is not usefull, since ui:whatever can't out of the box deal with picklists values, field dependencies, translation and localization like number- and date-formats. It can be done, but needs an unjustified amout of complexity and code. My workaround is to use visualforce with it's mature apex:inputField instead.

Here is the Known Issue

<force:inputField> is not rendering fields on UI when being used inside <aura:iteration> tag or inside nested components in Lightning application

Summary

<force:inputField> is not rendering fields on UI as expected when being used inside <aura:iteration> or inside inner component in Lightning application

Repro

  1. Login to your org
  2. Try creating a sample lightning application
  3. Create a component with below sample code snippet & try to preview the app
  4. You will notice the fields doesn't render on the UI as expected.
<aura:component>  
    <aura:iteration aura:id="iteration" items="{!v.expenses}" var="expenses">  
        <force:inputField value="{!expenses.Amount__c}"/>  
    </aura:iteration>  
</aura:component>

Workaround

Try using <ui:inputText> or or <ui:inputCheckbox> instead of for rendering input fields inside <aura:iteration> or inside inner components