You need to sign in to do that
Don't have an account?
SHAIK MOULALI369
By using Apex class to Insert Contacts and return the inserted contacts
Using Apex class to Insert contact and return inserted contact values, i have write component inside a component.
I am getting a Error as :
" Action failed: c:QuickContact$controller$dosave [action.setparams is not a function] Failing descriptor: {c:QuickContact$controller$dosave} "
Kindly Suggest how to resolve this Issue.
Apex Class :
public class ContactListController {
@auraEnabled
public static contact createContact(Contact con, Id AccountId){
con.AccountId = AccountId;
insert con;
return con;
}
}
Comp 1 :
<aura:component Controller="ContactListController"
implements="force:hasRecordId,force:hasSobjectName,flexipage:availableForAllPageTypes" >
<aura:attribute name="ContactList" type="Contact[]"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<c:QuickContact accountId ="{!v.recordId}" />
<div class="slds-p-around_xxx-small">
<div class="slds-grid slds-wrap">
<aura:iteration items="{!v.ContactList}" var="con">
<div class="slds-col slds-size_1-of-3 slds-p-around_xxx-small">
<lightning:card footer="{!con.Email}" title="{!con.LastName}" iconName="action:add_contact">
<aura:set attribute="actions">
<lightning:button name="{!con.Id}" variant="brand" label="View Details" onclick="{!c.DoRedirect}"/>
</aura:set>
<p class="slds-p-horizontal_xxx-small">
{!con.FirstName} {!con.LastName} <br/>
{!con.phone}
</p>
</lightning:card>
</div>
</aura:iteration>
</div>
</div>
</aura:component>
Comp 2 :
<aura:component Controller="ContactListController">
<aura:attribute name="accountId" type="String" />
<aura:attribute name="CreateContact" type="Contact" default="{
SObjectName : 'Contact',
FirstName : '',
LastName : '',
Email : '',
Phone : ''
}"/>
<div class='slds-p-around_xx-small'>
<lightning:input type="text" value="{!v.CreateContact.FirstName}" label="First Name" required="true"/>
<lightning:input type="text" value="{!v.CreateContact.LastName}" label="Last Name" required="true"/>
<lightning:input type="Email" value="{!v.CreateContact.Email}" label="Email ID" required="true"/>
<lightning:input type="Phone" value="{!v.CreateContact.Phone}" label="Phone Number" required="true"/><br/>
<lightning:button label="Create Contact" variant="brand" onclick="{!c.dosave}"/>
</div>
</aura:component>
Controller code for comp 2 :
({
dosave : function(component, event, helper) {
var action = component.get('c.createContact');
action.setparams({
con : component.get('v.CreateContact'),
AccountId : component.get('v.accountId')
});
action.setcallback(this,function(response){
var state = response.getState();
alert(state);
if(state === 'SUCCESS' || state ==='DRAFT'){
var responseValue = response.getReturnValue();
}else if(state === 'INCOMPLETE'){
}else if(state === 'ERROR'){
}
}, 'ALL');
$A.enqueueAction(action);
}
})
I am getting a Error as :
" Action failed: c:QuickContact$controller$dosave [action.setparams is not a function] Failing descriptor: {c:QuickContact$controller$dosave} "
Kindly Suggest how to resolve this Issue.
Apex Class :
public class ContactListController {
@auraEnabled
public static contact createContact(Contact con, Id AccountId){
con.AccountId = AccountId;
insert con;
return con;
}
}
Comp 1 :
<aura:component Controller="ContactListController"
implements="force:hasRecordId,force:hasSobjectName,flexipage:availableForAllPageTypes" >
<aura:attribute name="ContactList" type="Contact[]"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<c:QuickContact accountId ="{!v.recordId}" />
<div class="slds-p-around_xxx-small">
<div class="slds-grid slds-wrap">
<aura:iteration items="{!v.ContactList}" var="con">
<div class="slds-col slds-size_1-of-3 slds-p-around_xxx-small">
<lightning:card footer="{!con.Email}" title="{!con.LastName}" iconName="action:add_contact">
<aura:set attribute="actions">
<lightning:button name="{!con.Id}" variant="brand" label="View Details" onclick="{!c.DoRedirect}"/>
</aura:set>
<p class="slds-p-horizontal_xxx-small">
{!con.FirstName} {!con.LastName} <br/>
{!con.phone}
</p>
</lightning:card>
</div>
</aura:iteration>
</div>
</div>
</aura:component>
Comp 2 :
<aura:component Controller="ContactListController">
<aura:attribute name="accountId" type="String" />
<aura:attribute name="CreateContact" type="Contact" default="{
SObjectName : 'Contact',
FirstName : '',
LastName : '',
Email : '',
Phone : ''
}"/>
<div class='slds-p-around_xx-small'>
<lightning:input type="text" value="{!v.CreateContact.FirstName}" label="First Name" required="true"/>
<lightning:input type="text" value="{!v.CreateContact.LastName}" label="Last Name" required="true"/>
<lightning:input type="Email" value="{!v.CreateContact.Email}" label="Email ID" required="true"/>
<lightning:input type="Phone" value="{!v.CreateContact.Phone}" label="Phone Number" required="true"/><br/>
<lightning:button label="Create Contact" variant="brand" onclick="{!c.dosave}"/>
</div>
</aura:component>
Controller code for comp 2 :
({
dosave : function(component, event, helper) {
var action = component.get('c.createContact');
action.setparams({
con : component.get('v.CreateContact'),
AccountId : component.get('v.accountId')
});
action.setcallback(this,function(response){
var state = response.getState();
alert(state);
if(state === 'SUCCESS' || state ==='DRAFT'){
var responseValue = response.getReturnValue();
}else if(state === 'INCOMPLETE'){
}else if(state === 'ERROR'){
}
}, 'ALL');
$A.enqueueAction(action);
}
})
setParams, setCallback are case sensitive but you have declared as setparams, setcallback
Thanks,
Maharajan.C
All Answers
setParams, setCallback are case sensitive but you have declared as setparams, setcallback
Thanks,
Maharajan.C
Hi,
You can take reference from this code.
Component
Controller:
Helper:
ThanksApex: