Question

I have an MS CRM 2013 that I want to set the OwnerId on an Account to a Business Unit using the REST interface (and the VS2013 proxy class created).

I've tried a number of ways, but seems like something like this should work. However, it throws an "Invalid ownerIdType = 10" error message

var crmService = new CrmServiceReference.Context(crmUri);
var owner = crmService.BusinessUnitSet.First();
var newAccount = new CrmServiceReference.Account();
newAccount.AccountNumber = "123456";
newAccount.Name = "Hello World";
newAccount.Ownerid = new CrmServiceReference.EntityReference() { Id = owner.BusinessUnitId, Name = owner.Name, LogicalName = "businessunit" };
crmService.AddToAccountSet(newAccount);
crmService.SaveChanges();

I've also tried:

  • sending just the Id (error: no system user)
  • setting .OwningBusinessUnit = owner (which doesn't set the owner to the business unit)
  • looked into .OwnershipCode (but wasn't able to set/determine what that is)
  • removed .OwnerId = line and tried crmService.AddLink(newAccount, "ownerId", owner); (The closed type CrmServiceReference.Account does not have a corresponding ownerId settable property)
  • removed .OwnerId = line and tried crmService.AddRelatedObject(newAccount, "ownerId", owner); (The closed type CrmServiceReference.Account does not have a corresponding ownerId settable property)
Was it helpful?

Solution

It looks like a team is created with each business unit and that the ownerId should be the team rather than the business unit, so ...

var owner = crmService.BusinessUnitSet.First();

becomes

var team = crmService.TeamSet.First();

and

newAccount.Ownerid = new CrmServiceReference.EntityReference() { Id = owner.BusinessUnitId, Name = owner.Name, LogicalName = "businessunit" };

becomes

newAccount.Ownerid = new CrmServiceReference.EntityReference() { Id = team.TeamId, Name = owner.Name, LogicalName = "team" };

OTHER TIPS

Business Units can't own records. The owner of a record can be only a user or a team.

Just for your information, to change the owner you need to use AssignRequest message

http://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.assignrequest.aspx

To assign ownership of a new record to the default Team for a Business Unit use the following code:

        var biz = crmService.BusinessUnitSet.First();
        var owner = crmService.TeamSet.First(x => x.BusinessUnitId.Id == biz.BusinessUnitId && x.IsDefault == true);
        var newAccount = new Account();
        newAccount.AccountNumber = "123456";
        newAccount.Name = "Hello World";
        newAccount.OwnerId = owner.ToEntityReference();

You can a where clause to the first line to get a specific Business Unit, if necessary.

Obviously, you should add error handling and possibly change to FirstOrDefault to avoid errors. I'll leave how you manage those issues up to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top