0

I have overriden "New Event" standard action with a custom lightning component but i get an error while typing. Code below:

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,lightning:availableForFlowScreens,lightning:actionOverride"
                controller="PoCEventsManagement"
                access="global">

    <!-- Event Handlers -->
    <aura:handler value="{!this}" name="init" action="{!c.doInit}"/>

    <!--Define Attributes -->
    <aura:attribute name="eventRecord" type="Event" />
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="isActionClicked" type="Boolean" default="false"/>
    <aura:attribute name="spinner" type="boolean" default="true"/>

    <!-- If action is selected, show a modal!-->
    <aura:if isTrue="{!v.isActionClicked}">
        <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open slds-modal_small">
            <div class="slds-modal__container">
                <!--loading spinner start-->
                <div class="slds-is-relative">
                    <aura:if isTrue="{!v.spinner}">
                        <div aura:id="spinnerId" class="slds-spinner_container">
                            <div class="slds-spinner--brand  slds-spinner slds-spinner--medium slds-is-relative" role="alert">
                                <span class="slds-assistive-text">Loading</span>
                                <div class="slds-spinner__dot-a"></div>
                                <div class="slds-spinner__dot-b"></div>
                            </div>
                        </div>
                    </aura:if>

                    <!--Loading spinner end--> 
                    <header class="slds-modal__header">
                        <lightning:buttonIcon iconName="utility:close"
                                              onclick="{! c.closeModal }"
                                              alternativeText="close"
                                              variant="bare-inverse"
                                              class="slds-modal__close"/>
                        <h2 id="modal-heading-01" class="slds-text-heading_small slds-hyphenate">New Event</h2>
                    </header>
                    <div class="slds-modal__content slds-p-around_small slds-wrap"  id="modal-content-id-1">
                        <!--26/03/2020 George Galaios: Subject-->
                        <lightning:input aura:id="evtSubject" name="evtSubject" label="Subject" value="{!v.eventRecord.Subject}" required="true" class="slds-size--1-of-2 slds-p-horizontal_x-small" />

                        <!--26/03/2020 George Galaios: Assigned to-->
                        <lightning:input aura:id="evtAssignedTo" name="evtAssignedTo" label="OwnerId" value="{!v.eventRecord.OwnerId}" required="true" class="slds-size--1-of-2 slds-p-horizontal_x-small" />

                        <!--26/03/2020 George Galaios: Start Datetime-->
                        <lightning:input aura:id="evtStart" type="Datetime" name="evtStart" label="Start" value="{!v.eventRecord.StartDateTime}" year="2-digit" month="short" day="2-digit" hour="numeric"
                                         weekday="long" minute="2-digit" class="slds-size--1-of-2 slds-p-horizontal_x-small" />

                        <!--26/03/2020 George Galaios: End Datetime-->
                        <lightning:input aura:id="evtEnd" type="Datetime" name="evtEnd" label="End" value="{!v.eventRecord.EndDateTime}" year="2-digit" month="short" day="2-digit" hour="numeric"
                                         weekday="long" minute="2-digit" class="slds-size--1-of-2 slds-p-horizontal_x-small" />

                        <div class="slds-col slds-size_1-of-1">
                            <div class="slds-p-top_medium"></div>
                        </div>


                    </div>
                    <footer class="slds-modal__footer">
                        <lightning:button iconName="utility:undo"
                                          iconPosition="right"
                                          variant="neutral" 
                                          label="Cancel Flow"
                                          title="Cancel Flow"
                                          onclick="{! c.closeModal }"/>

                                <lightning:button iconName="utility:send"
                                                  variant="brand" 
                                                  label="Done"
                                                  title="Done"
                                                  onclick="{!c.saveRecord }"/>
                    </footer>
                </div>
            </div>
        </section>
        <div class="slds-backdrop slds-backdrop_open"></div>
    </aura:if>

</aura:component>

Controller js

