You need to sign in to do that
Don't have an account?
Mark Schafer
Display Account Owner on Opportunity Page
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 assume you are plugging in the owner's name and not the ID in the custom field. This should work better for you.
You can get the Owner Name through a relational reference and thus you need only one map, and you use the account ID in opportunity to reference the map's values. You don't need to use an integer for loop to iterate through the trigger's list.
All Answers
I assume you are plugging in the owner's name and not the ID in the custom field. This should work better for you.
You can get the Owner Name through a relational reference and thus you need only one map, and you use the account ID in opportunity to reference the map's values. You don't need to use an integer for loop to iterate through the trigger's list.
Why dont you use a formula field?
Thanks dmcheng. This is just what I was looking for.
Carlo- I started by trying to use a formula field set up as simply Account.Owner but it did not recognize the Owner field. I tried Account.OwnerName and a few other things like that and finally did some digging on the regular board. Apparently there are some limitations pulling Owner info into a formula field in that way.
I could haved missing something along, but I was ultimately directed to triggers and got a little crash course. I was able to make this work and deploy it into Production.
Thanks again!
Noted for future reference.
carlo - you can get the owner ID through a formula field, but not the owner name.
Thanks