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

Update Lookup Field with Contents of Another Lookup Field in the Same Object
So, here's a newbie question (this is my very FIRST development project). I have a custom object called Account Corrections (Account_Correction__c), that we're using to document Account Owner change requests. On the form, there is a lookup field where the user looks up the Proposed Account Owner (Proposed_Account_Owner__c). Consequently, I want another lookup field to self-populate with the Proposed Account Owner's Manager (Proposed_Account_Owner_s_Manager__c). The reason I'm doing this is to facilitate an Approval Process on the backend that works best with lookup fields to dynamically specify approvers.
Anyway, I'm trying to use an "after insert" trigger to do the job , but I'm not clear on the approach to take. If someone could just put me on the right track, I would be VERY appreciative. If it helps, here are the fields from the Account Corrections object:
Status__c, Request_Details__c, Related_Account__c, Proposed_Account_Owner_s_Manager__c, Proposed_Account_Owner__c, OwnerId, Name, Id, Current_Acct_Owner_s_Manager__c, Current_Account_Owner__c, Assigned_To__c
The code below is as far as I've gotten:
trigger TRG_AccountCorrectionRequest_ChangeOwner on Account_Correction__c (after insert, after update) { for (Account_Correction__c ac: Trigger.new){ if (ac.Status__c == 'Submitted' && ac.Request_Details__c == 'Assign Different Account Owner'){ ac.Current_Acct_Owner_s_Manager__c = ac.Current_Account_Owner__r.ManagerId; ac.Proposed_Account_Owner_s_Manager__c = ac.Proposed_Account_Owner__r.ManagerId; } } }
Any assistance is very much appreciated. Thanks in advance!
Actually, I finally got this figured out. Here is the code that ended up working. Thanks for the assist by Vinit Kumar.
All Answers
Hi Jeff,
Follow these steps
1. You need to have before trigger instead of after
2. As manager Id is field from User object so you could not fetch it from triiger context list (triger.new)
3. You have to create a set to contain the user ids and then query the user object
4. Have a map of user
Your code would be like below. It not exact but the idea how to achieve it.
To know more about trigger s you can see : http://forceschool.blogspot.in/2011/06/decimal-rounding-in-apex.html#comment-form
let me know if you still face issues.
Thanks for your quick reply on this. I've been playing around with it, and evidently, I'm getting this error right now:
Save error: Illegal assignment from LIST<User> to MAP<Id,User>
Happening with this line of code:
Actually, I finally got this figured out. Here is the code that ended up working. Thanks for the assist by Vinit Kumar.
As an admin trying to learn enough about APEX to get by, this post really helped.
Thanks Jeff!
Glad this could be of use. I have since used this approach for other needs as well. It's a nice little piece of code to recycle.