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
Neerudu PriyankaNeerudu Priyanka 

HI all i have a dought at lightning component contact creation PFB CODE

<aura:component controller="ContactControllerlightning">
	<aura:attribute name="contactObj" type="Contact" default="{'sobjectType':'Contact',
                                                              'FirstName':'' ,
                                                              'LastName':'' ,
                                                              'phone':''}"/>
    <aura:attribute name="contactId" type="String"/>
 
    <lightning:input value="{!v.contactObj.FirstName}" label="First Name" placeholder="Enter FirstName"/>
    <lightning:input vlaue="{!v.contactObj.LastName}" label="Last Name" placeholder="Enter LastName"/>
    <lightning:input value="{!v.contactObj.phone}" label="Phone" placeholder="Enter Phone"/>
        <lightning:button variant="brand" label="Save" title="Save" onclick="{!c.dosave}"/>

  

    
    
    
</aura:component>
 
({
	dosave : function(component, event, helper) {
        var action = component.get("c.createContact");
        
        action.setParams({'contObj':component.get('v.contactObj')});
        
        action.setCallback(this,function(data){
         component.set('v.contactId',data.getReturnValue())
        });
		$A.enqueueAction(action);
	}
})

 
Best Answer chosen by Neerudu Priyanka
Khan AnasKhan Anas (Salesforce Developers) 
Hi Neerudu,

Greetings to you!

Please try the below code to insert contacts using lightning component, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Application:
<aura:application extends="force:slds">
    <c:CreateCon />
</aura:application>

Component:
<aura:component controller="CreateConC"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="RegForm" type="Contact" default="{'sobjectType' : 'Contact'}"/>
    
    <div class = "slds-size--3-of-8">
        <lightning:input label="Enter First Name" name="firstName" value="{!v.RegForm.FirstName}"/>
        <br/>
        <lightning:input label="Enter Last Name" name="lastName" value="{!v.RegForm.LastName}"/>
        <br/>
        <lightning:input label="Enter Phone" name="phone" value="{!v.RegForm.Phone}"/>
        <br/>
        <lightning:button label="Submit" onclick="{!c.doSubmit}"/>
    </div>
</aura:component>

Controller:
({
    doSubmit : function(component, event, helper) {
        var regForm = component.get("v.RegForm");
        var action = component.get("c.saveDetails");
        action.setParams({regForm1  : regForm});
        action.setCallback(this, function(response) {
            var state = response.getState();          
            if (state === "SUCCESS") {
                var res = response.getReturnValue();
                alert('Successfully Saved');
                component.set('v.RegForm','');
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                } 
                else {
                    console.log(response.getReturnValue());
                }
            }
        });
        $A.enqueueAction(action);
    },
})

CSS:
.THIS {

}

.THIS.slds-size--3-of-8 {
    
    margin-left: 430px;
}

.THIS label.slds-form-element__label{
    
    font-size: 1.00rem;
    color: blue;
}

Apex:
public class CreateConC {

    @AuraEnabled
    public static void saveDetails(Contact regForm1){
    	// DML operation to save RegForm Details   
    	INSERT regForm1;
    }
}

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 Neerudu,

Greetings to you!

Please try the below code to insert contacts using lightning component, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Application:
<aura:application extends="force:slds">
    <c:CreateCon />
</aura:application>

Component:
<aura:component controller="CreateConC"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="RegForm" type="Contact" default="{'sobjectType' : 'Contact'}"/>
    
    <div class = "slds-size--3-of-8">
        <lightning:input label="Enter First Name" name="firstName" value="{!v.RegForm.FirstName}"/>
        <br/>
        <lightning:input label="Enter Last Name" name="lastName" value="{!v.RegForm.LastName}"/>
        <br/>
        <lightning:input label="Enter Phone" name="phone" value="{!v.RegForm.Phone}"/>
        <br/>
        <lightning:button label="Submit" onclick="{!c.doSubmit}"/>
    </div>
</aura:component>

Controller:
({
    doSubmit : function(component, event, helper) {
        var regForm = component.get("v.RegForm");
        var action = component.get("c.saveDetails");
        action.setParams({regForm1  : regForm});
        action.setCallback(this, function(response) {
            var state = response.getState();          
            if (state === "SUCCESS") {
                var res = response.getReturnValue();
                alert('Successfully Saved');
                component.set('v.RegForm','');
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                } 
                else {
                    console.log(response.getReturnValue());
                }
            }
        });
        $A.enqueueAction(action);
    },
})

CSS:
.THIS {

}

.THIS.slds-size--3-of-8 {
    
    margin-left: 430px;
}

.THIS label.slds-form-element__label{
    
    font-size: 1.00rem;
    color: blue;
}

Apex:
public class CreateConC {

