function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kajal rathodkajal rathod 

Create Custom object Records Using Lightning Component

Hi ,
I just sarted learning lightning and i am trying to insert data in my custom object from client side to server side but i m not able to .I try to debug also but it is showing value as undefined .
component----
<aura:component controller='TraningDetailsServer'>
    <aura:attribute name='TraningDetails' Type='List'  default="{ 'sobjectType': 'TraningDetails__c',
                     'Name': ' ',
                      'Employee ID': ' ',                                                       
                   }"/>
 
    <lightning:input name="TraningDetails" label="Name" value='{!v.TraningDetails.Name__c}'/>
    <lightning:input name="TraningDetails" label="Employee ID" value='{!v.TraningDetails.Emp_ID__c}'/>

    
  <ui:button label="Submit" 
                   class="slds-button slds-button--neutral"
                   labelClass="label"
                   press="{!c.getDetails}"/>
 
</aura:component>

controller---
({
    getDetails : function(component, event, helper) {
    var DetailsCmp=component.get("v.TraningDetails");
    //var item = JSON.parse(JSON.stringify(DetailsCmp));
    var action=component.get("c.TraningDetailsServer");
    console.log(DetailsCmp);
        var i;
        for(i in DetailsCmp)
        action.setParams({ 
            "xyz": JSON.stringify(DetailsCmp[i].Name)
    });
   
        action.setCallback(this, function(a) {
           var state = a.getState();
           console.log(state);
            if (state === "SUCCESS") {
                var DetailsCall = a.getReturnValue();
                alert("hello from here"+DetailsCall.Name);
              
            }
        });
        $A.enqueueAction(action)
    }
})

Apex class---
public with sharing class TraningDetailsServer {
   @AuraEnabled
    
    public static List<TraningDetails__c> TraningDetailsServer(List<TraningDetails__c> xyz)
    {
    
      insert xyz;
      return xyz;   
    }
}
 can some body help me with what is wrong in my code
kajal rathodkajal rathod
undefined values
Dhanik L SahniDhanik L Sahni
Have you checked that record is created or not?
As per image, your code is executed and it is successful. As you are returning List, please try to iterate and then check DetailsCall.Name with record index.
Dhanik L SahniDhanik L Sahni
You can also use Lightning Data Service. I have used this only. Please check at https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service.htm
kajal rathodkajal rathod
Yes i checked but records are not getting created and the value is coming from client side but it is not getting stored to server side controller as i m debugging apex code the value for "xyz" is undefined  :-(  , but in DetailsCmp value is coming .