0

I'm trying to get the saved data from my recordForm component handling the onsuccess event but the controller's function handling the event is not triggered. I think this happens because my component is dynamically crated.

Do you have any idea how to solve it?

Thank you!

componentConroller.js

 handleNewRecordAdder : function(component, event, handler) {

    var sObjectType = event.getParam("data");
    var fields = ["Name"];
    component.set("v.isActive", true);
    component.set("v.loading", true);

    $A.createComponent(
        "lightning:recordForm",
        {
            "aura:id": "sobjectAdder",
            "objectApiName" : sObjectType,
            "fields" : fields,
            "columns" : "1",
            "mode" : "Edit",
            "onsuccess" : "{!c.handleSuccess}"
        },
        function(recordForm, status, errorMessage){
            //Add the new button to the body array
            if (status === "SUCCESS") {

                var container = component.find("container");
                component.set("v.loading", false);
                container.set("v.body", recordForm);
            }
            else if (status === "INCOMPLETE") {
                console.log("No response from server or client is offline.")
                // Show offline error
            }
            else if (status === "ERROR") {
                console.log("Error: " + errorMessage);
                // Show error message
            }
        }
    );

},
handleSuccess : function(component, event, helper) {

    console.log("done");
} 

})
3

You need to use getReference method of Component to tell the button that which methods needs to call.

component.getReference("c.handleSuccess")

Your code will look like below

 handleNewRecordAdder : function(component, event, handler) {

    var sObjectType = event.getParam("data");
    var fields = ["Name"];
    component.set("v.isActive", true);
    component.set("v.loading", true);

    $A.createComponent(
        "lightning:recordForm",
        {
            "aura:id": "sobjectAdder",
            "objectApiName" : sObjectType,
            "fields" : fields,
            "columns" : "1",
            "mode" : "Edit",
            "onsuccess" : component.getReference("c.handleSuccess")
        },
        function(recordForm, status, errorMessage){
            //Add the new button to the body array
            if (status === "SUCCESS") {

                var container = component.find("container");
                component.set("v.loading", false);
                container.set("v.body", recordForm);
            }
            else if (status === "INCOMPLETE") {
                console.log("No response from server or client is offline.")
                // Show offline error
            }
            else if (status === "ERROR") {
                console.log("Error: " + errorMessage);
                // Show error message
            }
        }
    );

},
handleSuccess : function(component, event, helper) {

    console.log("done");
} 

})

Refer the given link: - Dynamically Creating Components

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.