({
    //@ggalaios 26/03/2020 function on page load
    doInit: function(component, event, helper) {
        console.log('doInit.');
        component.set("v.isActionClicked", true);
        console.log('ok done');
        component.set('v.spinner', false);
    },

    //@ggalaios 26/03/2020 function to close modal flow
    closeModal: function(component, event, helper) {
        component.set('v.spinner', false);
        component.set('v.isActionClicked', false);
        var recId = component.get("v.recordId");
        if (!recId) {
            var homeEvt = $A.get("e.force:navigateToObjectHome");
            homeEvt.setParams({
                "scope": "Event"
            });
            homeEvt.fire();
        } else {
            helper.navigateTo(component, recId);
        }
    },


    saveRecord : function(component, event, helper) {
        console.log('called!');
        component.set("v.eventRecord.Subject", component.find('evtSubject').get("v.value"));
        component.set("v.eventRecord.OwnerId", component.find('evtAssignedTo').get("v.value"));

        component.set("v.eventRecord.StartDateTime", component.find('evtStart').get("v.value"));
        component.set("v.eventRecord.EndDateTime", component.find('evtEnd').get("v.value"));

        console.log('after cmps. set!');


        //Call Apex to get the Questions for the current Page
        var saveEvent = component.get("c.saveEvent");
        saveEvent.setParams({"currentEvent" : component.get('v.eventRecord')});
        saveEvent.setCallback(this, function(response) {
            var saveEventState = response.getState();
            if(saveEventState==="SUCCESS"){
                var resultsToast = $A.get("e.force:showToast");
                resultsToast.setParams({
                    "title": "Saved",
                    "message": "The record was saved."
                });
                resultsToast.fire();
                var evt = response.getReturnValue();
                var recId = evt.Id;
                helper.navigateTo(component, recId);
            } else if(saveEventState == "INCOMPLETE"){
                component.set("v.spinner", false);
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Oops!",
                    "message": "No Internet Connection",
                    type: "warning",
                    mode: "sticky"
                });
                toastEvent.fire();
            } else if(saveEventState == "ERROR"){
                component.set("v.spinner", false);
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Error Occured!",
                    "message": "Please contact your Administrator and try again later.",
                    type: "error",
                    mode: "sticky"
                });
                toastEvent.fire();                    
            }
        });
        $A.enqueueAction(saveEvent);
    }
})

Helpr Js

({
    navigateTo: function(component, recId) {
        var navEvt = $A.get("e.force:navigateToSObject");
        navEvt.setParams({
            "recordId": recId
        });
        navEvt.fire();
        window.setTimeout(
            $A.getCallback(function() {
                $A.get('e.force:refreshView').fire();
            }), 2000
        );
    }
})

Apex Controller

public class PoCEventsManagement {
    public static Event saveEvent (Event currentEvent) {
        //try{
            insert currentEvent;
            return currentEvent;
        /*} catch (Exception e) {
            throw new AurahandledException('Could not save record');
        }*/
    }
}

