Sep 21, 2015

Determine if Owner (SystemUser /Team) and assign to a record

In Dynamics CRM Owner can be either a user or a team. This can be little tricky when reading Owner from a record and set to another one.

Please check below screen shot of Account. You can simply check this with OwnerIdType, yet you can’t retrieve that field for some reason.

 
Still we can check two different fields that holds Either User Id or team Id.

In fact, I tried below approach which worked for me.

String OwningEntityType = String.Empty;
if (_account.OwningTeam != null)
{
    OwningEntityType = "team";
}
else
{
    OwningEntityType = "systemuser";
}

AssignRequest assignAcc = new AssignRequest
{
     Assignee = new EntityReference(OwningEntityType, _account.OwnerId.Id),
     Target = new EntityReference(<Target Entity Name>, <Target Entity Name ID>)
};
organizationService.Execute(assignAcc);