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
brahmanaidubrahmanaidu 

Creating a Record on clicking of a button in salesforce Lightning

public class CreateRecord
{
public static void createAccount(){
    List<Account> accList=Trigger.new;
    List<Account> accs=new List<Account>();
    for(Account a:accList){
        Account a1=new Account();
        a.Name='mbnchowdary';
        a.Site='mandalapu';
        a.Email__c='mbnchowdarysfdc@gmail.com';
        accs.add(a);
        System.debug(accs);
        
    }
    
    insert accs;

}    
    
}


<aura:component  controller="RecordInsertion">
    <ui:button label="click"  aura:id="button" press="{!c.doPerform}"/>
</aura:component>


({
    
    
               doPerform:function(component,event,helper){
               console.log('method is called');
                   var action=component.get('c.createAccount');
                       action.setParams({

            });
        $A.enqueueAction(action)
                   
                   
                   
        
        
    }
    
    
})
Best Answer chosen by brahmanaidu
Raj VakatiRaj Vakati
Here is the code
 
public class RecordInsertion
{
    @AuraEnabled
    public static void createAccount(){
        List<Account> accs=new List<Account>();
        for(Integer i = 0 ;i < 10 ;i++){
            Account a1=new Account();
            a1.Name='mbnchowdary';
            a1.Site='mandalapu';
           a.Email__c='mbnchowdarysfdc@gmail.com';
            accs.add(a1);
        }
        
        insert accs;
        
    }    
    
}
 
<aura:component  controller="RecordInsertion">
    <ui:button label="click"  aura:id="button" press="{!c.doPerform}"/>
</aura:component>
 
({
    doPerform:function(component,event,helper){
        var action=component.get('c.createAccount');
        action.setCallback(this, function(response) {
            var state = response.getState();
            
            if (state === "SUCCESS") {
                alert(state);
            }
        });
        $A.enqueueAction(action)
        
    }
    
    
})

 

All Answers

Raj VakatiRaj Vakati
Here is the code
 
public class RecordInsertion
{
    @AuraEnabled
    public static void createAccount(){
        List<Account> accs=new List<Account>();
        for(Integer i = 0 ;i < 10 ;i++){
            Account a1=new Account();
            a1.Name='mbnchowdary';
            a1.Site='mandalapu';
           a.Email__c='mbnchowdarysfdc@gmail.com';
            accs.add(a1);
        }
        
        insert accs;
        
    }    
    
}
 
<aura:component  controller="RecordInsertion">
    <ui:button label="click"  aura:id="button" press="{!c.doPerform}"/>
</aura:component>
 
({
    doPerform:function(component,event,helper){
        var action=component.get('c.createAccount');
        action.setCallback(this, function(response) {
            var state = response.getState();
            
            if (state === "SUCCESS") {
                alert(state);
            }
        });
        $A.enqueueAction(action)
        
    }
    
    
})

 
This was selected as the best answer
Raj VakatiRaj Vakati
Correct Class is here \
 
public class RecordInsertion
{
    @AuraEnabled
    public static void createAccount(){
        List<Account> accs=new List<Account>();
        for(Integer i = 0 ;i < 10 ;i++){
            Account a1=new Account();
            a1.Name='mbnchowdary';
            a1.Site='mandalapu';
           a1.Email__c='mbnchowdarysfdc@gmail.com';
            accs.add(a1);
        }
        
        insert accs;
        
    }    
    
}

 
GarryPGarryP
instead writing custom code user, force:createrecord event which will do thing for you automatically
brahmanaidubrahmanaidu
Your code is not working
Raj VakatiRaj Vakati
what is the issue? Its working for me 
brahmanaidubrahmanaidu
User-added image
brahmanaidubrahmanaidu
Perfect.Thanks for spending your valuable time .
brahmanaidubrahmanaidu
Can you Please help me in delete functionality.I am not getting any error but unable to achieve the functionality public class ssss { @AuraEnabled public static void DeleteAccounts(){ List accs=new List(); List delaccList=new List(); accs=[select Id,Name,Email__c,Count__c,Site from Account LIMIT 100]; for(Account a:accs){ delaccList.add(a); System.debug(delaccList); } if(delaccList.size()>0 && !delaccList.isEmpty()){ delete delaccList; } } } ({ doPerform:function(component,event,helper){ var action=component.get('c.DeleteAccounts'); action.setCallback(this, function(response) { var state = response.getState(); if (state === "SUCCESS") { console.log('method is called'); } }); $A.enqueueAction(action) } })
Raj VakatiRaj Vakati
@AuraEnabled public static void DeleteAccounts(){
List<account> accs=[select Id,Name,Email__c,Count__c,Site from Account LIMIT 100]; 
 delete accs ;

 
brahmanaidubrahmanaidu
It is not working
brahmanaidubrahmanaidu
It's working fine 
brahmanaidubrahmanaidu
It's working fine i didn't check the record count that's the error