+ Start a Discussion
EldonEldon 

Create record from a lightning component

Hi,

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");
        alert(dets);
        //console.log('dets::'  + dets);
        //debugger;
        
    helper.save(component,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");
        dets3.push(a.getReturnValue());
        console.log('here'+dets3);
        component.set("v.custDetail", dets3);
    });
},
upsertDet : function(component, dets, callback) {
  var action = component.get("c.saveDetailsGiven");
  action.setParams({ 
      "dets": dets
  });
  if (callback) {
      action.setCallback(this, callback);
  }
  $A.enqueueAction(action);
}
})


Apex class::

public class saveCusDetails {
     @AuraEnabled 
    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.
 
EldonEldon
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!

https://github.com/fracarma/Lightning-Object-Creator-Component

Thanks
Arun Garg 9Arun Garg 9
Refer this link to display Accounts record using lightning components.
http://www.salesforceadda.com/2017/09/create-and-display-account-records.html