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
TuckoTucko 

Trigger for creating Account records

Hello, 

I am new to this platform. I started to learn about triggers and I stuck with this code, I can't get what is not right about it. Every help will be great. Thank you! 
 

trigger AccountTrigger on Account (after update,after insert, before update, before insert) {
    List<Account>listAccounts = new List<Account>();
    
    for(Account a: Trigger.new){
        if(a.CheckPayment__c == True){
           
            Account account1 = new Account();
            account1.Name = 'Akaunt1';
            account1.First_Name__c = 'Acount1';
            account1.Second_Name__c = 'Surname1';            
            account1.Status__c = 'Idle';
            
            Account account2 = new Account();
            account2.Name = 'Akaunt2';
            account2.First_Name__c = 'Acount2';
            account2.Second_Name__c = 'Surname2';
            account2.Status__c = 'Idle';
            
            Account account3 = new Account();
            account3.Name = 'Akaunt3';
            account3.First_Name__c = 'Acount3';
            account3.Second_Name__c = 'Surname3';
            account3.Status__c = 'Idle';
            
            Account account4 = new Account();
            account4.Name = 'Akaunt4';
            account4.First_Name__c = 'Acount4';
            account4.Second_Name__c = 'Surname4';
            account4.Status__c = 'Idle';
            
            Account account5 = new Account();
            account5.Name = 'Akaunt5';
            account5.First_Name__c = 'Acount5';
            account5.Second_Name__c = 'Surname5';
            account5.Status__c = 'Idle';
            
     	}
    }
    insert listAccounts;
}
Best Answer chosen by Tucko
ManojjenaManojjena
Hi  Bozidar Andev 7,

Try with below code .
 
trigger AccountTrigger on Account (after update,after insert, before update, before insert) {
    List<Account>listAccounts = new List<Account>();
    
    for(Account a: Trigger.new){
        if(a.CheckPayment__c == True){
           
            Account account1 = new Account();
            account1.Name = 'Akaunt1';
            account1.First_Name__c = 'Acount1';
            account1.Second_Name__c = 'Surname1';            
            account1.Status__c = 'Idle';
			listAccounts.add(account1);
            
            Account account2 = new Account();
            account2.Name = 'Akaunt2';
            account2.First_Name__c = 'Acount2';
            account2.Second_Name__c = 'Surname2';
            account2.Status__c = 'Idle';
            listAccounts.add(account2);
            Account account3 = new Account();
            account3.Name = 'Akaunt3';
            account3.First_Name__c = 'Acount3';
            account3.Second_Name__c = 'Surname3';
            account3.Status__c = 'Idle';
            listAccounts.add(account3);
            Account account4 = new Account();
            account4.Name = 'Akaunt4';
            account4.First_Name__c = 'Acount4';
            account4.Second_Name__c = 'Surname4';
            account4.Status__c = 'Idle';
            listAccounts.add(account4);
            Account account5 = new Account();
            account5.Name = 'Akaunt5';
            account5.First_Name__c = 'Acount5';
            account5.Second_Name__c = 'Surname5';
            account5.Status__c = 'Idle';
            listAccounts.add(account5);
     	}
    }
    insert listAccounts;
}

Thanks 
Manoj

All Answers

ManojjenaManojjena
Hi  Bozidar Andev 7,

Try with below code .
 
trigger AccountTrigger on Account (after update,after insert, before update, before insert) {
    List<Account>listAccounts = new List<Account>();
    
    for(Account a: Trigger.new){
        if(a.CheckPayment__c == True){
           
            Account account1 = new Account();
            account1.Name = 'Akaunt1';
            account1.First_Name__c = 'Acount1';
            account1.Second_Name__c = 'Surname1';            
            account1.Status__c = 'Idle';
			listAccounts.add(account1);
            
            Account account2 = new Account();
            account2.Name = 'Akaunt2';
            account2.First_Name__c = 'Acount2';
            account2.Second_Name__c = 'Surname2';
            account2.Status__c = 'Idle';
            listAccounts.add(account2);
            Account account3 = new Account();
            account3.Name = 'Akaunt3';
            account3.First_Name__c = 'Acount3';
            account3.Second_Name__c = 'Surname3';
            account3.Status__c = 'Idle';
            listAccounts.add(account3);
            Account account4 = new Account();
            account4.Name = 'Akaunt4';
            account4.First_Name__c = 'Acount4';
            account4.Second_Name__c = 'Surname4';
            account4.Status__c = 'Idle';
            listAccounts.add(account4);
            Account account5 = new Account();
            account5.Name = 'Akaunt5';
            account5.First_Name__c = 'Acount5';
            account5.Second_Name__c = 'Surname5';
            account5.Status__c = 'Idle';
            listAccounts.add(account5);
     	}
    }
    insert listAccounts;
}

Thanks 
Manoj
This was selected as the best answer
ManojjenaManojjena
Hi Bozidar Andev 7,

Above trigger will work ,I don't know your requirment but as you have added before insert and after insert ,So it will create 10 Accouns on insert of one account and 10 Accounts on update of one Account if your CheckPayment__c  is true .


 
TuckoTucko
Thank you very much Manoj, so I was missing that crucial line of code after every account. Yes it creates 10 accounts, I only added all of the conditions just to check if it works as I need only after insert and update, will fix that issue, again thank you very much. :)
Suraj TripathiSuraj Tripathi
I want to create multiple account using loop. How can i do that?