You need to sign in to do that
Don't have an account?
Awesome User11
Apex Trigger to create record on custom object from another custom object fields and related fields
I am looking to create a trigger off of a custom object (ObjA - Looks up to Accounts and Opportunities) that will create a record in custom object (ObjB) based on fields from ObjA and some related fields from Accounts/Opportunities.
I receive errors that my related fields do not exist. Can anyone provide insight?
I receive errors that my related fields do not exist. Can anyone provide insight?
trigger OpptyHierarchy on Additional_Account__c (after insert) { List<Opportunity_Hierarchy__c> recordsToInsert = new List<Opportunity_Hierarchy__c>(); for (Additional_Account__c l : Trigger.new) { if (l.IsInsert) { Opportunity_Hierarchy__c la = new Opportunity_Hierarchy__c(); V.Account__r.Name = O.Account_Name__c; v.Name = o.Account__c; v.Account__r.Enterprise_Account__r.Name = o.Enterprise_Account__c; v.Account__r.Billing_Location__r.Name = o.Main_Account__c; v.Opportunity__c = o.Opportunity__c; recordsToInsert.add(la); } } insert recordsToInsert; }
la.Account_Name__c = l.Account__c ;
la.Account__c = l.Id;
la.Enterprise_Account__c = l.Account__r.Enterprise_Account__r.Id;
la.Main_Account__c = l.Account__r.Billing_Location__r.Id;
la.Opportunity__c = l.Opportunity_ID__c;
You have to adjust the fields slightly, based on the definition though.
All Answers
At the same time, you referencing fields with O as the instance name. Where is this defined ? Probably the Instantiation step is missing here (O).
See if this helps.
I am using l and la for my insert.
However, I am being told that the field isn't writeable for:
l.Name
l.Opportunity
V.Account__r.Name = O.Account_Name__c; << Change this to V.Account__c = O.Account_Id__c;
Note : Account__c is the Account Id and Account__r is the Relationship Reference.
If you are Capturing Account Name in the Account_Name__c field, you need to get the Account Id and then assign it there. Probably you need to revise your logic.
This worked.
However, I got this compile error now:
Error: Compile Error: Arithmetic expressions must use numeric arguments at line 9 column 12
for
v.Account__r.Enterprise_Account__r.Name = o.Enterprise_Account__c;
v.Account__r.Billing_Location__r.Name = o.Main_Account__c;
Glad to be of some help to you.
the issue above with the Complie Error didn't.
here it is.
Error: Compile Error: Field is not writeable: Opportunity_Hierarchy__c.Enterprise_Account__c at line 8 column 13
Thanks
l.Account__r.Billing_Location__r.Name = la.Main_Account__c;
It appears that you are trying to set the Billing Location on the Account Relationship. You cannot set the Name, you need to get the Billing Location Id and have it on the Opportunity Hierarchy. And do the following.
l.Account__r.Billing_Location__c = la.Billing_Record_Id__c; // Billing Record Id is the Id for the Billing Location
Remember only Id's should be assigned, assigning Name would not build the relationship.
Hope it helps.
I think that it's all makes sense now.
The Apex Trigger is there, but I am now having an issue with my after insert.
OpptyHierarchyCreate: execution of AfterInsert caused by: System.FinalException: Record is read-only Trigger.OpptyHierarchyCreate: line 6, column 1
Line#5, LA is created and line#6, LA value is getting assigned to L. Not making sense.
But the error "Read Only", is not related to your trigger. SEems like related to the visibility rules around the objects you are playing. Correct the above error and see what happens first.
la.Account_Name__c = l.Account__c ;
la.Account__c = l.Id;
la.Enterprise_Account__c = l.Account__r.Enterprise_Account__r.Id;
la.Main_Account__c = l.Account__r.Billing_Location__r.Id;
la.Opportunity__c = l.Opportunity_ID__c;
You have to adjust the fields slightly, based on the definition though.
la.Enterprise_Account__c and la.Main_Account__c are formula text fields.
They lookup to the Enterprise and Main Account fields on the Account in question.
So they come back as field not writeable.
They lookup to the Enterprise and Main Account fields on the Account in question.
So they come back as field not writeable.
Remove them and try .