Thursday, May 11, 2017

To set value for owner field in Dynamics CRM using Plugin


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

Creating a Plugin

The below code snippet shows how the owner field of the newly created record is being set. But one thing we need to know is this process can be done only during 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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Messages;

using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk.Query;

using System.Runtime.Serialization;



namespace projectname.fieldmappings

{

    public class FieldMappings : IPlugin

    {

       //Main function of the plugin

        public void Execute(IServiceProvider serviceProvider)

        { 

            //Extract the tracing service for use in debugging sandboxed plug-ins.

            ITracingService tracingService =

                (ITracingService)serviceProvider.GetService(typeof(ITracingService));



            // Obtain the execution context from the service provider.

            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)

                serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));



            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));



            IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);

           

            // The Input Parameters collection contains all the data passed in the message request.

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

            {

                 // Obtain the target entity from the input parameters.

                  Entity entity = (Entity)context.InputParameters["Target"];



                 //Create record in Contact Entity

                  Entity ContactDetails = new Entity("Contact");

                  // To assign the owner id of Contact Details

                  EntityReference owner = new EntityReference();

                  owner.Id = ((EntityReference)(entity.Attributes["primarycontactid"])).Id;// Taking id from current entity and assigning it to contact’s owner

                  owner.LogicalName = ((EntityReference)(entity.Attributes["primarycontactid"])).LogicalName; // Taking id from current entity and assigning it to contact’s owner

                  ContactDetails["ownerid"] = owner;



orgSvc.Create(ContactDetails);

            }

         }

      }

}



Please refer this link for registering and debugging the plugin.

Section: 2

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

// Create the Request Object and Set the Request Object's Properties

AssignRequest assignowner = new AssignRequest

    {

        Assignee = new EntityReference(SystemUser.EntityLogicalName,

            _otherUserId),

        Target = new EntityReference(Account.EntityLogicalName,

            _accountId)

    };





// Execute the Request

orgService.execute(assignowner);

Output:

Account

Primary Contact: Dave Adam

Contact:

Owner: Dave Adam

No matter who creates the contact from Account but the plugin will set the primary contact as owner.

 Hope this blog may be helpful for you, will come up with another blog soon.

No comments:

Post a Comment

Augmented Reality and Virtual Reality

Here is the quick overview of Augmented Reality and Virtual Reality, also have explained how it is being used today and how it can chan...