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
@GM@GM 

$A.get(applicationEvent) returning undefined and Uncaught TypeError: Cannot read property 'setParams' of undefined

Hi All,

when I'm trying to call application event then I'm getting below error and $A.get(event) is returning undefined which is marked bold in the below code.
Error : Uncaught TypeError: Cannot read property 'setParams' of undefined
 
CalendarView.cmp  :

<aura:component controller="CalendarController" implements="force:hasRecordId,force:hasSObjectName,force:appHostable" access="global">
<aura:registerEvent name="loadMyEvent" type="harshabr:ResultEvent"/>
    
    <ltng:require styles="/resource/gpcal_lex_yo_SLDS0121/assets/styles/salesforce-lightning-design-system-ltng.css,/resource/gpcal_lex_yo_fullcalendar/fullcalendar-2.6.0/fullcalendar.css" 
	scripts="/resource/gpcal_lex_yo_jquery,/resource/gpcal_lex_yo_fullcalendar/fullcalendar-2.6.0/lib/moment.min.js,/resource/gpcal_lex_yo_fullcalendar/fullcalendar-2.6.0/fullcalendar.js"
              
	afterScriptsLoaded="{!c.doInit}"
/>
	<aura:attribute name="recordId" type="String" access="global"/>
	<aura:attribute name="sObjectName" type="String" access="global"/>	
	<aura:attribute name="user_id" type="String" />
    
    <aura:attribute name="cal" type="Boolean" default="true" />
	<aura:attribute name="evtRec" type="List" />
    <aura:attribute name="eventId" type="String" />
    
     <aura:renderIf isTrue="{!v.cal}">
        <div id='calendar'>
        <!-- <c:EventsList evtId="{!v.eventId}" /> -->
	</div>
    </aura:renderIf>    
      
</aura:component>

CalendarViewController :

({
	doInit: function(component, event, helper){
        $('#calendar').fullCalendar({
	    	defaultView: 'month',
	    	allDaySlot: false,
	    	selectable: true,
			selectHelper: true,
			select: function(start, end) {
				
				var title = prompt("Please enter subject of event:");
				var eventData;
				if (title) {

					var action = component.get("c.createNewEvent");
					var params = { 
						start_time : start.format("YYYY-MM-DD HH:mm:ss"),
						end_time   : end.format("YYYY-MM-DD HH:mm:ss"),
						user_id    : component.get("v.user_id"),
						what_id    : component.get("v.recordId"),
						sobjectName    : component.get("v.sObjectName"),
						title      : title		
					};
					action.setParams(params);

					action.setCallback(this, function(actionResult) {				        				        
				        if( actionResult.getReturnValue() ){
				        	$('#calendar').fullCalendar( 'refetchEvents' );
				        	console.log("save successful");
				        }
				        else{

				        	alert( "Save failed for some reason." );
				        }
				    });
				    
				    $A.run(function(){
				    	$A.enqueueAction(action);						
				    });

				    

				}
				$('#calendar').fullCalendar('unselect');
			},
	    	events: function(start, end, timezone, callback) {
	    		var user_id = component.get("v.user_id");	    		
				var action = component.get("c.getEvents");
				action.setParams(
					{ 
						start_time : start,
						end_time   : end,
						user_id    : user_id		
					}
				);

				action.setCallback(this, function(actionResult) {
			        callback(JSON.parse(actionResult.getReturnValue()));
			    });
			    $A.enqueueAction(action);				
	    	},
            eventClick: function(calEvent, jsEvent, view) {
    	        console.log('gm calEvent '+calEvent);
                console.log('gm1 '+calEvent.id);
                var evtId = calEvent.id;
                component.set("v.cal",true);
                var evt = $A.get("e.c:ResultEvent");
            	console.log(evt);
                console.log(evtId);
				evt.setParams({ "pass_result": evtId });
				evt.fire();
                
		    }
	    });
	}
})


ResultEvent.evt :
<aura:event type="APPLICATION">
    <aura:attribute name="pass_result" type="String"/>
</aura:event>

app:

<aura:application extends="force:slds">
    <c:CalendarView />
    <c:EventsList />
</aura:application>


EventsList.cmp :
<aura:component controller="CalendarController" access="global">
  <aura:attribute name="eventData" type="List" access="global" />
    <aura:handler event="harshabr:ResultEvent" action="{!c.getMyEvents}" />
    
    <table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">
    <thead>
      <tr class="slds-text-heading--label">
        <th scope="col"><div class="slds-truncate" title="ID">ID</div></th>
        <th scope="col"><div class="slds-truncate" title="Subject">Subject</div></th>
        <th scope="col"><div class="slds-truncate" title="StartDateTime">StartDateTime</div></th>
      </tr>
    </thead>
    <tbody>
      <!-- Use the Apex model and controller to fetch server side data -->
      <aura:iteration items="{!v.eventData}" var="e">
        <tr>
          <th scope="row"><div class="slds-truncate" title="{!e.Id}">{!e.Id}</div></th>
          <td><div class="slds-truncate" title="{!e.Subject}">{!e.Subject}</div></td>
          <td><div class="slds-truncate" title="{!e.StartDateTime}">{!e.StartDateTime}</div></td>
          <td>
            <form class="account-form" onsubmit="{!c.deleteAccount}">
              <input type="hidden" value="{!e.Name}" class="account-name" />
             
               <!--  Use a Lightning Base Component
                To display an icon next to the label-->
             
              <lightning:button label="Delete"
                                iconName="utility:delete"
                                iconPosition="left"
                                variant="destructive"
                                />
            </form>
          </td> 
        </tr>
      </aura:iteration>
    </tbody>
  </table>
</aura:component>

 EventsListController :
({
getMyEvents: function(component, event, helper){
        var action = component.get('c.getEventss');
    	var evtIds = event.getParam("pass_result");
      	console.log('GM evtId '+evtIds);
      	action.setParams({
        	  "eventId": evtIds
      	});
    	// Set up the callback
    	var self = this;
    	action.setCallback(this, function(actionResult) {
     		component.set('v.eventData', actionResult.getReturnValue());
    	});
      	console.log("GM res "+component.get('v.eventData'));
    	$A.enqueueAction(action);
    },
  deleteAccount: function(component, event, helper) {
    // Prevent the form from getting submitted
    event.preventDefault();

    // Get the value from the field that's in the form
    var accountName = event.target.getElementsByClassName('account-name')[0].value;
    confirm('Delete the ' + accountName + ' account? (don’t worry, this won’t actually work!)');
  }
})
GauravendraGauravendra
Hi GM,

I think when you are calling ResultEvent event, you are not specifying the namespace.
It should be var evt = $A.get("e.harshabr:ResultEvent");

Hope this helps.
@GM@GM
Hi Gauravendra,

Thanks for the reply but when I specify the namespace then I get same error since console.log(evt); is showing as undefined.

TypeError: evt is undefined throws at ....../auraFW/javascript/5HxA6PNAIzmJB2pSyRCygA/aura_proddebug.js line 5360 > eval:77:5

Regards,
GM
Pankaj GurbaniPankaj Gurbani
Enclosing your eventClick function in $A.getCallback() will fix it.
Like this: (Line 89)
eventClick: $A.getCallback(function(calEvent, jsEvent, view){ 
//your code
})

You're Welcome