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

How to get record owner instead of current user?
I have the below trigger in my custom object. When a record is saved, the Manager field is being updated (via a lookup against the userInfo object)
It works fine, except that when another user edits an existing record, the Manager field is being updated again with the current users manager.
So in fact I should be getting the Manager of the record Owner rather than the Mnaager of the current user.
trigger trg_LineManager on Request_for_System_Change__c (after insert, after update, before insert, before update) { string LineManagerid = [SELECT Id, ManagerId from User where Id =: userinfo.Getuserid()].ManagerId; if (trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) { for (Request_for_System_Change__c r : Trigger.new) { r.Manager__c = LineManagerId; } } }
It works fine, except that when another user edits an existing record, the Manager field is being updated again with the current users manager.
So in fact I should be getting the Manager of the record Owner rather than the Mnaager of the current user.
How can I modify that trigger to get the record owner for the Manager lookup, instead of the current user?
Just change your trigger code with the below code and everything will be fine : Now the Manager field will be set only at the time when a record is craeted and not at the time when record is updated.
Please let me know if your requirement is something different or if you need any further help on this.
Thanks,
Abhishek Bansal.