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

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;
     }
ShashankShashank (Salesforce Developers) 
Are you having a difficulty with this? If so, what is it?
RamuRamu (Salesforce Developers) 
Plese modify the code as below
Try the below code and let me know if this worked.

trigger createcontacts on Account (after insert) {
L​ist<contact> con=new list<contact>();     
   
   for(account acc:trigger.new)
  {
   contact c1=new contact();
     c1.accountid=acc.id;
     c1.lastname=acc.name;
     con.add(c1);
 }
   if(con.size>0){
     insert con;
}
}

 
Sree SalesforceSree Salesforce
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
 
Sree SalesforceSree Salesforce
if i run the above code i got an error like Compile Error: Invalid type: l​ist at line 04 column 2
RamuRamu (Salesforce Developers) 
Oops I found that there was a hidden character in my code in the word 'List' something as L*ist. Please copy the below one. I also have done a small correction 
 
trigger createcontacts on Account (after insert) {
L​ist<contact> con=new list<contact>();     
   
   for(account acc:trigger.new)
  {
   contact c1=new contact();
     c1.accountid=acc.id;
     c1.lastname=acc.name;
     con.add(c1);
 }
   if(con.size>0){
     insert con;
}
}