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
Sriram Varma 1Sriram Varma 1 

Triggers apex

Create a custom field called number in account in that field if we enter 5 then automatically 5 records should create in contact using apex trigger.
SwethaSwetha (Salesforce Developers) 
HI Sairam,
What is the business use case you are trying to achieve?What should be the 5 contact details if the number entered in account is 5.

The code in https://salesforce.stackexchange.com/questions/27241/write-a-trigger-that-automatically-create-a-contact-when-account-is-created is about a trigger that automatically creates a contact when the account is created and should help you get started.

If this information helps, please mark the answer as best.Thank you
CharuDuttCharuDutt
Hii Sriram Varma 1 
Try The Following Code
trigger triggercontact on Account (before insert) {
 		Integer numberOfContacts=0;
   
   for(account a : trigger.new){
      numberOfContacts = (Integer)a.NumberField__c;
   }
   List<contact> listOfContactToInsert = new List<contact>();
   
   for(integer i = 1; i<=numberOfContacts;i++){
      contact c =  new contact();
      c.LastName = 'Test'+i;
      listOfContactToInsert.add(c);
   }
   insert listOfContactToInsert;
}
If It Helps Please Mark It As Best Answer.
Thank You!
 
Malika Pathak 9Malika Pathak 9

Hi Sriram Varma,

Here is the solution of your question

trigger YAtrigger on Account (After insert) {
    
    if(trigger.isAfter && trigger.isInsert){
        YAQuest.method1(trigger.new);
    }

}

public class YAQuest {
    public static void method1(List<Account> accountList){
        
        List<contact> con = new List <contact>();
        
        for(account ac:accountList){
            for(decimal i=ac.Number__c;i>0;i--){
                contact c= new contact();
                c.lastname='Aspire'+i;
                c.AccountId=ac.id;
                con.add(c);
            }
        }
        insert con;
        
    }

}

If this information helps, please mark the answer as best,so that other can take refernce from here.
sachinarorasfsachinarorasf
Hi Sriram,
You can use the below piece of code. I have also tested it in my org.
Trigger:
trigger CreateContactTrigger on Account (After insert) {  
    if(trigger.isAfter && trigger.isInsert){
        CreateContactController.createContact(trigger.new);
    }
}
Apex Class:
public class CreateContactController{
    public static void createContact(List<Account> accountList){        
        List<contact> contactList = new List<contact>();
        if(accountList != null && accountList.size() > 0){        
          for(account ac : accountList){
            if( ac.Number__c != null){
               for(Integer i = 1 ;i <= ac.Number__c; i++){
                 contact con = new contact();
                 con.lastname= ac.Name+' '+i;
                 con.AccountId=ac.id;
                 contactList.add(con);
              }
            }
          }
        }
        if(contactList != null && contactList.size() > 0)
           insert contactList;
    }
}

I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com