Friday, January 28, 2011

CRM 2011 SDK - Set owner of record to inactive user

Can you programmatically set the owner of a record in CRM to an inactive user?  Yes, but see below for more information.

I'm working on a data migration project involving Salesforce.com to Dynamics CRM 2011 Online. The source data includes thousands of records that are assigned to inactive Salesforce.com users. Our client wants to maintain the owners in CRM but also wants the users who are inactive in Salesforce to be inactive in CRM.

That brought up the question: "Can you programmatically set the owner of a record in CRM to an inactive user?". Yes (see sample code below). The only requirement is that each user (systemuser) must have at least one security role assigned, otherwise the CRM proxy service will throw a FaultException with the message "SecLib::RetrievePrivilegeForUser failed - no roles are assigned to user.".

Here's the code I used to test this scenario:
Guid ownerIdForInactiveUser = new Guid("1412c4da-fb2a-e011-8526-1cc1def8ea35");

string accountName = "Tim Test 1";
Entity accountEntity = new Entity("account");
accountEntity["name"] = accountName;
accountEntity.Attributes.Add("ownerid", new EntityReference("systemuser", ownerIdForInactiveUser));
try
{
   crmService.Create(accountEntity);
}
catch (FaultException ex)
{
   Console.WriteLine(ex.ToString());
}
Cheers,
-Tim

1 comment:

  1. This was a big help for me while I have been transitioning our product code based to CRM2011.

    Thanks again for the post...it worked perfectly for me.

    ReplyDelete