5

This question already has an answer here:

We have a lightning component thats being used a quick action. When the lightning component is activated it calls the init handler multiple times

Component

<aura:component implements="force:lightningQuickAction,force:hasRecordId,flexipage:availableForAllPageTypes" controller="CloneObject">
    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="name" type="String"/>

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <ui:inputText label="Quote Name" value="{!v.name}"/>
    <br/>
    <ui:button class="slds-button slds-button--neutral" press="{!c.clone}" label="Clone" />
</aura:component>

js controller

({
doInit : function(component, event, helper) {
    var action = component.get("c.doInit");
    var recordId = component.get("v.recordId");
    console.log(recordId);
    action.setParams({
        "recordId" : recordId
    });

    action.setCallback(this, function(response) {
        var state = response.getState();
        if(component.isValid() && state == "SUCCESS"){
            component.set("v.name", response.getReturnValue());
        } else {
            console.log('There was a problem and the state is: '+state);
        }
    });
    $A.enqueueAction(action);
},

clone : function(component, event, helper) {
    var action = component.get("c.cloneQuote");
    var recordId = component.get("v.recordId");
    console.log('record Id');
    console.log(recordId);
    action.setParams({
        "objectId": recordId,
        "cloneAttachments": true
    });

    action.setCallback(this, function(response) {
        var state = response.getState();
        if(component.isValid() && state == "SUCCESS"){
            var urlEvent = $A.get("e.force:navigateToURL");
            urlEvent.setParams({
                "url": "/" + response.getReturnValue()
            });
            urlEvent.fire();
        } else {
            console.log('There was a problem and the state is: '+state);
        }
    });
    $A.enqueueAction(action);
}
})

Apex Controller

public class CloneObject {

    @auraEnabled
    public static String doInit(String recordId) {
        return [SELECT Name FROM Quote WHERE Id = :recordId].Name;
    }
 }

Output when the quickaction is clicked

enter image description here enter image description here

marked as duplicate by Sebastian Kessel, Santanu Boral, martin, Mohith Shrivastava, Praveen Mar 21 '17 at 5:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Try do rename you Apex controller static method to something other than 'doInit' – benahm Mar 20 '17 at 22:21
  • @benahm that is fascinating. I figured it would be best practice to continue out the method name from component -> controller -> apex method. – Programmatic Mar 20 '17 at 22:25
4

I marked it as a possible dupe of this question, but here is the answer.

You cannot name the Apex Component method identically to the Lightning Controller method. Not sure why, but it creates recursion (as you've seen in your logs).

Change either of them and you'll be fine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.