You need to sign in to do that
Don't have an account?
Biswojeet 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]
<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]
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:
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
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:
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
Hi Khan Anas,
Got it. Thanks for clarifying.
Thank you so much...
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