I figured I'd learn Apex by trying to convert processes that I build to triggers in a sandbox.
I'm trying to pass some contact information onto the header of the Account when the Contact has been identified as the primary contact.
here's what I have so far:
trigger ContactStuff on Contact (before insert, after insert, after update) {
List<Account> accList = new List<Account>();
for (Contact c : Trigger.new) {
if(c.Primary_Account_Contact__c == true){
Account a = new Account(
Id = c.Account.Id,
Contact_Email__c = c.Email,
Fax__c = c.Fax,
Full_Name__c = c.FirstName + ' ' + c.LastName,
Telephone__c = c.Phone);
accList.add(a);
}
}
update accList;
}
I'm getting the following error:
Error:Apex trigger ContactOnCreate caused an unexpected exception, contact your administrator: ContactOnCreate: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.ContactOnCreate: line 32, column 1
Best Answer
The
Account.Id
value is going to be null in the trigger context. You can't pull in parent fields. But in this case, you don't have to. Just useAccountId
instead.You should also:
make sure you only fire this trigger on your after events