i am trying to create contact upon account insertion
public static Account createContact()
{ List<account> acc= new List<Account>();
for(Account a : Trigger.new)
{
Contact c=new Contact();
C.LastName='Sharma';
c.Description=a.name;
acc.add(c);
}
insert acc;
}
but there is error
unexpected token: 'Account'
You are adding the Contact object instance to Account List , which is Wrong .
For you scenario, the code would be:
Trigger ContactInsertor on Account (after insert) {
List<Contact> conList = new List<Contact>();
if(Trigger.isAfter && Trigger.isInsert){
for(Account a : trigger.new) {
Conatct c = new Contact();
c.LastName='Sharma';
c.Description=a.name;
conList.add(c);
}
insert conList;
}
}
}