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
Robert Lange 6Robert Lange 6 

I have been trying to write an Apex trigger that adds the following Contact to ALL Account records.

The trigger should relate the Contact to the Account record.  I have tried the following code.  I am getting an error message on the line, "WhatID = acc.ID;"

trigger CreateContact on Account (after insert) {
    for (Account acc : Trigger.new){
        Contact c = new Contact();
        c.FirstName = 'Jo Jo';
        c.LastName = 'Murphy';
        WhatID = acc.ID;
        insert c;        
    }

}
Best Answer chosen by Robert Lange 6
SarvaniSarvani
Hi Robert,

You need to place c.AccountId=acc.ID in place of whatId. I belive you are trying to relate the contact for any new Account inserted.
trigger CreateContact on Account (after insert) {
    for (Account acc : Trigger.new){
        Contact c = new Contact();
        c.FirstName = 'Jo Jo';
        c.LastName = 'Murphy';
        c.AccountId=acc.ID;
        insert c;        
    }

}

Hope this helps! Please mark as best if it does

Thanks

All Answers

SarvaniSarvani
Hi Robert,

You need to place c.AccountId=acc.ID in place of whatId. I belive you are trying to relate the contact for any new Account inserted.
trigger CreateContact on Account (after insert) {
    for (Account acc : Trigger.new){
        Contact c = new Contact();
        c.FirstName = 'Jo Jo';
        c.LastName = 'Murphy';
        c.AccountId=acc.ID;
        insert c;        
    }

}

Hope this helps! Please mark as best if it does

Thanks
This was selected as the best answer
Robert Lange 6Robert Lange 6
Thank you.  I'm still getting an error for Line 0 that says, "invalid parameter value."  Any thoughts?
SarvaniSarvani
The code will work fine with no issues unless you have other triggers or apex code which is causing the issue. Try closing all the apex triggers or classes and recreate the trigger on Account object and paste the same code. Also, cross check in your error section (Problems) Name column which should say the file in which it's causing the issue (I believe its not the trigger on which you are working).

Thanks