You need to sign in to do that
Don't have an account?
Aakanksha Singh
Query in Creating rercord
Hello Everyone,
my code is:
component1:
<aura:component>
<aura:registerEvent name="createRecord" type="c:Event"/>
<ui:button action={!c.createRecord}>Create Record</ui:button>
<c:form/>
</aura:component>
controller1:
createRecord:function(cmp,event){
var evt = cmp.getEvent("createRecord");
evt.fire();
}
form-
<aura:component controller="saveRecord">
<aura:attribute name="account" type="Account[]"/>
<aura:handler name="createRecord" action="{!c.createRecord}" event="c:Event"/>
<aura:attribute name="newAccount" type="Account"
default="{ 'sobjectType': 'Account',
'Name': '',
'AccountNumber': '',
'AccountSource':'',
'Active__c': '',
}"/>
<ui:inputText aura:id="Name" label="Account Name" value="{!v.newAccount.Name}" required="true"/>
<ui:inputText label="Account Name" value="{!v.newAccount.accountNumber}"/>
<ui:inputText label="Account Name" value="{!v.newAccount.accountSource}" />
<ui:inputText label="Account Name" value="{!v.newAccount.Active__c}"/>
</aura:component>
form controller-
createRecord : function(component, event, helper) {
var accField = component.find("Name");
var acc = accField.get("v.value");
if (acc==''){
accField.set("v.errors", [{message:"Enter account name."}]);
}
else {
accField.set("v.errors", null);
var newAccount = component.get("v.newAccount");
helper.createRecord(component, newAccount);
}
}
form helper-
createrecord:function(cmp,event,callback){
var action = component.get("c.saveAccount");
action.setParams({
"account": account
});
if (callback) {
action.setCallback(this, callback);
}
$A.enqueueAction(action);
}
I'm not able to create any record on click of the button, how will it be possible?
Thanks in advance
my code is:
component1:
<aura:component>
<aura:registerEvent name="createRecord" type="c:Event"/>
<ui:button action={!c.createRecord}>Create Record</ui:button>
<c:form/>
</aura:component>
controller1:
createRecord:function(cmp,event){
var evt = cmp.getEvent("createRecord");
evt.fire();
}
form-
<aura:component controller="saveRecord">
<aura:attribute name="account" type="Account[]"/>
<aura:handler name="createRecord" action="{!c.createRecord}" event="c:Event"/>
<aura:attribute name="newAccount" type="Account"
default="{ 'sobjectType': 'Account',
'Name': '',
'AccountNumber': '',
'AccountSource':'',
'Active__c': '',
}"/>
<ui:inputText aura:id="Name" label="Account Name" value="{!v.newAccount.Name}" required="true"/>
<ui:inputText label="Account Name" value="{!v.newAccount.accountNumber}"/>
<ui:inputText label="Account Name" value="{!v.newAccount.accountSource}" />
<ui:inputText label="Account Name" value="{!v.newAccount.Active__c}"/>
</aura:component>
form controller-
createRecord : function(component, event, helper) {
var accField = component.find("Name");
var acc = accField.get("v.value");
if (acc==''){
accField.set("v.errors", [{message:"Enter account name."}]);
}
else {
accField.set("v.errors", null);
var newAccount = component.get("v.newAccount");
helper.createRecord(component, newAccount);
}
}
form helper-
createrecord:function(cmp,event,callback){
var action = component.get("c.saveAccount");
action.setParams({
"account": account
});
if (callback) {
action.setCallback(this, callback);
}
$A.enqueueAction(action);
}
I'm not able to create any record on click of the button, how will it be possible?
Thanks in advance
Please refer below link
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_records_save.htm
If it is helpful please mark is as best Answer .
Thanks ,
karthik
can you share your apex class controller code ? controller="saveRecord"
Thanks
controller-
public with sharing class saveRecord {
@AuraEnabled
public static void saveAccount(account Acc){
insert Acc;
}
}
thanks
apex class looks fine but i find many mistakes in your lightning component and js controller so i rectification them and reply back or you can mail me on sfdcjaipur@gmail.com :)
Thank you.
try and understand the functionality of my sample component of new account insert ---
aura:event are call by only nested(child to parent) component in your code you call it by parent component
click the button on main component and handle event in child component(form) so..
try this functionality-:
event-: Event.evt apex:class-: MysaveRecord.apsx
form component-:
form
form js btnComponent.cmp
Thanksjscontroller.js
please let me inform if it works and mark it solve :)
If you don't mind, can you explain me why is it important to store response in this situation, I really need to understand.
Thanx
<aura:component>
<c:form/>
<ui:button press="{! c.addNewAccount}" label="create record"></ui:button>
</aura:component>
let me explain all things one by one
#1. first of all we have not need in this case to use var storeResponse = response.getReturnValue(); because our apex method not return any value so you can skip this step :)
#2 you can use helper method by this way
js.controller
js helper
3# and you can achieve this functionality by some little changes in your event and your js controller
* The only thing that changes in the Lightning Event is the level=”COMPONENT” changes to level=”APPLICATION.” i.e Nothing needs to change with the way the component uses the aura:register to declare that it fires the event. :)
Change in event handler
<aura:handler event="c:Event" action="{!c.addAccoutOnButtonPress}" /> // remove the name
cahnge in your js controller or helper
addNewAccount : function(component, event, helper) {
var myevt = $A.getEvt("c:Event"); use for APPLICATION type event (In your case)
//var myevt = component.getEvent("cmpEvent"); // use for COMPONENT Type eventmyevt.fire();
alert('event fire successfully');
},
for more understanding Application level event go to bleow link
https://developer.salesforce.com/blogs/developer-relations/2015/03/lightning-component-framework-custom-events.html
Thanks :)
Mark it solve if it helps you :)