function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mathew Andresen 5Mathew Andresen 5 

call to apex working on one component but not on other in lightning

Hi,

I have a lightning component that is creating a new task.  It used to call it's component, which calls the apex class and that works fine.  But then I realized it would really be better if it called an event which passed the task up so it could be created by the main component, and that doesn't work


Account Activities component calls for creation
<aura:component controller="AccountProspectingClass">
	<aura:attribute name="selectedAccount" type="account"/>
    <aura:attribute name="newTask" type="Task" />
    <aura:attribute name="subject" type="string"/>
    <aura:registerEvent name="createTask" type="c.accountProspecting_TaskEvent"/>
    
    
  
    <lightning:tabset >
        <lightning:tab label="Make a Call">
            <fieldset class="slds-box slds-theme--default slds-container--small">
                <form class="slds-form--stacked">
                
                      <div class="slds-form-element slds-is-required">
                          <div class="slds-form-element__control">
                              <ui:inputText aura:id="subject" label="Subject:"
                                  class="slds-input"
                                  labelClass="slds-form-element__label"
                                  value="{!v.newTask.Subject}"
                                  required="true"/>
                          </div>
                     </div>                

                      <div class="slds-form-element slds-is-required">
                          <div class="slds-form-element__control">
                              <ui:inputText aura:id="comments" label="Comments:"
                                  class="slds-input"
                                  labelClass="slds-form-element__label"
                                  value="{!v.newTask.Description}"
                                  required="true"/>
                          </div>
                     </div>                      

                    <div class="slds-form-element slds-is-required">
                          <div class="slds-form-element__control">
                              <ui:inputDate aura:id="taskDate" label="Date:"
                                  class="slds-input"
                                            displayDatePicker="true"
                                  labelClass="slds-form-element__label"
                                  value="{!v.newTask.ActivityDate}"
                                  required="true"/>
                          </div>
                     </div>                        
                    
                     <div class="slds-form-element">
                          <ui:button label="Create Expense"
                              class="slds-button slds-button--brand"
                              press="{!c.clickCreateTask}"/>
                     </div>

                
                 
                </form>
            </fieldset>             
        </lightning:tab>
        
        <lightning:tab label="New Task">
        
        
        </lightning:tab>
    
    </lightning:tabset>
 
 
</aura:component>
That component then calls the lightning controller which calls the apex class and that works fine
 
({
	clickCreateTask : function(component, event, helper) {
   
		// old way of doing works fine
        var newTask = component.get("v.newTask");
        var acct = component.get("v.selectedAccount");
        component.set("v.newTask.WhatId", acct.Id);
        console.log("acct Id = " + acct.Id);
        console.log("task is : " + JSON.stringify(newTask));
        
        var action = component.get("c.createTask");
        action.setParams({"newTask": newTask});
       
        action.setCallback(this, function(response){
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                var returned =response.getReturnValue()
                console.log("items returned: " + JSON.stringify(returned));
             //   var expenses = component.get("v.expenses");
              //  expenses.push(response.getReturnValue());
              //  component.set("v.expenses", expenses);
              
            }
        });
        $A.enqueueAction(action);
                             
		
	}
})

New way, calls the event
({
	clickCreateTask : function(component, event, helper) {
           // new way of doing things, calls event
        var newTask = component.get("v.newTask");
        
        var updateEvent = component.getEvent("createTask");
        updateEvent.setParam({"currentTask": newTask});
        updateEvent.fire();

		
	}
})
<aura:event type="COMPONENT" description="task event" >
    <aura:attribute name="currentTask" type="Task"/>
</aura:event>
The main component
<aura:component controller="AccountProspectingClass"  implements="force:appHostable,flexipage:availableForAllPageTypes" access="global">
    <aura:attribute name="accountList" type="account[]"/>
    <aura:attribute name="selectedAccount" type="account"/>
    <aura:attribute name="newTask" type="Task" default="{'sobjectType': 'Task', 'Subject': 'blank subject', 'Description': ' blank description'}"/>
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    <aura:handler name="selectAccount" event="c.accountProspecting_selectAccountEvent" action="{!c.updateAccount}"/>
    <aura:handler name="createTask" event="c.accountProspecting_TaskEvent" action="{!c.createTask}"/>
	<!-- header for selected Account -->
    <c:accountProspectingSelected selectedAccount="{!v.selectedAccount}"/>
    <!-- end header -->
    <div class="slds-grid  "  >
       <div class="slds-col slds-size--1-of-6 slds-scrollable " style="height: 600px;">    
           <table class="slds-table " >
           <aura:iteration items="{!v.accountList}" var="acct" >
                <c:accountProspectingItem acct="{!acct}"/>  
        
           </aura:iteration>
           </table>
       </div> 
    
    
        <div class="slds-col slds-size--2-of-3 slds-p-around--large">
            <c:accountActivities selectedAccount="{!v.selectedAccount}" newTask="{!v.newTask}"/>
        </div>
                    

        

    </div> <!-- end the main grid -->
	
</aura:component>

and the controller for the main component
createTask: function(component, event, helper) {
        
       
        var newTask = event.getParam("currentTask");
        var acct = component.get("v.selectedAccount");
        component.set("v.newTask.WhatId", acct.Id);
        console.log("acct Id = " + acct.Id);
        console.log("task is : " + JSON.stringify(newTask));
        
        var action = component.get("c.createTask");
        action.setParams({"newTask": newTask});
       
        action.setCallback(this, function(response){
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                var returned =response.getReturnValue()
                console.log("items returned: " + JSON.stringify(returned));
             //   var expenses = component.get("v.expenses");
              //  expenses.push(response.getReturnValue());
              //  component.set("v.expenses", expenses);
              
            }
        });
        $A.enqueueAction(action);
       
        
    },