salesforcehandlersalesforce-lightninglightning

salesforce lightning component init function call


I am trying to check something before the modal popup shows in salesforce opportunity page. When I call the doInit function the button handler functions stop working. My code is

<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,force:appHostable,flexipage:availableForRecordHome,force:hasRecordId,force:hasSObjectName" access="global">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <b>This is recordID {!v.recordId} </b><br/>
  Are you sure you want to proceed?
  <div class="slds-align_absolute-center">
    <lightning:button
      label="No"
      variant="destructive"
      onclick="{!c.handleNo}"
    ></lightning:button>
    <lightning:button label="Yes" onclick="{!c.handleYes}"></lightning:button>
  </div>
</aura:component>

and the controller is

(   {
    
    doInit:function(component,event,helper){
   alert(component.get("v.recordId"));
  }
},

    {
  handleNo: function (component, event, helper) {
    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      url: "/apex/NQuote",
      isredirect: "true"
    });
    urlEvent.fire();
  },

  handleYes: function (component, event, helper) {
    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      url: "/apex/TestPage",
      isredirect: "true"
    });
    urlEvent.fire();
  }
});

Solution

  • The format for your controller is incorrect. The format should be basically a JSON like this:

    {
        method1 : function (component, event, helper){},
        method2 : function (component, event, helper){}
    }
    

    You have extra curly braces after the doInit method. It should be like this:

    ({
      doInit: function (component, event, helper) {
        alert(component.get("v.recordId"));
      },
      handleNo: function (component, event, helper) {
        var urlEvent = $A.get("e.force:navigateToURL");
        urlEvent.setParams({
          url: "/apex/NQuote",
          isredirect: "true"
        });
        urlEvent.fire();
      },
    
      handleYes: function (component, event, helper) {
        var urlEvent = $A.get("e.force:navigateToURL");
        urlEvent.setParams({
          url: "/apex/TestPage",
          isredirect: "true"
        });
        urlEvent.fire();
      }
    });