I need to write a trigger in which If I have an Account named 'Smith' and it doesnt have any related contacts when It is edited, its contact is created. I am doing this way but I am getting an error.
trigger AddRelatedContacts on Account (after insert, after update)
{
Set<Id> accIdSet = new Set<Id>();
List<Contact> conList = new List<Contact>();
for(Account acc :Trigger.New)
{
accIdSet.add(acc.id);
}
map<Id,Account> mapAccounts = new map<Id,Account>([Select id, name, (Select id, lastname
From Contacts) From Account
Where Id IN : accIdSet]);
for(Account acc : Trigger.New)
{
Account a = new Account();
a.id = mapAccounts.get(acc.Id);
if(a.contacts.size <= 0)
{
Contact con = new Contact();
con.accountId = a.id;
con.lastname = a.name;
conList.add(con);
}
}
insert conList;
}
Error message is : Error: Compile Error: Illegal assignment from Account to Id at line 17 column 9