+ Start a Discussion

Account to Opportunity Update Trigger

Ihave a custom field on Account that is a user lookup called "SA Manager."  I also have an "SA Manager User" lookup field on Opportunity that is a user lookup.  Every opportunity related to that account needs to have the SA Manager User field updated when "SA Manager" field is changed on the Account.


I created a trigger to update it, but can't seem to get the code to compile.  My IF statement is off and I'm getting the following error:


Error: Compile Error: Illegal assignment from Schema.SObjectField to Id at line 13 column 8




trigger UpdateSAMgrOnOppty on Account (after update) { Account acctObj = Trigger.new[0]; IF (acctObj.SA_Manager__r.Id != null && ISCHANGED(acctObj.SA_Manager__r.Id))

User name = [SELECT u.Id, u.Name from User u where u.Id =: acctObj.SA_Manager__r.Id]; for (Opportunity oppty : [Select o.Id, o.SA_Manager_User__r.Id From Opportunity o where o.Account.id = :acctObj.id ]) { oppty.SA_Manager_User__c = name.id ; update oppty; } }


Any help is appreciated!



What's "ISCHANGED"? Is it yours? I don't think that is available in Apex...


and where is line 13 in your original source code?




You're doing a dangerous thing here:


1.  first of all you are only referencing Trigger.new[0] which is one record, not considering the case of a bulk update


2.  secondly you are updating a child object when a parent object is updated.  What if there are 10,000 opportunities on an account?



solution:  new text formula field on opportunity = account.SA_Manager__r.Id 


We've already tried the account.SA_Manager__r.Id field, but this field is part of an Opportunity approval process where the SA Manager User needs to receive an email, which you can't send to a formula field, and can't get to Account fields from an Opportunity approval.  So that won't work.


Obviously I haven't added anything for governor limits to my trigger yet.  I'm just trying to get the core functionality working first.