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
Mark SchaferMark Schafer 

Trigger to display Account owner in custom field on Opportunity

I am looking to create a trigger to get the Account Owner Name to display in a custom text field on the Opportunity object. I found a post with code for a similar trigger. I got some help trying to adapt this to my problem, but I'm still getting an error: 

 

Error: Compile Error: Incompatible element type SOBJECT:Account for collection of Id at line 7 column 9

 

Does anyone have any suggestions?

 

trigger trgAccOwner on Opportunity (before insert,before update) 
{
    Set<Id> accid=new  Set<Id>();
    Set<Id> accownerid=new  Set<Id>();
    for(Opportunity  tl:Trigger.new)
    {
        accid.add(tl.account);
    }
    map<id,account> mpacc=new map<id,account>([select id,name,ownerid from account where id in :accid]);
    for(account acc:mpacc.values())
    {
        accownerid.add(acc.ownerid);
    }
    map<id,user> mpuser=new map<id,user>([select id,name from user where id in :accownerid]);
    if(mpuser.size()>0)
    {
        for(integer i=0;i<Trigger.new.size();i++)
        {
            Trigger.new[i].Account_Owner__c=mpuser.get(mpacc.get(Trigger.new[i].account.ownerid.name));
        }
    }
}

 

Thanks much!

LegendLegend

Hi,

 

The error which you are getting because you are assigning Account to an ID field.

 

You should have to update your code at line 7:

 

for(Opportunity  tl:Trigger.new)
    {
        accid.add(tl.account.Id);
    }