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
PRADEEP YADAV 5PRADEEP YADAV 5 

insert based trigger

trigger InsertBefore on Account (after insert) 
{
    List<Contact>contdetail = new List<Contact>();
  
  Map<Id, Account>mapaccount = new Map<Id, Account>([Select Id, name, phone, 
                              (Select Id,FirstName, LastName, phone From Contacts
                              )From Account Where Id In : Trigger.New]);
                              
  for(Account a : Trigger.New)
  {
         if(mapaccount.get(a.Id).contacts.size() == 0)
         {
             contdetail.add(new Contact(FirstName =a.Name, LastName =a.Name, Phone =a.phone, AccountId =a.Id));
         }
  }
}
Best Answer chosen by PRADEEP YADAV 5
Agustin BAgustin B
Please mark the answer as the correct one if it solved your question.
It may help others.
 

All Answers

PRADEEP YADAV 5PRADEEP YADAV 5
How to Perform through list not map then what way doing
Agustin BAgustin B
Trigger InsertBefore on Account (after insert) 
{
    List<Contact>contdetail = new List<Contact>();
   
  for(Account a : [Select Id, name, phone, 
                              (Select Id,FirstName, LastName, phone From Contacts
                              )From Account Where Id In : Trigger.New])
  {
         if(a.contacts.size() == 0)
         {
             contdetail.add(new Contact(FirstName =a.Name, LastName =a.Name, Phone =a.phone, AccountId =a.Id));
         }
  }
}

 
PRADEEP YADAV 5PRADEEP YADAV 5
Thanks Agustin B
Agustin BAgustin B
Please mark the answer as the correct one if it solved your question.
It may help others.
 
This was selected as the best answer