• Mark Schafer
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies

I've been trying to use a custom field to display the owner of an account in a custom Opportunity field Account_Owner__c. I'm new to Apex, but found a similar trigger I'm trying to adapt to my purpose. I'm currently receiving the following error:

 

Compile Error: Invalid foreign key relationship: Account.ownerid at line 19 column 81

 

Any guidance here is much appreciated.

 

 

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.Id);
    }
    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!

 

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!

I've been trying to use a custom field to display the owner of an account in a custom Opportunity field Account_Owner__c. I'm new to Apex, but found a similar trigger I'm trying to adapt to my purpose. I'm currently receiving the following error:

 

Compile Error: Invalid foreign key relationship: Account.ownerid at line 19 column 81

 

Any guidance here is much appreciated.

 

 

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.Id);
    }
    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!

 

Hi,

 

I have a custom object "xyz" with a look up field to accounts

 

We need to see the name of the account owner, I cannot get it via formula (well, I could, but then I would need to modify the formula each time a new user is created and TEXT( Account__r.Ownership ) didn´t work)

 

I guess this is a simple trigger, to get the account owner name populated in the "xyz" object...

 

 

Any sample trigger you may have please to direct me to the right direction....

 

Thank you!!:) 

 

  • November 04, 2011
  • Like
  • 0