• john alem
  • NEWBIE
  • -6 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
I have two custom objects, Booking and related Services. I want to implement an override for the standars New button of the Service object as a new Service record can only be created in the context of a Booking. So I created a Lightning component for that, which should open a new page saying 'please create a Service record from within a Booking, showing below a datatable of the most recent edited Booking records. 

<aura:component controller="BookingController" implements="flexipage:availableForRecordHome,force:hasRecordId,lightning:actionOverride" access="global">
    <ui:message title="Information" severity="warning" closable="false">
        Please create new Service records from a Booking record.
    </ui:message>
    
    <aura:attribute type="Booking__c[]" name="bookList"/>
    <aura:attribute name="mycolumns" type="List"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.fetchBookings}"/>
    
    <lightning:datatable data="{! v.bookList }"
                         columns="{! v.mycolumns }"
                         keyField="id"
                         hideCheckboxColumn="true"/>          
</aura:component>

The component controller has following code:

({
    fetchBookings : function(component, event, helper) 
    {    
        helper.fetchBookingsHelper(component, event, helper);
    }
})

And the helper:

({
    fetchBookingsHelper : function(component, event, helper) 
    {
        component.set('v.mycolumns', [
            {label: 'Booking Number', fieldName: 'Name', type: 'text'},
            {label: 'Booking Reference', fieldName: 'Booking_Name__c', type: 'text'},
            {label: 'Arrival Date', fieldName: 'Arrival_Date__c', type: 'date'},
            {label: 'Departure Date', fieldName: 'Departure_Date__c', type: 'date '},
            {label: 'Agent', fieldName: 'Agent__c', type: 'text'}
           ]);

        var action = component.get("c.fetchBookings");
        
        action.setParams({});
        
        action.setCallback(this, 
            function(response)
            {
                var state = response.getState();
                
                if (state === "SUCCESS") 
                { 
                    component.set("v.bookList", response.getReturnValue());
                    alert("Success: " + response.getReturnValue());
                }
                else if (state === "INCOMPLETE")
                {
                    // do something
                }
                else if (state === "ERROR") 
                {
                    var errors = response.getError();
                    
                    if (errors) 
                    {
                        if (errors[0] && errors[0].message) 
                        {
                            alert("Error message: " + errors[0].message);
                        }
                    }
                } 
                else 
                {
                    alert("Unknown error");
                }
            }
        );
        
        $A.enqueueAction(action);
    }
})

To retrieve the data I wrote an Apex class:

public class BookingController {
    @AuraEnabled
    public static List <Booking__c> fetchBookings() {
        //Qyery 20 recent bookings
        List <Booking__c> bookList = 
            [SELECT Id, Name, Booking_Name__c, Arrival_Date__c, Departure_Date__c, Agent__c 
             FROM Booking__c
             ORDER BY LastModifiedDate DESC
             LIMIT 20];
        //return list of bookings
        return bookList;
    }
}

The BookingController() function works perfectly fine. The data is retrieved correctly. However, when clicking the New button within one of the Service record views, the Lightning components appears (without data) on the screen, followed by an infinite loop of Success message.

I can't get my head around why the callback function that should receive a result set is not receiving any result and on top of that is being called in an endless loop.

Any idea?