You need to sign in to do that
Don't have an account?
Abraham Durojaiye
How to create an Apex Trigger to auto-populate a custom field in an Opportunity object once the Account name is entered.
Hi,
I am new to developing so I am not sure how to approach this task. I created a custom lookup field in my Accounts object called Relationship Manager. This field is manually populated with a user name. I also addes the same Relationship Manager field into my Opportunity object and would like that field to auto-populate the user name based on the Account name entered.
Here is my attempt at creating the apex trigger below. I am getting an error message that states:
Error: Compile Error: unexpected token: '<EOF>' at line 6 column 0
trigger RelationshipManager on Opportunity (before insert) {
for (Opportunity o = Trigger.new;
o.AccountId = true;
)
I appreciate any assistance I can get.
Thanks
I am new to developing so I am not sure how to approach this task. I created a custom lookup field in my Accounts object called Relationship Manager. This field is manually populated with a user name. I also addes the same Relationship Manager field into my Opportunity object and would like that field to auto-populate the user name based on the Account name entered.
Here is my attempt at creating the apex trigger below. I am getting an error message that states:
Error: Compile Error: unexpected token: '<EOF>' at line 6 column 0
trigger RelationshipManager on Opportunity (before insert) {
for (Opportunity o = Trigger.new;
o.AccountId = true;
)
I appreciate any assistance I can get.
Thanks
In general, you have to query the database to get at the related records
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks
All Answers
Please use the below trigger code :
Let me know if you have any issue or you need more help on this.
Thanks,
Abhishek Bansal
In general, you have to query the database to get at the related records
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks
I got it!
Thanks again for setting me up with the trigger for the Relationship Manager field.
The one thing I have noticed now is that this trigger only works when setting up a new record. It does not work when attempting to edit an existing record. Meaning that, if I go into an existing record to make a change, the auto-populate trigger does not work.
Can you please help with this?
Here is the trigger I currently have in place per you:
trigger RelationshipManager on Opportunity (before insert) {
map<id,account> accounts = new map<id, account>();
for (opportunity o:trigger.new) {
accounts.put(o.accountid,null);
}
accounts.remove(null);
accounts.putAll([Select id, name, Relationship_Manager__c from account where id in :accounts.keyset()]);
for(opportunity o:trigger.new) {
if(accounts.containskey(o.accountid)) {
o.Relationship_Manager__c = accounts.get(o.accountid).Relationship_Manager__c;
}
}
}
Please update your trigger as follows :
Thanks,
Abhishek