im new to lightning, i created a lightning component to insert contact records,but when i click on save record is not getting saved.here is my code

apex controller

public class cninsert { @AuraEnabled public static contact cinsert(contact cnt){ insert cnt; return cnt; } }

contact cmp

<aura:component controller="cninsert" implements="flexipage:availableForAllPageTypes" access="global" > <aura:attribute name="cont" type="contact" default="{'sobjectType' : 'contact'}"/> <lightning:input label="first name" value="{!v.cont.firstname}"/> <lightning:input label="last name" value="{!v.cont.lastname}"/> <lightning:button variant="brand" label="save" onclick="{!c.createcontact}"/> </aura:component>

contact controller.js

({ createcontact : function(component, event, helper) { var action = component.get("c.cinsert"); action.setParams({ "cnt" : component.get("v.cont") }); action.setCallback(this , function(response){ var state = response.getState(); var cn = response.getReturnValue(); alert('eferferfer'+state) if(state === "SUCCESS"){ component.set("v.cont",cn); alert("saved successfully"); } }); $A.enqueueAction(action); } })

  1. Try using case sensitive FirstName and LastName for contact fields.

  2. Put debug in apex so that you know what is coming in apex and if at all apex methid is being invoked. Also add try catch in apex.

    public static contact  cinsert(contact cnt){
            System.debug('Contact => '+cnt);
            insert cnt;
            return cnt;
        catch(Exception ex){
            System.debug('Contact => '+ex.getMessage());
            throw new AuraHandledException(ex.getMessage());
  1. Add the else block when state is not SUCCESS
    if (state === "SUCCESS") {
                alert("saved successfully");
            else if (state === "INCOMPLETE") {
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.error("Error message: " + 
                } else {
                    console.error("Unknown error");

Now keep checking the browser log and apex log (in dev console) - it will tell you exactly what the problem is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.