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
nagarjuna gurajanagarjuna guraja 

Need Trigger for Execution

Hello,
 Write a trigger such that we have two objects Account and Contact. Field named numberofcontacts on Account. If we insert number of contacts as 3, then three contacts should be created.
Best Regards 
Vineela ProddaturuVineela Proddaturu
Hi Nagarjuna,

Can you please try this one.


trigger numberofcontactsonAcc on Account (after insert) {
    list<Contact> lstcon = new list<Contact>();
    for(Account acc : trigger.new){
        if(acc.Create_N_Contacts__c != null){
            for(Integer i = 0; i < acc.Create_N_Contacts__c; i++){
                contact con = new contact();
                con.LastName = acc.Name + i;
                con.AccountId = acc.Id;
                lstcon.add(con);
            }
        }
    }
    if(!lstcon.isEmpty()){
        insert lstcon;
        
    }

}    
    If it helps,Pls, mark it as best answer.
Thank you.

 
SubratSubrat (Salesforce Developers) 
Hello ,

You can also refer this discussion -> https://developer.salesforce.com/forums/?id=906F00000008p4WIAQ

Thank you .