You need to sign in to do that
Don't have an account?
Athira Venugopal
Unable to create a contact using Lightning aura components
ContactComponent.cmp
<aura:component controller ="ContactController " implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name = "conList" type = "Contact"></aura:attribute>
<lightning:input type ="text" label = "First Name" value="{!v.conList.FirstName}" ></lightning:input>
<lightning:input type ="text" label = "Last Name" value="{!v.conList.LastName}"></lightning:input>
<lightning:input type ="tel" label = "Phone" value="{!v.conList.Phone}"></lightning:input>
<lightning:input type ="email" label = "Email" value="{!v.conList.Email}" ></lightning:input>
<lightning:button variant="brand" label="Create" onclick="{!c.handleClick}" class="slds-m-left_x-small"></lightning:button>
</aura:component>
ContactComponentController.js
({
handleClick : function(component, event, helper) {
console.log("HIIIIIII" + conlist);
var action = component.get("c.createContact");
action.setParams({
ac : component.get("v.conList")
});
action.setCallback(this,function(response){
if(response.getState() == 'SUCCESS') {
alert("Updated successfully");
}
});
$A.enqueueAction(action);
}
})
ContactController.apxc
public class ContactController {
@auraEnabled
public static void createContact( Contact c) {
insert c;
}
}
Is there any mistake in my code
<aura:attribute name = "conList" type = "Contact"></aura:attribute>
<lightning:input type ="text" label = "First Name" value="{!v.conList.FirstName}" ></lightning:input>
<lightning:input type ="text" label = "Last Name" value="{!v.conList.LastName}"></lightning:input>
<lightning:input type ="tel" label = "Phone" value="{!v.conList.Phone}"></lightning:input>
<lightning:input type ="email" label = "Email" value="{!v.conList.Email}" ></lightning:input>
<lightning:button variant="brand" label="Create" onclick="{!c.handleClick}" class="slds-m-left_x-small"></lightning:button>
</aura:component>
ContactComponentController.js
({
handleClick : function(component, event, helper) {
console.log("HIIIIIII" + conlist);
var action = component.get("c.createContact");
action.setParams({
ac : component.get("v.conList")
});
action.setCallback(this,function(response){
if(response.getState() == 'SUCCESS') {
alert("Updated successfully");
}
});
$A.enqueueAction(action);
}
})
ContactController.apxc
public class ContactController {
@auraEnabled
public static void createContact( Contact c) {
insert c;
}
}
Is there any mistake in my code
I was using the below code for create a custom record and I was able to create a new record can you try this code once:
Component:
Controller:
Helper:
Controller:
Let me know in case if this comes in handy and in case if this helps can you please mark this as the best answer so that it can be used by others in the future.
Regards,
Anutej
All Answers
I was using the below code for create a custom record and I was able to create a new record can you try this code once:
Component:
Controller:
Helper:
Controller:
Let me know in case if this comes in handy and in case if this helps can you please mark this as the best answer so that it can be used by others in the future.
Regards,
Anutej
I would suggest to use Lightning:RecordEditForm Insteead using this much big lines of code.
Something Like this:
You don't need to use apex to insert contact record, all that is taken care by Salesforce.
Lightning:RecordEditForm (https://developer.salesforce.com/docs/component-library/bundle/lightning:recordEditForm/documentation) - Salesforce Documentation
Trailhead Module (https://trailhead.salesforce.com/content/learn/projects/workshop-lightning-programmatic/handle-data-with-recordeditform) - Trailhead module on Lightning:RecordEditForm
Changes I made are:
>> in the controller class made it with sharing.
>>and while setting parameters you are doing it wrongly instead of using this: action.setParams({
ac : component.get("v.conList")
});
//try changing ac to c as the prarmeter in apex class is c and not ac
I used action.setParams({"c": component.get("v.conList")});
>> to handle various response states checked it with conditional statements and showed error if there are any.
I hope this helps and in case if this comes handy can you please choose this as best answer so that it can be useful for others in the future.
Let me know in case if there are any questions.
Hi
Just update your parameter name in the apex controller as shown below and try to execute the code.
public static void createContact( Contact ac)
If your query is resolved please mark this as a best answer.