    @AuraEnabled
    public static void saveDetails(Contact regForm1){
    	// DML operation to save RegForm Details   
    	INSERT regForm1;
    }
}

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
Neerudu PriyankaNeerudu Priyanka
ITS throwing me some error as This page has an error. You might just need to refresh it. Unable to find action 'saveDetails' on the controller of c:ContactFINAL Failing descriptor: {c:ContactFINAL}
Khan AnasKhan Anas (Salesforce Developers) 
Please share your complete code.
Neerudu PriyankaNeerudu Priyanka
<aura:application >
    <c:ContactFINAL/>
</aura:application>
 
<aura:component controller="contactfinalapexclass"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="RegForm" type="Contact" default="{'sobjectType' : 'Contact'}"/>
    
    <div class = "slds-size--3-of-8">
        <lightning:input label="Enter First Name" name="firstName" value="{!v.RegForm.FirstName}"/>
        <br/>
        <lightning:input label="Enter Last Name" name="lastName" value="{!v.RegForm.LastName}"/>
        <br/>
        <lightning:input label="Enter Phone" name="phone" value="{!v.RegForm.Phone}"/>
        <br/>
        <lightning:button label="Submit" onclick="{!c.doSubmit}"/>
    </div>
</aura:component>
 
({
    doSubmit : function(component, event, helper) {
        var regForm = component.get("v.RegForm");
        var action = component.get("c.saveDetails");
        action.setParams({regForm1  : regForm});
        action.setCallback(this, function(response) {
            var state = response.getState();          
            if (state === "SUCCESS") {
                var res = response.getReturnValue();
                alert('Successfully Saved');
                component.set('v.RegForm','');
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                } 
                else {
                    console.log(response.getReturnValue());
                }
            }
        });
        $A.enqueueAction(action);
    },
})
 
public class contactfinalapexclass {
    @AuraEnabled
    public static void saveDetails(Contact regForm1){
    	// DML operation to save RegForm Details   
    	INSERT regForm1;
    }
}

 
Khan AnasKhan Anas (Salesforce Developers) 
The code is good. There is no problem with it. 

Follow below steps:

1. Create Apex Class

2. Create Lightning Component.
Please check that you are creating new component from Developer Console -> File -> New -> Lightning Component

3. Create Component's Controller.
Click on Controller from Component Bundle.

4. Create Lightning Application.
Developer Console -> File -> New -> Lightning Application

If the problem still persists, please share screenshots of all the code and the error.

I hope it helps you.

Kindly mark this as solved if the information was helpful. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Neerudu PriyankaNeerudu Priyanka
HI sorry for  its working fine but can u plz let mw now the error in the code i posted above
Khan AnasKhan Anas (Salesforce Developers) 
There are few things which you need to correct in the component. There is spelling mistake in <lightning:input vlaue="{!v.contactObj.LastName}" label="Last Name" placeholder="Enter LastName"/>
Instead of value you have typed vlaue. 

Also, as lightning is based on aura framework which is a type of JavaScript and JavaScript is case sensitive so lightning is case sensitive. You have phone instead of Phone in a component.

I hope it helps you.

Kindly mark this as solved if the information was helpful. It will help to keep this community clean.

Regards,
Khan Anas
Ronaldo Costa 8Ronaldo Costa 8
Can you please post a test class for:
public class contactfinalapexclass {
    @AuraEnabled
    public static void saveDetails(Contact regForm1){
    	// DML operation to save RegForm Details   
    	INSERT regForm1;
    }
}

My test class is currently at 66% only, please help.
 
@isTest
private class SaveAndNewC_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Lead lead_Obj = new Lead(LastName = 'LastName836', FirstName = 'First211', Company = 'Company565', Phone = '54343-13889', Status = 'Unqualified', IsConverted = false, IsUnreadByOwner = false);
    Insert lead_Obj;
    test.stopTest();
  }
  static testMethod void test_saveDetails_UseCase1(){
    List<Lead> lead_Obj  =  [SELECT LastName,FirstName,Company,Phone,Status,IsConverted,IsUnreadByOwner from Lead];
    System.assertEquals(true,lead_Obj.size()>0);
    SaveAndNewC obj01 = new SaveAndNewC();
    SaveAndNewC.saveDetails(lead_Obj[0]); 
      //lead_Obj[0]
  }
}

Thanks,
Ron​​​​​​​
Ronaldo Costa 8Ronaldo Costa 8
SaveAndNewC_Testtest_saveDetails_UseCase1System.DmlException: Insert failed. First exception on row 0 with id 00Qf400000IsyWLEAZ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] 

Stack Trace: Class.SaveAndNewC.saveDetails: line 6, column 1 Class.SaveAndNewC_Test.test_saveDetails_UseCase1: line 14, column 1