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
HelloSanHelloSan 

i need a apex trigger code to autopopulate the account owner on the opportunity object field for all the pre exsisting account and opportunities,how can i achieve this

Best Answer chosen by HelloSan
Bhanu MaheshBhanu Mahesh

Hi HelloSan,

If you want the name of the Account Owner on the Opportunity, you can acheive this by simply creating a formula field of type Text and the fomula will be Account.Owner.FirstName + ' ' +  Account.Owner.LastName . This formula will work for both existing records and new records without any extra efforts.

You can achieve this by using by WorkFlow and triggers as well. But for both Workflows and triggers to fire there should be DML operation on those records. For newly created or updated records it will work But to populate that field for exixting records you have to update thos erecords.

You can simply update existing opportunities by just including the Id field in the CSV file and do update operation using data loader

For Workflow,
Evaluation Criteria will be every time record created or updated
Rule Criteria will be AccountId != null
Add a field update with formula Account.Owner.FirstName + ' ' +  Account.Owner.LastName 

If you want it will be a lookup field or if you want to do it by trigger below is the code
If you want a look up field on Opportunity lets suppose API Name will be Account_owner_lookUp__c
If you want a text field on Opportunity with name of account owner lets suppose API Name will be Account_owner_Name_Text__c
 

trigger UpdateAccountOwner on Opportunity(before insert,before update){
	Set<Id> accIds = new Set<Id>();
	Map<Id,Account> mapIdWithAccount = new Map<Id,Account>();
	
	for(Opportunity opp : trigger.new){
		if(opp.AccountId != null){
			accIds.add(opp.AccountId);
		}
	}
	if(!accIds.isEmpty()){
		for(Account acc : [SELECT Id, OwnerId,Owner.Name FROM Account WHERE Id IN :accIds]){
			mapIdWithAccount.put(acc.Id,acc);
		}
		
		for(Opportunity opp : trigger.new){
			if(opp.AccountId != null && mapIdWithAccount != null && mapIdWithAccount.get(opp.AccountId) != null){
					opp.Account_owner_lookUp__c = mapIdWithAccount.get(opp.AccountId).OwnerId; //For Lookup field
					opp.Account_owner_Name_Text__c = mapIdWithAccount.get(opp.AccountId).Owner.Name; //For text field
			}
		}
	}
}

Regards,
Bhanu Mahesh

All Answers

ManojjenaManojjena
Hi San,
Trigger code will execute only when u do the DML operation .

If you want that your trigger code will populate all account owner in any of the opportunity field . or Opportunity owner field ,then u need to write tigger on before insert & before update and needs to update all opportunity once .
After that trigger will take care of the new opportunity which will create after onwards .

 
Bhanu MaheshBhanu Mahesh

Hi HelloSan,

If you want the name of the Account Owner on the Opportunity, you can acheive this by simply creating a formula field of type Text and the fomula will be Account.Owner.FirstName + ' ' +  Account.Owner.LastName . This formula will work for both existing records and new records without any extra efforts.

You can achieve this by using by WorkFlow and triggers as well. But for both Workflows and triggers to fire there should be DML operation on those records. For newly created or updated records it will work But to populate that field for exixting records you have to update thos erecords.

You can simply update existing opportunities by just including the Id field in the CSV file and do update operation using data loader

For Workflow,
Evaluation Criteria will be every time record created or updated
Rule Criteria will be AccountId != null
Add a field update with formula Account.Owner.FirstName + ' ' +  Account.Owner.LastName 

If you want it will be a lookup field or if you want to do it by trigger below is the code
If you want a look up field on Opportunity lets suppose API Name will be Account_owner_lookUp__c
If you want a text field on Opportunity with name of account owner lets suppose API Name will be Account_owner_Name_Text__c
 

trigger UpdateAccountOwner on Opportunity(before insert,before update){
	Set<Id> accIds = new Set<Id>();
	Map<Id,Account> mapIdWithAccount = new Map<Id,Account>();
	
	for(Opportunity opp : trigger.new){
		if(opp.AccountId != null){
			accIds.add(opp.AccountId);
		}
	}
	if(!accIds.isEmpty()){
		for(Account acc : [SELECT Id, OwnerId,Owner.Name FROM Account WHERE Id IN :accIds]){
			mapIdWithAccount.put(acc.Id,acc);
		}
		
		for(Opportunity opp : trigger.new){
			if(opp.AccountId != null && mapIdWithAccount != null && mapIdWithAccount.get(opp.AccountId) != null){
					opp.Account_owner_lookUp__c = mapIdWithAccount.get(opp.AccountId).OwnerId; //For Lookup field
					opp.Account_owner_Name_Text__c = mapIdWithAccount.get(opp.AccountId).Owner.Name; //For text field
			}
		}
	}
}

Regards,
Bhanu Mahesh
This was selected as the best answer
HelloSanHelloSan
Thanks Manoj and Mahesh, this code is working for new opportunity creation and updation, but  i need the code to work for all existing accounts and opportunities already present in the production .
ManojjenaManojjena
Hi San,
 As suggested by Bhanu as well you need to update old records if you want to achive by trigger . If you don't want to update the old records then better go for formula fields .
There i sno way to achive this with the help of trigger without doing any DML .
 
HelloSanHelloSan
Thanks Manoj  you are right