You need to sign in to do that
Don't have an account?

Issue with a basic trigger
So this is my first trigger going into production. I've gone through the workbooks but I have come to a stop as I'm not sure where I'm going wrong. With the trigger I'm trying to populate a lookup field on the opportunity with the current account owner that is associated to the opportunity.
Here is the code:
trigger AccountOwner on Opportunity (after update) { //update opportunity with current account owner List<Opportunity> AccountOwner = [SELECT Account.OwnerId, Acct_Owner__c FROM Opportunity FOR UPDATE]; for (Opportunity o: AccountOwner){ if(o.Acct_Owner__c <> Account.OwnerId){ o.Acct_Owner__c=Account.OwnerId; } } update AccountOwner; }
I'm getting a compile error at line 9 column 9 Illegal assignment from Schema.SObjectField to Id.
Suggestions/ help? It doesn't have to be a look up field, it can be changed to text if that is the issue.
thanks,
There may be a simplier solution, but the below code should work for one or more opportunities being sent to the trigger:
Hope this helps.
All Answers
When comparing the Account_Owner__c to the Account.OwnerId, you need to specify that it's the Account.OwnerId of the Opportunity that you are currently referenceing (variable o). See the code below, i hope that helps.
There may be a simplier solution, but the below code should work for one or more opportunities being sent to the trigger:
Hope this helps.
Thanks! Now I need to walk through this to fully understand what I just did. But it works!