Task: Write code that searches the database for the record ID of the account owner and put that ID into a user lookup field on the opportunity.


Steps to implement in trigger.

List<Id> accountIdList = new List<Id>();
Map<Id,Id> accOwnerMap = new Map<Id,Id>();
for(Opportunity op : Trigger.new) {
for(Account acc : [Select OwnerId from Account Where id in :accountIdList] ) {

for(Opportunity op : Trigger.new) {
      op.userLoookup = accOwnerMap.get(op.AccountId)

Basically Opportunities are childern of Account object.So i am assuming that you want to write a script to copy the account owner to a custom field say AccountOwner__c(lookup of User) on all the related opportunities.


1.Basically you need a custom field on opportunity object say AccountOwner__c(lookup of User).

2.You need to query all account records, in that query you will also retrieve the child opportunity records.You will take result in List accList. eg.

Select Id, OwnerId, (Select id,AccountOwner__c from Opportunities) from Account

3.You will create a map<AccountId, RelatedOpportunityRecords>

4.By using accList and the above map you can copy the Owner field of account to the related Opportunity records.

As helping by code is not generally suggested in the community so i'm keeping that spirit up :P :)

Happy Coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.