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
SHAIK MOULALI369SHAIK 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}&nbsp; {!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);
    }
})
 
Best Answer chosen by SHAIK MOULALI369
Maharajan CMaharajan C
Hi Shaik,

setParams, setCallback are case sensitive but you have declared as setparams, setcallback
 
({
    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);
    }
})

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Shaik,

setParams, setCallback are case sensitive but you have declared as setparams, setcallback
 
({
    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);
    }
})

Thanks,
Maharajan.C
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47

Hi,

You can take reference from this code.

Component

<aura:component controller="Test" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="LastName" type="String"  />
    <lightning:input type="String" name="LastName" label="LastName" value="{!v.LastName}" />
    
    <lightning:button variant="success" label="Success" title="Success" onclick="{! c.handleClick }"/>

    <aura:attribute name="FromInsertedData" type="String" /> 
    <p>{!FromInsertedData}</p> 
    </aura:component>


Controller:
 

({
	handleClick : function(component, event, helper) {
		helper.helperMethod(component, event, helper);
	}
})

 Helper:

({
	helperMethod : function(component, event, helper) {
      
        var action=component.get("c.forContact");
        action.setParams({
            LN:component.get("{!v.LastName}")
        
        });
        action.setCallback(this,function(response){
            console.log(JSON.stringify(response.getReturnValue()));
            if(response.getReturnValue()!=null){
                alert("Your Contact is Inserted");
                component.set("v.FromInsertedData", response.getReturnValue().LastName);
            }
        });
        $A.enqueueAction(action);
	}
})


Apex:

public class Test{
      @AuraEnabled
    public static Contact forContact(String LN){
        Contact con=new Contact();
        con.LastName=LN;
        insert con;
        return con;
    }
}
Thanks