Reputation: 4663
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:
Upvotes: 0
Views: 2318
Reputation: 7224
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.
Upvotes: 0
Reputation: 4663
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" };
Upvotes: 2
Reputation: 15128
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
Upvotes: 1