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
Sree SalesforceSree Salesforce 

In my requirement when i create parent record i want to create a child record .i have a lookup relationship


i unable to crate a reord .i got an error like this  Initial term of field expression must be a concrete SObject: LIST<Id> at line 13 column 19

trigger createcontacts on Account (after insert) {
   
     list<contact> con=new list<contact>();
     
    list<id> l1=new list<id>();
   for(account acc:trigger.new)
  {
   l1.add(acc.id);  
 }
   if(l1.size()>0 && l1!=null)
    {
     contact c1=new contact();
     c1.accountid=l1.id;
     c1.firstname=l1.name;
     con.add(c1);
     }
     insert con;
     }
Best Answer chosen by Sree Salesforce
Arunkumar RArunkumar R
Hi Sri,
Here is the code for your requirement,
trigger CreateContacts on Account (after insert)
{
	List<Contact> conList = new List<Contact>();

	for(Account acc:Trigger.newMap.Values())
	{
	Contact cnt =new Contact();
	cnt.accountid=acc.id;
	cnt.LastName=acc.name;
	conList.add(cnt);
	}
	
	if(conList.size()>0)
	{
		insert conList;
	}
}

All Answers

ch ranjeethch ranjeeth
 if(l1.size()>0 && l1!=null)
    {
     contact c1=new contact();
     c1.accountid=l1[0].id;
     c1.firstname=l1[0].name;
     con.add(c1);
     }
     insert con;
     }
Arunkumar RArunkumar R
Hi Sri,
Here is the code for your requirement,
trigger CreateContacts on Account (after insert)
{
	List<Contact> conList = new List<Contact>();

	for(Account acc:Trigger.newMap.Values())
	{
	Contact cnt =new Contact();
	cnt.accountid=acc.id;
	cnt.LastName=acc.name;
	conList.add(cnt);
	}
	
	if(conList.size()>0)
	{
		insert conList;
	}
}
This was selected as the best answer