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
SFDC12SFDC12 

my requirement

Hi everyone,am trying to create a contact related to paritcular account in lightning .tried so far..,and embeded this component in accountrecord page ,but unable to save. can someone help me out .

Thanks in Advance.
apex:
public class QuickCreatecontact {
@AuraEnabled
    public static void createcontact(contact con,Id AccountId){
        con.AccountId=AccountId;
        insert con;
    }
}

component:
<aura:component controller="QuickCreatecontact" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <aura:attribute name="createcontact" type="contact"/>
    <aura:attribute name="accountId" type="string"/>
   <!-- <aura:handler name="init" value="{!this}" action="{!c.doinit}"/>-->
    <div>
        <lightning:input type="text" name="{!v.createcontact.LastName}" label="Enter LastName" />
        <lightning:input type="text" name="{!v.createcontact.FirstName}" label="Enter firstName"/>
        <lightning:input type="phone" name="{!v.createcontact.phone}" label="Enter phone"/>
        
    </div>
    <div>
        <lightning:button label="save" variant="brand" onclick="{!c.dosave}"/>
     </div>
</aura:component>

js:
({
    dosave : function(component, event, helper) {
        var action=component.get("c.createcontact");
        action.setParams({
            con:component.get("v.createcontact"),
            Id:component.get("v.accountId")
        });
        action.setCallback(this,function(response){
            var state=response.getState();
           
            if(state==='SUCCESS'){
                var result=response.getReturnValue();
               alert('SUCCESS');
                
               
            }else if(state==='INCOMPLETE'){
                
            }
        });
        $A.enqueueAction(action);
    }
})
Best Answer chosen by SFDC12
Maharajan CMaharajan C
Hi Anshi,

Am seeing the below problems in your code:

Problem 1:

Specify the default values:
 
<aura:attribute name="createcontact" type="contact" default="{ 
                                                                 SObjectName : 'Contact',
                                                                 FirstName : '',
                                                                 LastName : '',
                                                                 Phone : ''
                                                                 }"/>


Problem 2:

you have to use the value in lightning:input tag... But you are using the name to get the  input.
<div>
        <lightning:input type="text" value="{!v.createcontact.LastName}" label="Enter LastName" />
        <lightning:input type="text" value="{!v.createcontact.FirstName}" label="Enter firstName"/>
        <lightning:input type="phone" value="{!v.createcontact.Phone}" label="Enter phone"/>
</div>

Problem 3: 

setParams key needs to be the exact param name mentioned in apex class:

Also use the component.get("v.recordId") to get the current account Id. 
action.setParams({
            con:component.get("v.createcontact"),
            AccountId :component.get("v.recordId")
        });


Component File:
 
<aura:component controller="QuickCreatecontact" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <aura:attribute name="createcontact" type="contact" default="{ 
                                                                 SObjectName : 'Contact',
                                                                 FirstName : '',
                                                                 LastName : '',
                                                                 Phone : ''
                                                                 }"/>
    <aura:attribute name="accountId" type="string"/>
    <div>
        <lightning:input type="text" value="{!v.createcontact.LastName}" label="Enter LastName" />
        <lightning:input type="text" value="{!v.createcontact.FirstName}" label="Enter firstName"/>
        <lightning:input type="phone" value="{!v.createcontact.Phone}" label="Enter phone"/>
    </div>
    <div>
        <lightning:button label="save" variant="brand" onclick="{!c.dosave}"/>
    </div>
</aura:component>

JS File :
 
({
    dosave : function(component, event, helper) {
        var action=component.get("c.createcontact");
        action.setParams({
            con:component.get("v.createcontact"),
            AccountId :component.get("v.recordId")
        });
        action.setCallback(this,function(response){
            var state=response.getState();
            
            if(state==='SUCCESS'){
                var result=response.getReturnValue();
                alert('SUCCESS');
                
                
            }else if(state==='INCOMPLETE'){
                
            }
        });
        $A.enqueueAction(action);
    }
})


Thanks,
Maharajan.C

All Answers

SwethaSwetha (Salesforce Developers) 
HI Anshi,
Can you add the Lightning Inspector for Chrome and see what error it shows when you click save? 
Related: https://www.sfdckid.com/2019/08/salesforce-lightning-component-to-display-contacts.html
karimulla salesforcekarimulla salesforce
hi ,
 
@AuraEnabled
    public static Contact  CreatecontactRec(contact con, id Accountid){
     
        con.AccountId = Accountid;
         insert con;
        return con;
        
    }
 
