0
votes

I have created a trigger that will auto-create a contact when a specific Account record type is created or updated. The problem is that I am not sure how to populate the Contact 'Account Name' lookup field. This field is a lookup to the Account object. My code is below. Any help on how to integrate this missing component would be greatly appreciated.

trigger autoCreateContact on Account (after update, after insert) { List newContact = new List();

for (Account oAccount : trigger.new)
{
    if (oAccount.RecordTypeid == '012F0000001MCfgIAG')
    {
        List<Contact> cCheck = [SELECT ID From Contact WHERE LastName=:oAccount.Name];
        if(cCheck.isEmpty()==True)
        {
        System.debug(oAccount);
        Contact oContact = new Contact();
        oContact.LastName = oAccount.Name;
        oContact.phone = oAccount.Phone;
        oContact.email = oAccount.Email__c;
        oContact.Owner = oAccount.Owner;
        newContact.add(oContact);
        }
    }

   if(newContact.isEmpty() == false)
   {
       Database.insert(newContact);
   }

    }
}
2

2 Answers

0
votes

nice trigger I'm pretty sure you just need to add one line which is a reference to the account.id.

So if I were you I would add the link:

oContact.AccountID = oAccount.id;
0
votes

NOTE: its not a good practice to have a SOQL inside the for loop.