1

I'm trying to figure out how to display a list of objects (and just a few certain fields) in a lightning component and expose those fields for editing. What my full app is supposed to do is clone a custom object and it's children and then open a screen for editing them all at once. I can't figure out how to get my save button to actually save the field edits. Please let me know if you can help.

Component:

<aura:component controller="EventCloneController" implements="force:lightningQuickAction,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" >
<!-- This is automatically populated because of force:hasRecordId above -->
<aura:attribute name="recordId" type="Id" />

    <!-- This is where the events are held -->
<aura:attribute name="Events" type="Event__c[]"/>
<aura:attribute name="EventLength" type="Integer"/>

<!-- Calls the JS controller which calls the Apex controller on load. Name must = init to get this to run on load -->
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />


<!-- Show alerts -->
<aura:if isTrue="{!(!empty(v.Events))}">  
        <div class="" style="width:100%;">
            <h1 class="alertText" >Enter dates for each Event:&nbsp;</h1> 
          <!--  create form to take input -->
            <form class="slds-form--stacked">
            <div>
                <ui:button press="{!c.saveEdits}" label="Clone Event"/>
            </div>
                <div class="slds-form-element slds-is-required">
                    <div class="slds-form-element__control">    

                      <aura:iteration items="{!v.Events}" var="event" indexVar="i"> 
                          <div class="slds-col slds-m-around--medium"> 

                                 <ui:inputText aura:id="Name" label="Name"
                                      class="slds-input"
                                      labelClass="slds-form-element__label"
                                      value="{!event.Name}"
                                      required="false"
                                      aura:Id = "EventName" /><br />
                                <ui:inputDateTime aura:id="startdate" label="Event Start Date: "
                                      class="slds-input"
                                      labelClass="slds-form-element__label"
                                      value="{!event.Start_Date_Time__c}"
                                      displayDatePicker="true"
                                      required="false"
                                      aura:Id = "EventStart" /> 
                                <ui:inputDateTime aura:id="enddate" label="Event End Date: "
                                      class="slds-input"
                                      labelClass="slds-form-element__label"
                                      value="{!event.End_Date_Time__c}"
                                      displayDatePicker="true"
                                      required="false"
                                      aura:Id = "EventEnd" /> 
                            </div>
                        </aura:iteration>
                    </div>
                </div>

        </form>

    </div>

</aura:if>

</aura:component>

JS Controller:

({
doInit : function(component, event) {
    var action = component.get("c.cloneEvents");
    action.setParams({
        "recordId": component.get("v.recordId")
    });
    // Register the callback function
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var results = response.getReturnValue();
            component.set("v.Events", results);
            component.set("v.EventLength", results.length);
        }
    });
    // Invoke the service
    $A.enqueueAction(action);
},

saveEdits : function(component, event) {
    var action = component.get("c.saveEdits");
    action.setParams({
        "eventList": component.get("v.Events")
    });
    // Register the callback function
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var results = response.getReturnValue();
            var location = "/one/one.app?#/sObject/" + results + "/view";
            parent.window.location.href=location;
        } else {
            alert(state);
        }
    });
    // Invoke the service
    $A.enqueueAction(action);
}

})

Apex Controller:

public class EventCloneController {
    @AuraEnabled
    public static List<Event__c> cloneEvents(ID recordId){
        List<Event__c> eventsToAdd = new List<Event__c>();
        try{
            List<Event__c> events = [Select Id, Name, Start_Date_Time__c, End_Date_Time__c, Parent_Event__c  
            FROM Event__c WHERE ID = :recordId OR Parent_Event__c = :recordId];

            Event__c firstEvent;
            ID newRecordId;

            for(Event__c event : events){
                if(event.Parent_Event__c == null){
                    Event__c newEvent2 = event.clone();                 
                    insert newEvent2;
                    newRecordId = newevent2.id;
                    firstEvent = newEvent2;
                    eventsToAdd.add(newEvent2);
                }
                break;
            }
            for(Event__c event : events){
                Event__c newEvent = event.clone();

                if(event.Parent_Event__c != null){
                    newEvent.Parent_Event__c = newRecordId;
                    eventsToAdd.add(newEvent);              
                }
            }

            if(eventsToAdd.size() > 0){
                upsert eventsToAdd;
            }

            return eventsToAdd;
        } catch(Exception e){
            String errorMessage = 'Error: ' + e.getMessage() + ' ' + e.getStackTraceString();
            system.debug(errorMessage);
            return eventsToAdd;
        }


    }

    @AuraEnabled
    public static String saveEdits(List<Event__c> eventList){
        String parentId = '';
        try{
            for(Event__c e : eventList){
                if(e.Parent_Event__c == null){
                    parentId = e.id;
                    break;
                }
            }
            update eventList;
        } catch(Exception e){
            String errorMessage = 'Error: ' + e.getMessage() + ' ' + e.getStackTraceString();
            system.debug(errorMessage);
        }

        return parentId;
    }
}
2
  • I do not understand your problem, your code looks OK in general, this approach should work. Can you explain better why this does not work? Jul 10, 2017 at 5:06
  • When I change one of the input fields for the record and then click my button, nothing happens. The Event__c record just stays the same.
    – melsherm3
    Jul 11, 2017 at 15:17

1 Answer 1

1

I figured it out! My JS and Apex methods had the same name. Doh!

You must log in to answer this question.

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