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
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
}
}
}
}
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 .
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
}
}
}
}
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 .
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 .
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
Regards,
Bhanu Mahesh
All Answers
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 .
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
Regards,
Bhanu Mahesh
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 .