<aura:component controller="ContactlistClass">
	 
    <aura:attribute name="recordid" type="string"/>
    <aura:attribute name="Createcontact"  type="contact"  default="{
                                                                    'sobjectname':'contact',
                                                                     'FirstName':'',
                                                                     'LastName':'',
                                                                     'Email':'',
                                                                      'Phone':'',
                                                                   }"/>
    
    
    <lightning:input type="text"  value="{!v.Createcontact.FirstName}" label="Enter Firstname"  required="true"/>
    <lightning:input type="text"  value="{!v.Createcontact.LastName}"  label="Enter Lastname"  required="true"/>
    <lightning:input type="text"  value="{!v.Createcontact.Email}"  label="Enter Email"  required="true"/>
    <lightning:input type="text"  value="{!v.Createcontact.Phone}"  label="Enter Phone"  required="true" />
        
    <br/>
    
     
    <lightning:button label="Create"  variant="brand"  onclick="{!c.CreateContactRecord}"/>
</aura:component>
 
({
    CreateContactRecord : function(component, event, helper) {
        alert('Entered in the Contact Creation');
        var action=component.get('c.CreatecontactRec');
        var recordid= component.get('v.recordid');
        alert(recordid);
        action.setParams({
            con:component.get('v.Createcontact'),
            Accountid:recordid
        });
        action.setCallback(this,function(response){
         var state = response.getState();
         alert(state);
            if(state === 'SUCCESS' || Sate === 'DRAFT' ){
                
             alert('SUCCESS');  
                
               var response = response.getReturnValue();
                
            } else if(state === 'ERROR'){
                
                alert('ERROR SOMETHING went wrong'); 
                
                 var errors  = response.getError();
                 console.log(errors);
            }
        },'ALL');
      
        $A.enqueueAction(action);
    }
})



Note: Dont forget to pass the {!v.Recordid} this is important, please do check you are getting the record id with alert 


Thanks and Regrads
karimulla syed
https://www.linkedin.com/in/karimulla-syed-191943153/
 
Suraj Tripathi 47Suraj Tripathi 47
Hi anshi,
can you provide me a Screenshot of your Error?
I guess you are not providing the correct account Id, make sure you should give a exact Id of a account record in your salesforce
If you find your Solution then mark this as the best answer.

Thank you!
Regards,
Suraj Tripathi

 
Maharajan CMaharajan C
Hi Anshi,

Am seeing the below problems in your code:

Problem 1:

Specify the default values:
 
<aura:attribute name="createcontact" type="contact" default="{ 
                                                                 SObjectName : 'Contact',
                                                                 FirstName : '',
                                                                 LastName : '',
                                                                 Phone : ''
                                                                 }"/>


Problem 2:

you have to use the value in lightning:input tag... But you are using the name to get the  input.
<div>
        <lightning:input type="text" value="{!v.createcontact.LastName}" label="Enter LastName" />
        <lightning:input type="text" value="{!v.createcontact.FirstName}" label="Enter firstName"/>
        <lightning:input type="phone" value="{!v.createcontact.Phone}" label="Enter phone"/>
</div>

Problem 3: 

setParams key needs to be the exact param name mentioned in apex class:

Also use the component.get("v.recordId") to get the current account Id. 
action.setParams({
            con:component.get("v.createcontact"),
            AccountId :component.get("v.recordId")
        });


Component File:
 
<aura:component controller="QuickCreatecontact" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <aura:attribute name="createcontact" type="contact" default="{ 
                                                                 SObjectName : 'Contact',
                                                                 FirstName : '',
                                                                 LastName : '',
                                                                 Phone : ''
                                                                 }"/>
    <aura:attribute name="accountId" type="string"/>
    <div>
        <lightning:input type="text" value="{!v.createcontact.LastName}" label="Enter LastName" />
        <lightning:input type="text" value="{!v.createcontact.FirstName}" label="Enter firstName"/>
        <lightning:input type="phone" value="{!v.createcontact.Phone}" label="Enter phone"/>
    </div>
    <div>
        <lightning:button label="save" variant="brand" onclick="{!c.dosave}"/>
    </div>
</aura:component>

JS File :
 
({
    dosave : function(component, event, helper) {
        var action=component.get("c.createcontact");
        action.setParams({
            con:component.get("v.createcontact"),
            AccountId :component.get("v.recordId")
        });
        action.setCallback(this,function(response){
            var state=response.getState();
            
            if(state==='SUCCESS'){
                var result=response.getReturnValue();
                alert('SUCCESS');
                
                
            }else if(state==='INCOMPLETE'){
                
            }
        });
        $A.enqueueAction(action);
    }
})


Thanks,
Maharajan.C
This was selected as the best answer
SFDC12SFDC12
Hi am able to insert the contact using aura  ,But unable to see the contact related to particular account.But when i click on contacts tab am able to see the conatcts.But not related to particular account.

Thanks in Advance.