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
Aakanksha SinghAakanksha 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
Trail Head 33Trail Head 33
Hi Akanksha Sing,

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
sfdcMonkey.comsfdcMonkey.com
hi Aakanksha Singh
can you share your apex class controller code ? controller="saveRecord"
Thanks
Aakanksha SinghAakanksha Singh
Hi,
controller-
public with sharing class saveRecord {

   @AuraEnabled
    public static void saveAccount(account Acc){
       insert Acc;
  }
  }
thanks
sfdcMonkey.comsfdcMonkey.com
hi 
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 :)
Aakanksha SinghAakanksha Singh
Kindly reply back,
Thank you.
sfdcMonkey.comsfdcMonkey.com
hi Aakanksha Singh

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
<aura:event type="COMPONENT" description=""/>
apex:class-: MysaveRecord.apsx
public class MysaveRecord {

   @AuraEnabled
    public static void saveAccount(account Acc){
       insert Acc;
     }
  }


form component-:
form
<aura:component controller="MysaveRecord">
   <aura:handler name="cmpEvent" event="c:Event" action="{!c.addAccoutOnButtonPress}" />
    <aura:attribute name="newAccount" type="Account"
                   default="{ 'sobjectType': 'Account',
                               'Name': '',        
                             }"/>
    <c:btnComponent/>
    <ui:inputText aura:id="Name" label="Account Name"  value="{!v.newAccount.Name}" />
    
</aura:component>

form js 
({
	addAccoutOnButtonPress : function(component, event, helper) {
        alert('Handle fire successfully');
      var action = component.get("c.saveAccount");
            action.setParams({ Acc : component.get("v.newAccount") });

        action.setCallback(this, function(response) {
        var state = response.getState();
        if (component.isValid() && state === "SUCCESS") {
        var storeResponse =   response.getReturnValue();
        alert('save record successfully');     
        console.log('@-->' + storeResponse);
	        }
       });
       
     $A.enqueueAction(action);
	}, 
    
})
btnComponent.cmp
<aura:component>
    <aura:registerEvent name="cmpEvent" type="c:Event" />
    <ui:button press="{! c.addNewAccount}" label="create record"></ui:button>
</aura:component>


jscontroller.js

({
	addNewAccount : function(component, event, helper) {
         var myevt = component.getEvent("cmpEvent");
        myevt.fire();
	},
})
Thanks
please let me inform if it works and mark it solve :)





 

Aakanksha SinghAakanksha Singh
Hi Soni Piyush,
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
 
Aakanksha SinghAakanksha Singh
And also i've to execute the code through helper.. how can i do that??
Aakanksha SinghAakanksha Singh
@Soni Piyush, thanx for showing this way of creating records, it is useful, bt what i have to do is jst the opposite of this one, I have form component included in the component that has button i.e.,
<aura:component>
    <c:form/>
    <ui:button press="{! c.addNewAccount}" label="create record"></ui:button>
</aura:component>
sfdcMonkey.comsfdcMonkey.com
hi Aakanksha Singh
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
addAccoutOnButtonPress : function(component, event, helper) {
        alert('Handle fire successfully and call the helper ');
      helper.saveRecordHelper(component,event);
     
	},

js helper 
saveRecordHelper : function(component,event) {
          var action = component.get("c.saveAccount");
            action.setParams({ Acc : component.get("v.newAccount") });
           action.setCallback(this, function(response) {
           var state = response.getState();
        if (component.isValid() && state === "SUCCESS") {  
        alert('save record successfully');        
	        }
       });
     $A.enqueueAction(action);
	},

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
<aura:event type="APPLICATION" description=""/>
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 event 
        myevt.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 :)