0

I am new to Salesforce Lightning. Have been stuck with this error in various use cases. Trying a simple code to insert a contact. Getting this error every time. This page has an error. You might just need to refresh it.

Action failed: c:CreateContact$controller$saveContact [Cannot read property 'get' of undefined] Failing descriptor: {c:CreateContact$controller$saveContact}

Apex

public class ContactListController {
    @AuraEnabled
    public static contact doSaveContact(contact con,string accid)

    {
        con.AccountId=accid;
        insert con;
        return con;
    }

}

Component

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="ContactListController" >

<aura:attribute name ="createContact" type="Contact" default="{                                                                  
     sobjectname:'contact',
     FirstName:'', 
     LastName:'',
     Email:'', 
     Phone:'' }"/>

<div class="slds-p-around_small">
        <lightning:input name ="FirstName" required="true" value="{!v.createContact.FirstName}" label="First Name"/>
        <lightning:input name ="LastName" required="true" value="{!v.createContact.LastName}" label="Last Name"/>
        <lightning:input name ="Email" required="true" value="{!v.createContact.Email}" label="Email"/>
        <lightning:input name ="phone" required="true" value="{!v.createContact.Phone}" label="phone"/>
        <lightning:button variant="brand" label="Save Contact" title="Save Contact" onclick="{!c.saveContact}"/>
    </div>
</aura:component>

JS

({
    saveContactInvoke : function(component, event, helper) {
        var action=component.get('c.doSaveContact');
        action.setParams({
            "con":component.get('v.createContact'),
            "accid":"0012v00002S8Y1hAAF"
        }
        );
        action.setCallback(this, function(response){
            var state=response.getState(); 
            if(state=="SUCCESS")
            {
                var responseValue=response.getReturnValue();
                console.log('responseValue' +responseValue);
            }
        });
          $A.enqueueAction(action,false);  
        }
})
  • On the onlcick event you are calling 'saveContact' , but in the controller you have defined 'saveContactInvoke' – Manish Anand Dec 13 '19 at 13:47
2

I think your function name is incorrect:

saveContactInvoke : function(component, event, helper)

Should be: saveContact : function(component, event, helper)

| improve this answer | |

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.