function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HARISH S 11HARISH S 11 

a trigger on account object and populate owner name in any custom field.

COuld someone please help me with a trigger on account object and populate owner name in any custom field.
Adarsh.SharmaAdarsh.Sharma
Hi Harish,

Please try these this:
trigger Accounts on Account (before insert) {
    if(trigger.isBefore){
        if(trigger.isInsert){
            set<id>setOwnerId = new set<id>();
            map<Id,User>mapUserIdWiseUser = new map<Id,User>();
            for(Account oAcc:trigger.new){
                setOwnerId.add(oAcc.OwnerId); 
            }
            if(setOwnerId.size()>0){
                for(User oUser:[Select Id,Name From User Where Id In:setOwnerId]){
                    mapUserIdWiseUser.put(oUser.Id,oUser);
                }
                if(!mapUserIdWiseUser.isEmpty()){
                    for(Account acc:trigger.new){
                        if(mapUserIdWiseUser.containsKey(acc.OwnerId)){
                            acc.Description = mapUserIdWiseUser.get(acc.OwnerId).Name;
                        }
                    }
                }
            }
        }
    }
}

Please  replace "Description" field with your custom field.

Hope this will help you!
Please mark this as best solution.

Regards