To Set Value For Owner Field In Dynamics CRM Using Plugin

In this blog, I will demonstrate how to set a value for the owner field on the record creation in Child entity (Contact) from Parent entity (Account) plugin, using C# in Dynamics CRM.

Creating a Plugin

The code snippet given below shows how the owner field of the newly created record is being set but one thing, which we need to know is this process can be done only during the pre-creation stage. If we are trying to update the owner field of a record, which is already created, then we may need to use AssignRequest, as shown in section 2.

Section:1 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.Xrm.Sdk;  
  7. using Microsoft.Xrm.Sdk.Messages;  
  8. using System.ServiceModel.Description;  
  9. using Microsoft.Xrm.Sdk.Query;  
  10. using System.Runtime.Serialization;  
  11. namespace projectname.fieldmappings {  
  12.     public class FieldMappings: IPlugin {  
  13.         //Main function of the plugin  
  14.         public void Execute(IServiceProvider serviceProvider) {  
  15.             //Extract the tracing service for use in debugging sandboxed plug-ins.  
  16.             ITracingService tracingService = (ITracingService) serviceProvider.GetService(typeof(ITracingService));  
  17.             // Obtain the execution context from the service provider.  
  18.             Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)  
  19.             serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));  
  20.             IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof(IOrganizationServiceFactory));  
  21.             IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);  
  22.             // The Input Parameters collection contains all the data passed in the message request.  
  23.             if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) {  
  24.                 // Obtain the target entity from the input parameters.  
  25.                 Entity entity = (Entity) context.InputParameters["Target"];  
  26.                 //Create record in Contact Entity  
  27.                 Entity ContactDetails = new Entity("Contact");  
  28.                 // To assign the owner id of Contact Details  
  29.                 EntityReference owner = new EntityReference();  
  30.                 owner.Id = ((EntityReference)(entity.Attributes["primarycontactid"])).Id; // Taking id from current entity and assigning it to contact’s owner  
  31.                 owner.LogicalName = ((EntityReference)(entity.Attributes["primarycontactid"])).LogicalName; // Taking id from current entity and assigning it to contact’s owner  
  32.                 ContactDetails["ownerid"] = owner;  
  33.                 orgSvc.Create(ContactDetails);  
  34.             }  
  35.         }  
  36.     }  
  37. }   

Please refer to this link to register and debug the plugin.

Section 2

To update the owner field of the existing record, use the code given below. 

  1. // Create the Request Object and Set the Request Object's Properties  
  2. AssignRequest assignowner = new AssignRequest {  
  3.     Assignee = new EntityReference(SystemUser.EntityLogicalName, _otherUserId),  
  4.         Target = new EntityReference(Account.EntityLogicalName, _accountId)  
  5. };  
  6. // Execute the Request  
  7. orgService.execute(assignowner);   

Output

Account

Primary Contact: Dave Adam

Contact:

Owner: Dave Adam

It does not matter who creates the contact from the account but the plugin will set the primary contact as an owner.

Hope, this blog may be helpful to you.

Ebook Download
View all
Learn
View all