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
Biswojeet Ray 11Biswojeet Ray 11 

I am just want to create a contact from component by use of apex controller but getting this error.

Component
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="ShowContactForAccount">
    <aura:attribute name="newContact" Type="Contact" default="{
                                                              sobjectName : 'Contact',
                                                              FirstName : '',
                                                              LastName : '',
                                                              Email : '',
                                                              Phone : ''
                                                              }"></aura:attribute>
    <aura:handler name="init" value="{!this}" action="{!c.showCOntactOnAccountPage}"/>
    <!--Create Contact-->
    <lightning:input type="text" label="First Name" value="{!v.newContact.FirstName}"/>
    <lightning:input type="text" label="Last Name" value="{!v.newContact.LastName}"/>
    <lightning:input type="email" label="Email" value="{!v.newContact.Email}"/>
    <lightning:input name="phone" label="Phone" value="{!v.newContact.Phone}"/>
    <lightning:button variant="success" label="Create Contact" title="Brand" onclick="{! c.createRecord }"/>
</aura:component>

Controller
    createRecord : function(component, event, helper) {
        alert(component.get('v.newContact'));
        var contact = component.get("v.newContact");
        var callMethod = component.get('v.createContact');
        callMethod.setParams({
            "accId" : component.get('v.recordId'),
            "con" : contact
        });
        callMethod.setCallback(this, function(response) {
            //var recs = response.getReturnValue();
            //component.set('v.contactList', recs);
        }, 'SUCCESS');
        $A.enqueueAction(callMethod);
    }
    
Apex Controller
public with sharing class ShowContactForAccount{

    @auraEnabled
    public Static Void createContact(String accId, Contact con){
    system.debug(con);
        con.AccountId = accId;
        Insert con;
    }
}

Error - Uncaught Action failed: c:MyShowRecordsComponentOnAccount$controller$createRecord [Cannot read property 'setParams' of undefined]
 
Best Answer chosen by Biswojeet Ray 11
Khan AnasKhan Anas (Salesforce Developers) 
Hi Biswojeet,

Greetings to you!

In the client-side controller, you need to use the value provider of c to invoke a server-side controller action. But you are using v.

Change your JS controller code:
({
    createRecord : function(component, event, helper) {
        alert(component.get('v.newContact'));
        var contact = component.get("v.newContact");
        var callMethod = component.get('c.createContact');
        callMethod.setParams({
            "accId" : component.get('v.recordId'),
            "con" : contact
        });
        callMethod.setCallback(this, function(response) {
            //var recs = response.getReturnValue();
            //component.set('v.contactList', recs);
        }, 'SUCCESS');
        $A.enqueueAction(callMethod);
    }
})

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Biswojeet,

Greetings to you!

In the client-side controller, you need to use the value provider of c to invoke a server-side controller action. But you are using v.

Change your JS controller code:
({
    createRecord : function(component, event, helper) {
        alert(component.get('v.newContact'));
        var contact = component.get("v.newContact");
        var callMethod = component.get('c.createContact');
        callMethod.setParams({
            "accId" : component.get('v.recordId'),
            "con" : contact
        });
        callMethod.setCallback(this, function(response) {
            //var recs = response.getReturnValue();
            //component.set('v.contactList', recs);
        }, 'SUCCESS');
        $A.enqueueAction(callMethod);
    }
})

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Biswojeet Ray 11Biswojeet Ray 11

Hi Khan Anas,

 

Got it. Thanks for clarifying. 

 

Thank you so much...

Deepali KulshresthaDeepali Kulshrestha
Hi Biswojeet, 

var callMethod = component.get('v.createContact'); 
here you have done a small fault on calling the apex method instead of "v" must wirte "c".

This is correct:-

  var callMethod = component.get('c.createContact'); 

Make sure 

 <aura:handler name="init" value="{!this}" action="{!c.showCOntactOnAccountPage}"/>

its "showCOntactOnAccountPage" action function must exist on controller.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha