You need to sign in to do that
Don't have an account?
Ø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?
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?
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
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
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.
If you are going to perform the dml in any related objects then use the after.