+ Start a Discussion

Create record from a lightning component


We were trying to get input from user through submit form in a lightning component and with that input values create a record in a custom object. We got the input by the following code:

    <aura:attribute name="custDetail" type="CustomerDetail__c[]"></aura:attribute>
    <aura:attribute name="customerInfo"  type="CustomerDetail__c" default="{            sobjectType: 'CustomerDetail__c' }" />
    <force:inputField value="{!v.customerInfo.Name}"/>
    <ui:button label="Edit Record" press="{!c.saveDetails}"/>

Our jscontroller looks like the following

    saveDetails : function(component, event, helper) 
    var dets = component.get("v.customerInfo.Name");
        //console.log('dets::'  + dets);

We get the name in the alert correctly when we submit.

Next the helper is as following

    save: function(component, dets) {
    //Save the expense and update the view
    this.upsertDet(component, dets, function(a) {
        var dets3 = component.get("v.custDetail");
        component.set("v.custDetail", dets3);
upsertDet : function(component, dets, callback) {
  var action = component.get("c.saveDetailsGiven");
      "dets": dets
  if (callback) {
      action.setCallback(this, callback);

Apex class::

public class saveCusDetails {
    public static string saveDetailsGiven(CustomerDetail__c details) {
    upsert details;
        system.debug('#####'+ details);
    return details.Id;

There are no errors but the record is not getting created. Any help would be appreciated.

Thank you
sharathchandra thukkanisharathchandra thukkani
dets variable is a string and you are passing the string to saveDetailsGiven method which accepts CustomerDetail__c as parameter you need to use the JSON.Stringfy method to pass the object to the method. Please check the trailhead.
We did it by passing the entire object to the apex controller and upserting it.
Francesco Carmagnola 10Francesco Carmagnola 10
You can try using this Lightning Component I am currently developing, maybe can fit your needs!


Arun Garg 9Arun Garg 9
Refer this link to display Accounts record using lightning components.