Dynamically created component with aura:id, set as a Facet inside a parent component, is not found when calling find() and passing in aura:id
This is a bug inside the framework where dynamically created components are not getting properly indexed to their parents. Doesn't look like anyone is actively working on it right now. If you'd like to put some pressure on the devs who own this you can file a case and tell them to link the case to internal bug W-2529066.
as @techbusinessman stated, the only workaround (so far) to get the value from a dynamically created component is to use :
var value = component.get("v.*attributeName*").get("v.value");// for type="Aura.Component"
or
var value = component.get("v.*attributeName*")[0].get("v.value"); // for type="Aura.Component[]"
posting it again so that it would help somebody. This small piece of bug has already eaten half of day when I found his comment.
Here's an alternative workaround which seems much neater to me (set as an answer because it gives space for the code)...
First declare an attribute to store dictionary object of aura:id to component:
<aura:attribute name="dynamicComponentsByAuraId" type="Object" />
Then, populate that object in the callback when you create your components:
$A.createComponents(
componentArray,
function(newComponents, status, errorMessage){
if (status === "SUCCESS") {
var container = component.find("formContainer");
var containerBody = component.get("v.body");
var dynamicComponentsByAuraId = {};
for(var i=0; i < newComponents.length; i++) {
var thisComponent = newComponents[i];
container.push(thisComponent);
dynamicComponentsByAuraId[thisComponent.getLocalId()] = thisComponent;
}
container.set("v.body", containerBody);
console.log("dynamicComponentsByAuraId");
console.log(dynamicComponentsByAuraId);
}
}
);
Then, instead of calling component.find("foo"), you can just use:
var fooComponent = dynamicComponentsByAuraId["foo"];
// do things with fooComponent