My goal is to enable my team to update a particular field on the Opportunity Object (Stage__c) to 'Closed Won', and have that auto-generate a new custom child object (Onboarding) that is linked to the same account that the Opportunity is linked to.
Now the problem seems to be that I am attempting to create an Onboarding page (custom child obj) from a field change on the Opportunity page (child obj) and have the new Onboarding page linked to the same Account that the Opportunity was linked to.
trigger CreateOB on Opportunity (after insert, after update) {
Onboarding__c[] inserts = new Onboarding__c[] {};
for (Opportunity a : Trigger.new) {
if (a.StageName == 'Closed Won') {
Opportunity old = Trigger.oldMap.get(a.Id);
if (a.StageName != old.StageName) {
inserts.add(new Onboarding__c(Account_Name__c = a.Id));
}
}
}
insert inserts;
}
I am able to save the code but am receiving the following error on the Opportunity page when I attempt to submit it with the StageName = Closed Won:
Error:Apex trigger CreateOB caused an unexpected exception, contact your administrator: CreateOB: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Account Name: id value of incorrect type: 00611000007THcnAAG: [Account_Name__c]: Trigger.CreateOB: line 11, column 1