Lightning Components – How to Update Field

Let me preface this by saying that I'm new to developing on the Salesforce platform. I'm building a simple Lightning component where I want to query and display fields from a custom object, and then allow the user to update the field and submit their changes, thus updating the field on the object. Right now I'm simply trying to update the Name field on my custom object just to get something working and then build from there.

It appears that when I click the Save button, the save function fires on the client side with no errors, but nothing is written to the actual object. That's what I've been able to garner so far.

I've followed a few Trailhead courses and read documentation on creating the sample Expense App, but I know I'm not connecting the dots correctly somewhere. Here's what I have built:

get4Dassessments.apxc

public class get4Dassessments {
    @AuraEnabled 
    public static X4D_Assessment__c getAssessments(){
        return (X4D_Assessment__c) Database.query( ' SELECT Name, Customer_Acquisition__c FROM X4D_Assessment__c LIMIT 1 ' )[0];
    }

    @AuraEnabled 
    public static X4D_Assessment__c saveAssessment(X4D_Assessment__c assessment){
        upsert assessment;
        return assessment;
    }  

}

sparkAssessmentController.js

({
init : function(component, event, helper) {
    var action = component.get("c.getAssessments");

    action.setParams({
        "assessmentId": component.get("v.recordId")
    });

    // Register the callback function
    action.setCallback(this, function(data) {
        component.set("v.assessments", data.getReturnValue());
    });

    // Invoke the service
    $A.enqueueAction(action);
}, 
save : function(component, event, helper) {

    var action = component.get("c.saveAssessment");
    var name = component.get("v.assessments.Name"); //gets value of field

    console.log("Name:" + name);
    action.setParams({"Name": name});
    $A.enqueueAction(action);

    console.log('save ran');

}
})

sparkAssessment.cmp

 <!-- controller: get4Dassessments references the get4Dassessments.apxc Apex class -->
 <aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId" controller="get4Dassessments">

<!-- This event handler calls the init client-side controller to handle initialization. -->
<aura:handler name="init" action="{!c.init}" value="{!this}" />

<!-- make the list of assessments accessible to your component. -->
<aura:attribute name="assessments" type="X4D_Assessment__c" />

<!-- define the recordId attribute -->
<aura:attribute name="recordId" type="Id" />

<!-- reference LDS static resource -->
<ltng:require styles="/resource/slds103/assets/styles/salesforce-lightning-design-system.min.css"/>

 <form>
    <ui:inputText aura:id="client"
                  label="{!$ObjectType.X4D_Assessment__c.fields.Name.Label}"
                  class="form-control"
                  value="{!v.assessments.Name}"
                  placeholder="Put answer here"
    />
    <br />
    <ui:button class="form-control" aura:id="button" label="Save" press="{!c.save}"/>
</form>

 </aura:component>

Again, I'm a new Salesforce developer and I'm fairly certain I must be missing something obvious. Thank you in advance for any tips or guidance you can provide.

Best Answer

Turns out that I had a flaw in my logic in the save function. Here's what wound up working for me:

save : function(component, event, helper) {

    var action = component.get("c.saveAssessment");
    var assessments = component.get("v.assessments");

    action.setParams({"assessment": assessments});
    $A.enqueueAction(action);

    console.log('save ran');

}
Related Topic