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
Øyvind Borgersen 10Øyvind Borgersen 10 

Owner profile name in account record

Hi,

I'm creating a trigger to get the profile name from account owner in the account record. This should be a fairly simple task, but I'm stuck with no results in the code. Do anyone have a quick code for this?
Best Answer chosen by Øyvind Borgersen 10
Maharajan CMaharajan C
Hi Oyvind,

Please refer the below sample code:

trigger getAccountOwnerProfile on Account( before Insert,before update )
{
    
    Set<Id> ownerIDs = new Set<Id>();
    for( Account acc : trigger.new )
    {
        
        if( acc.OwnerId != null )
        {
            ownerIDs.add( acc.OwnerId );
        }
    
    }
    
    Map<Id, User> ownerMap = new Map<Id, User>([ Select Id, ProfileId, Profile.Name From User where Id IN: ownerIDs ]);
    
    for( Account a : trigger.new )
    {
            if( ownerMap.containsKey( a.OwnerId ) )
            {
                system.debug('Account Owner Profile Name ==> ' + ownerMap.get( a.OwnerId ).Profile.Name);
                a.Description = ownerMap.get( a.OwnerId ).Profile.Name;
            }
        
    
    }   
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj C

All Answers

Maharajan CMaharajan C
Hi Oyvind,

Please refer the below sample code:

trigger getAccountOwnerProfile on Account( before Insert,before update )
{
    
    Set<Id> ownerIDs = new Set<Id>();
    for( Account acc : trigger.new )
    {
        
        if( acc.OwnerId != null )
        {
            ownerIDs.add( acc.OwnerId );
        }
    
    }
    
    Map<Id, User> ownerMap = new Map<Id, User>([ Select Id, ProfileId, Profile.Name From User where Id IN: ownerIDs ]);
    
    for( Account a : trigger.new )
    {
            if( ownerMap.containsKey( a.OwnerId ) )
            {
                system.debug('Account Owner Profile Name ==> ' + ownerMap.get( a.OwnerId ).Profile.Name);
                a.Description = ownerMap.get( a.OwnerId ).Profile.Name;
            }
        
    
    }   
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj C
This was selected as the best answer
Øyvind Borgersen 10Øyvind Borgersen 10
Hi Raj,

Thanks, this seems to have solved the issue. I have one more question for you. Why do you use before trigger instead of after trigger? Part of my failed apex was due to after trigger which I would say make more sense in this case.
Maharajan CMaharajan C
If you want to modify the Same record which is firing the trigger then it should handled in the before transaction . In the after transaction that record will become as read only.

If you are going to perform the dml in any related objects then use the after.
Øyvind Borgersen 10Øyvind Borgersen 10
OK, got it. Thanks a lot.