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
Abraham DurojaiyeAbraham 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     
 
Best Answer chosen by Abraham Durojaiye
William TranWilliam Tran
Abraham, here's a sample code:

In general, you have to query the database to get at the related records
 
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;


    }
  }
}

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

Abhishek BansalAbhishek Bansal
Hi Abraham,

Please use the below trigger code :
trigger RelationshipManager on Opportunity (before insert) {
	Set<id> accIds = new set<id>();
	for(Opportunity opp : trigger.new){
		if(opp.AccountId != null){
			accIds.add(opp.AccountId);
		}
	}
	Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select Relationship_Manager__c from Account where id in :accIds]);
	
	for(Opportunity opp : trigger.new){
		if(opp.AccountId != null && mapOfAccounts.containsKey(opp.AccountId)){
			opp.Relationship_Manager__c = mapOfAccounts.get(opp.AccountId).Relationship_Manager__c;
		}
	}
}
//Please replace Relationship_Manager__c field with its original API name for both account and opportunity

Let me know if you have any issue or you need more help on this.

Thanks,
Abhishek Bansal
William TranWilliam Tran
Abraham, here's a sample code:

In general, you have to query the database to get at the related records
 
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;


    }
  }
}

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
This was selected as the best answer
Abraham DurojaiyeAbraham Durojaiye
Thanks for the quick responses. 

I got it!
Abraham DurojaiyeAbraham Durojaiye
Hi William,

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;
        
        
        }
    }
}
Abhishek BansalAbhishek Bansal
Hi Abraham,

Please update your trigger as follows :
trigger RelationshipManager on Opportunity (before insert, before update) {
    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;
        
        
        }
    }
}

Thanks,
Abhishek