The error i get is This page has an error. You might just need to refresh it. First, would you give us some details? (We're reporting this as error ID: -491200357). Any help would be appreciated

0

Finally i found solution... I called an apex method from init js class to instantiate an event record! Code below

Component

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,lightning:availableForFlowScreens,lightning:actionOverride"
                controller="PoCEventsManagement"
                access="global">

    <!-- Event Handlers -->
    <aura:handler value="{!this}" name="init" action="{!c.doInit}"/>

    <!--Define Attributes -->
    <aura:attribute name="eventRecord" type="Event" />
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="isActionClicked" type="Boolean" default="false"/>
    <aura:attribute name="spinner" type="boolean" default="true"/>

    <!-- If action is selected, show a modal!-->
    <aura:if isTrue="{!v.isActionClicked}">
        <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open slds-modal_small">
            <div class="slds-modal__container">
                <!--loading spinner start-->
                <div class="slds-is-relative">
                    <aura:if isTrue="{!v.spinner}">
                        <div aura:id="spinnerId" class="slds-spinner_container">
                            <div class="slds-spinner--brand  slds-spinner slds-spinner--medium slds-is-relative" role="alert">
                                <span class="slds-assistive-text">Loading</span>
                                <div class="slds-spinner__dot-a"></div>
                                <div class="slds-spinner__dot-b"></div>
                            </div>
                        </div>
                    </aura:if>

                    <!--Loading spinner end--> 
                    <header class="slds-modal__header">
                        <lightning:buttonIcon iconName="utility:close"
                                              onclick="{! c.closeModal }"
                                              alternativeText="close"
                                              variant="bare-inverse"
                                              class="slds-modal__close"/>
                        <h2 id="modal-heading-01" class="slds-text-heading_small slds-hyphenate">New Event</h2>
                    </header>
                    <div class="slds-modal__content slds-p-around_small slds-wrap"  id="modal-content-id-1">
                        <!--26/03/2020 George Galaios: Subject-->
                        <lightning:input aura:id="evtSubject" name="evtSubject" label="Subject" value="{!v.eventRecord.Subject}" required="true" class="slds-size--1-of-2 slds-p-horizontal_x-small" />

                        <!--26/03/2020 George Galaios: Assigned to-->
                        <lightning:input aura:id="evtAssignedTo" name="evtAssignedTo" label="OwnerId" value="{!v.eventRecord.OwnerId}" required="true" class="slds-size--1-of-2 slds-p-horizontal_x-small" />

                        <!--26/03/2020 George Galaios: Start Datetime-->
                        <lightning:input aura:id="evtStart" type="Datetime" name="evtStart" label="Start" value="{!v.eventRecord.StartDateTime}" year="2-digit" month="short" day="2-digit" hour="numeric"
                                         weekday="long" minute="2-digit" class="slds-size--1-of-2 slds-p-horizontal_x-small" />

                        <!--26/03/2020 George Galaios: End Datetime-->
                        <lightning:input aura:id="evtEnd" type="Datetime" name="evtEnd" label="End" value="{!v.eventRecord.EndDateTime}" year="2-digit" month="short" day="2-digit" hour="numeric"
                                         weekday="long" minute="2-digit" class="slds-size--1-of-2 slds-p-horizontal_x-small" />

                        <div class="slds-col slds-size_1-of-1">
                            <div class="slds-p-top_medium"></div>
                        </div>

                        <!-- Code for Address Input -->
                        <lightning:inputAddress aura:id="evtAddress" name="evtAddress" addressLabel="Address"
                                                showAddressLookup="True"
                                                streetLabel="Street"
                                                cityLabel="City"
                                                countryLabel="Country"
                                                postalCodeLabel="PostalCode"
                                                street="{!v.eventRecord.Street__c}"
                                                city="{!v.eventRecord.City__c}"
                                                country="{!v.eventRecord.Country__c}"
                                                postalCode="{!v.eventRecord.Postal_Code__c}"
                                                class="slds-size--1-of-2 slds-p-horizontal_x-small">
                        </lightning:inputAddress>
                        <!--End of Code for Address Input -->
                    </div>
                    <footer class="slds-modal__footer">
                        <lightning:button iconName="utility:undo"
                                          iconPosition="right"
                                          variant="neutral" 
                                          label="Cancel Flow"
                                          title="Cancel Flow"
                                          onclick="{! c.closeModal }"/>

                                <lightning:button iconName="utility:send"
                                                  variant="brand" 
                                                  label="Done"
                                                  title="Done"
                                                  onclick="{!c.saveRecord }"/>
                    </footer>
                </div>
            </div>
        </section>
        <div class="slds-backdrop slds-backdrop_open"></div>
    </aura:if>

</aura:component>

Js Controller

({
    //@ggalaios 26/03/2020 function on page load
    doInit: function(component, event, helper) {
        console.log('doInit.');
        component.set("v.isActionClicked", true);
        console.log('ok done');
        component.set('v.spinner', false);
        //Call Apex to Instantiate a new event
        var createEvent = component.get("c.createNewEvent");
        createEvent.setCallback(this, function(response) {
            var createEventState = response.getState();
            if(createEventState==="SUCCESS"){
                var newevt = response.getReturnValue();
                component.set("v.eventRecord", newevt);
                console.log('hey, event record is instantiated!');
            } else if(createEventState == "INCOMPLETE"){
                component.set("v.spinner", false);
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Oops!",
                    "message": "No Internet Connection",
                    type: "warning",
                    mode: "sticky"
                });
                toastEvent.fire();
            } else if(createEventState == "ERROR"){
                component.set("v.spinner", false);
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Error Occured!",
                    "message": "Please contact your Administrator and try again later.",
                    type: "error",
                    mode: "sticky"
                });
                toastEvent.fire();                    
            }
        });
        $A.enqueueAction(createEvent);
    },

    //@ggalaios 26/03/2020 function to close modal flow
    closeModal: function(component, event, helper) {
        component.set('v.spinner', false);
        component.set('v.isActionClicked', false);
        var recId = component.get("v.recordId");
        if (!recId) {
            var homeEvt = $A.get("e.force:navigateToObjectHome");
            homeEvt.setParams({
                "scope": "Event"
            });
            homeEvt.fire();
        } else {
            helper.navigateTo(component, recId);
        }
    },


    saveRecord : function(component, event, helper) {
        console.log('called!');
        component.set("v.eventRecord.Subject", component.find('evtSubject').get("v.value"));
        component.set("v.eventRecord.OwnerId", component.find('evtAssignedTo').get("v.value"));

        component.set("v.eventRecord.StartDateTime", component.find('evtStart').get("v.value"));
        component.set("v.eventRecord.EndDateTime", component.find('evtEnd').get("v.value"));
        /*ADDRESS INFORMATION SECTION*/
        component.set("v.eventRecord.ShippingStreet", component.find('evtAddress').get("v.Street__c"));
        component.set("v.eventRecord.ShippingCity", component.find('evtAddress').get("v.City__c"));
        component.set("v.eventRecord.ShippingCountry", component.find('evtAddress').get("v.Country__c"));
        component.set("v.eventRecord.ShippingPostalCode", component.find('evtAddress').get("v.Postal_Code__c"));
        console.log('after cmps. set!');


        //Call Apex to save the Event record
        var saveEvent = component.get("c.saveEvent");
        saveEvent.setParams({"currentEvent" : component.get('v.eventRecord')});
        saveEvent.setCallback(this, function(response) {
            var saveEventState = response.getState();
            if(saveEventState==="SUCCESS"){
                var resultsToast = $A.get("e.force:showToast");
                resultsToast.setParams({
                    "title": "Saved",
                    "message": "The record was saved."
                });
                resultsToast.fire();
                var evt = response.getReturnValue();
                var recId = evt.Id;
                helper.navigateTo(component, recId);
            } else if(saveEventState == "INCOMPLETE"){
                component.set("v.spinner", false);
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Oops!",
                    "message": "No Internet Connection",
                    type: "warning",
                    mode: "sticky"
                });
                toastEvent.fire();
            } else if(saveEventState == "ERROR"){
                component.set("v.spinner", false);
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Error Occured!",
                    "message": "Please contact your Administrator and try again later.",
                    type: "error",
                    mode: "sticky"
                });
                toastEvent.fire();                    
            }
        });
        $A.enqueueAction(saveEvent);
    }
})

Helper

({
    navigateTo: function(component, recId) {
        var navEvt = $A.get("e.force:navigateToSObject");
        navEvt.setParams({
            "recordId": recId
        });
        navEvt.fire();
        window.setTimeout(
            $A.getCallback(function() {
                $A.get('e.force:refreshView').fire();
            }), 2000
        );
    }
})

Apex Controller

public class PoCEventsManagement {
    @AuraEnabled
    public static Event saveEvent (Event currentEvent) {
        //try{
            insert currentEvent;
            return currentEvent;
        /*} catch (Exception e) {
            throw new AurahandledException('Could not save record');
        }*/
    }

    @AuraEnabled
    public static Event createNewEvent() {
        Event evt = new Event();
        return evt;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.