Tuesday, June 26, 2012

Updating the Owner in CRM.


For updating the Owner field programatically, we need to make use of the Assignrequest message. I have captured the sample code to update the Owner Id in CRM 4.0 and CRM 2011.

Updating the Owner Id in CRM 4.0:

Note: Plase, make sure to update the "ownerUpdatingEntity" with one you are trying to update. Also, Replace the "Entity_Name" with the name of the entity you are updating.

private static void AssignUser(CrmService service, Guid ownerId, Guid recordId)
{
 SecurityPrincipal assignee = new SecurityPrincipal();
 assignee.PrincipalId = ownerId;
 TargetOwnedDynamic ownerUpdatingEntity = new TargetOwnedDynamic();

 ownerUpdatingEntity.EntityId = recordId;
 ownerUpdatingEntity.EntityName = "Entity_Name";

 AssignRequest assign = new AssignRequest();

 assign.Assignee = assignee;
 assign.Target = ownerUpdatingEntity;

 AssignResponse assignResponse = (AssignResponse)service.Execute(assign);
}


Updating the Owner Id in CRM 2011:

Note: Please, make sure to replace the "Account.EntityLogicalName" with the right Entity in the below code.
 

private static void AssignUser(IOrganizationService service, Guid ownerId, Guid recordId)
{
 // Create the Request Object and Set the Request Object's Properties
 AssignRequest assign = new AssignRequest
 {
   Assignee = new EntityReference(SystemUser.EntityLogicalName, ownerId),
   Target = new EntityReference(Account.EntityLogicalName,recordId)
 };
 // Execute the Request
 service.Execute(assign);
}

Hope this helps!

 

No comments: