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
Awesome User11Awesome 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?
 
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;
}

 
Best Answer chosen by Awesome User11
Anil KamisettyAnil Kamisetty
Correct the line # 6 to 10.They should be like as given below.

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

Anil KamisettyAnil Kamisetty
Looks like you are trying to build the relationships. Couple of observations. Try set the ID fields, not the Name fields.

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.
Awesome User11Awesome User11
I fixed the issue of the v. and o.
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
 
Anil KamisettyAnil Kamisetty
Yes, Name field is not Writable. You have to set the ID value, not the Name. For example,
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.
Awesome User11Awesome User11
Thanks Anil.
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;
Anil KamisettyAnil Kamisetty
If your problem got addressed, help others by choosing the right answer.

Glad to be of some help to you.
Awesome User11Awesome User11
Hi Anil,
​the issue above with the Complie Error didn't.
Anil KamisettyAnil Kamisetty
Paste the enite code, so it can be reviewed. Examine the line # specified, looks like you were not handling Numbers / String values properly. Probably doing some arithmatic operation with String value.
Awesome User11Awesome User11
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();
			l.Account__r.Name = la.Account_Name__c;
			l.Id = la.Account__c;
			l.Account__r.Enterprise_Account__r.Name = la.Enterprise_Account__c;
			l.Account__r.Billing_Location__r.Name = la.Main_Account__c;
			l.Opportunity__c = la.Opportunity__c;

	
			recordsToInsert.add(la);
		}
	}
	insert recordsToInsert;
}

here it is.
sfdcMonkey.comsfdcMonkey.com
hi Plase try this
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();
		    la.Account_Name__c = l.Account__r.Name ;
			la.Account__c = l.Id;
			la.Enterprise_Account__c = l.Account__r.Enterprise_Account__r.Name ;
			la.Main_Account__c = l.Account__r.Billing_Location__r.Name;
			la.Opportunity__c = l.Opportunity__c ;

	
			recordsToInsert.add(la);
		}
	}
	insert recordsToInsert;
}
Thanks let me infrom if it work :)
 
Awesome User11Awesome User11
got this:
Error: Compile Error: Field is not writeable: Opportunity_Hierarchy__c.Enterprise_Account__c at line 8 column 13
sfdcMonkey.comsfdcMonkey.com
can you please share your Opportunity_Hierarchy__c ,Additional_Account__c  object field's screenshort from object view page
Thanks
Anil KamisettyAnil Kamisetty
Problem seems to be in the line
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.
 
Awesome User11Awesome User11
Thanks!
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
trigger OpptyHierarchyCreate on Additional_Account__c (after insert) {
    List<Opportunity_Hierarchy__c> recordsToInsert = new List<Opportunity_Hierarchy__c>();
    for (Additional_Account__c l : Trigger.new) {
        if (Trigger.IsInsert) {
            Opportunity_Hierarchy__c la = new Opportunity_Hierarchy__c();
            l.Account__c = la.Account_Name__c;
            l.Id = la.Account__c;
            l.Account__r.Enterprise_Account__r.Id = la.Enterprise_Account__c;
            l.Account__r.Billing_Location__r.Id = la.Main_Account__c;
            l.Opportunity_ID__c = la.Opportunity__c;
            
            recordsToInsert.add(la);
        }
    }
    insert recordsToInsert;

 
Anil KamisettyAnil Kamisetty
I am little bit confused here., you are building Opportunity Hierarchy (instance name L). But when it comes to Records toInsert, you were adding LA. On Line#, you were creating Opp Hierarchy Isntance la. Can you please check your variables one time ? Using L and LA wrongly.

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.
Awesome User11Awesome User11
When I create a new Additional Account Record, the trigger should create a new Opportunity Hierarchy records based off of the fields above.
Anil KamisettyAnil Kamisetty
Correct the line # 6 to 10.They should be like as given below.

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.

 
This was selected as the best answer
Awesome User11Awesome User11
perfect!
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.
Anil KamisettyAnil Kamisetty
If your question is answered, dont forget to choose the right answer.
Awesome User11Awesome User11
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.
Anil KamisettyAnil Kamisetty
As they are formula fields, you cannot assign them any values. Thought you have removed those two statements from your Trigger.

Remove them and try .
Awesome User11Awesome User11
Got it!!!!!!!!!
Anil KamisettyAnil Kamisetty
If you are all set, please mark your answer
NagaSudheer Kamepalli 4NagaSudheer Kamepalli 4
I am fasing the error variable does'nt exit Match__Billing__Adreess__c can any one help Me
User-added image