Hello,
From what you've posted, we assume that there's one group per department. If that's correct, then the best way would be to store the department manager in a certain property of a Group. For this purpose, you can use an AD property of the DN string syntax and that you don't use for other purposes. For example, you can set the manager in the Managed By property. Using a property of the DN string format gives you the advantage of having the Browse button in the field that represents the property, so you don't have to input the department manager manually. You'll be able to select a department manager from AD.
As for setting the manager for a user automatically, in your Business Rule or Custom Command that adds/removes users to/from groups based on departments, you'll be able to use a PowerShell script and the Run a program or PowerShell script action that will copy the department manager's DN from the Managed By property of the group to the Manager property of the user, thus setting a new manager for the user. Here's a script that performs such a task:
# Bind to the group that represents the user's department
try
{
$group = $Context.BindToObjectByDN("CN=%department%,OU=Departments,OU=Groups,DC=corp,DC=com")
}
catch
{
$Context.LogMessage("Cannot set a manager for the user because there is no group for the user's department (%department%).", "Warning")
return
}
# Get the department manager
try
{
$manager = $group.Get("managedBy")
}
catch
{
$Context.LogMessage("Cannot set a manager for the user because the user's department doesn't have a manager (%department%).", "Warning")
return
}
# Set the manager
$Context.TargetObject.Put("manager", $manager)
$Context.TargetObject.SetInfoEx(@("manager"))