0

I am running into an issue with calling an apex method from a lightning aura component when a lightning date field is changed. Every time I update the field it seems to enter some sort of recursive process that prints out the console.log but never actually calls the controller Please see my code below

Aura Component

<aura:component controller="ContactSchedulingComponentCtl"  implements="force:hasRecordId,force:hasSObjectName,flexipage:availableForRecordHome,flexipage:availableForAllPageTypes" >
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="showModal" type="Boolean" default="false"/>
    <aura:attribute name="programs" type="List" />
    <aura:attribute name="program" type="String" />
    <aura:attribute name="programTypes" type="List" />
    <aura:attribute name="programType" type="String" />
    <aura:attribute name="selectDate" type="Date" />
    <aura:attribute name="showLesson" type="Boolean" default="false"/>
    <aura:attribute name="showCourse" type="Boolean" default="false"/>
    <aura:attribute name="lesson" type="SObject" />
    <aura:handler name="init" value="{! this }" action="{! c.init }"/>
    <button class="slds-button slds-button_brand" onclick="{!c.toggleModal}" >Schedule Participant</button>
    <aura:if isTrue="{!v.showModal}" >
        <section role="dialog" tabindex="-1" class="slds-modal slds-fade-in-open slds-modal_large" aria-labelledby="modal-heading-01" aria-modal="true">
            <div class="slds-modal__container">
                <lightning:buttonIcon iconName="utility:close" variant="bare" alternativeText="Close" title="Close" />
                <div class="slds-modal__header">
                    <h1 id="modal-heading-01" class="slds-modal__title slds-hyphenate">Schedule Participant</h1>
                </div>
                <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
                    <div class="slds-grid slds-gutters">
                        <div class="slds-col slds-size_1-of-4">
                            <lightning:select name="program" label="Program:" aura:id="program" value="{!v.program}" variant="label-inline" onchange="{!c.getProgramType}">
                                <option value="">choose one...</option>
                                <aura:iteration items="{!v.programs}" var="option">
                                    <option text="{!option.Name}" value="{!option.Id}" />
                                </aura:iteration>
                            </lightning:select>
                        </div>
                        <div class="slds-col slds-size_1-of-4">
                        </div>
                        <div class="slds-col slds-size_1-of-4">
                        </div>
                        <div class="slds-col slds-size_1-of-4">
                        </div>
                    </div>
                    <br/>
                    <aura:if isTrue="{!v.programType == 'Lesson'}">
                        <h2 class="slds-card__header-title">
                            <a href="#" class="slds-card__header-link slds-truncate" title="Accounts">
                                <span>Schedule Lesson</span>
                            </a>
                        </h2>
                        <div class="slds-grid slds-gutters">
                            <div class="slds-col">
                                <lightning:input type="date" name="input1" label="Select a Date" placeholder="select date..." value="{!v.selectDate}" oncommit="{!c.getLesson}"/>
                            </div>
                            <div class="slds-col">
                                Number of Participant: {!v.lesson.Number_of_Participants__c}
                            </div>
                            <div class="slds-col">
                                <button class="slds-button slds-button_brand">Schedule</button>
                            </div>
                        </div>
                        <br/>
                    </aura:if>
                    <div class="slds-modal__footer">
                        <button class="slds-button slds-button_neutral" aria-label="Cancel and close" onclick="{!c.toggleModal}">Cancel</button>
                        <button class="slds-button slds-button_brand">Save</button>
                    </div>
                </div>
            </div>
        </section>
        <div class="slds-backdrop slds-backdrop_open" role="presentation"> 
       </div>
    </aura:if> 
</aura:component>

JS Controller Method

getLesson : function(component, event, helper) {
    var programId = component.get("v.program");
    var selectDate = component.get("v.selectDate");
    console.log(programId+' '+selectDate);
    helper.getLesson(component, event,  programId, selectDate);
},

Helper Method

getLesson : function(component, event, helper, programId, selectDate) {
    console.log('get lessons '+programId+' '+selectDate);
    var action1 = component.get('c.getLesson');
    action1.setParams({
        programId: programId, 
        lessonDate: selectDate
    });
    action1.setCallback(this, function(response) {
        var state = response.getState();
        var data = response.getReturnValue();
        if(response.getState() == "SUCCESS"){
            console.log('RESPONSE is '+response);
            console.log('state is '+state);
            console.log(data);
            component.set('v.lesson', data);                
        } 
    });
    $A.enqueueAction(action1);
},

Apex Controller

public with sharing class ContactSchedulingComponentCtl { 
    @AuraEnabled
    public static List<pmdm__Program__c> getPrograms() {
        System.debug('get programs ');
        List<pmdm__Program__c> programs = [Select Name, Id, RecordType.Name, Program_Type__c From pmdm__Program__c where pmdm__Status__c = 'Active'];
        System.debug('programs '+programs);
        return programs;
    }
    
    @AuraEnabled
    public static Lesson__c getLesson(Id programId, Date lessonDate) {
        System.debug('get lessons ');
        Lesson__c returnLesson = new Lesson__c();
        Lesson__c currentLesson = [Select Id, Date__c, Name, Program__c, Number_of_Participants__c, Number_of_Instructors__c 
                                   from Lesson__c 
                                   Where Date__c = :lessonDate AND Program__c = :programId limit 1];
        System.debug(currentLesson);
        if(currentLesson != null){
            returnLesson = currentLesson;
        } else {
            Lesson__c l = new Lesson__c();
            l.Id = null;
            l.Date__c = lessonDate;
            l.Program__c = programId;
            l.Number_of_Participants__c = 0;
            returnLesson = l;
        }
        System.debug(returnLesson);
        return returnLesson;
    }
}
0

1 Answer 1

1

You cannot use the same name for an Aura controller method and an Apex controller method. This is highlighted in the documentation:

Use unique names for client-side and server-side actions in a component. A JavaScript function (client-side action) with the same name as an Apex method (server-side action ) can lead to hard-to-debug issues. In debug mode, the framework logs a browser console warning about the clashing client-side and server-side action names.

This is because it is possible to call controller methods using the exact same syntax (e.g. component.get("c.getLesson") will actually call the JavaScript code by that name, instead of the Apex method). Similarly, you should keep in mind that you cannot name two Apex methods in the same class with different parameters and expect the JavaScript code to call the correct method.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .