Wednesday, 15 March 2017

ASSIGN TASK TO NEWLY CREATED CONTACT ON DYNAMICS 365 USING PLUGIN

Assign the task to the new contact, when a new contact is created in dynamic 365 using developer tool kit plugin.



// <copyright file="PostOperationcontactCreate.cs" company="">
// Copyright (c) 2017 All Rights Reserved
// </copyright>
// <author></author>
// <date>3/14/2017 5:02:56 PM</date>
// <summary>Implements the PostOperationcontactCreate Plugin.</summary>
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.1
// </auto-generated>

using System;
using System.ServiceModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace CrmPackage1.Plugins1
{

    /// <summary>
    /// PostOperationcontactCreate Plugin.
    /// </summary>    
    public class PostOperationcontactCreate : PluginBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PostOperationcontactCreate"/> class.
        /// </summary>
        /// <param name="unsecure">Contains public (unsecured) configuration information.</param>
        /// <param name="secure">Contains non-public (secured) configuration information. 
        /// When using Microsoft Dynamics 365 for Outlook with Offline Access, 
        /// the secure string is not passed to a plug-in that executes while the client is offline.</param>
        public PostOperationcontactCreate(string unsecure, string secure)
            : base(typeof(PostOperationcontactCreate))
        {

            // TODO: Implement your custom configuration handling.
        }


        /// <summary>
        /// Main entry point for he business logic that the plug-in is to execute.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics 365 caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new InvalidPluginExecutionException("localContext");
            }

            // TODO: Implement your custom Plug-in business logic. 
            if (localContext.PluginExecutionContext.InputParameters.Contains("Target") && localContext.PluginExecutionContext.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
                //</snippetFollowupPlugin2>

                // Verify that the target entity represents an account.
                // If not, this plug-in was not registered correctly.
                if (entity.LogicalName != "contact")
                    return;

                try
                {
                    // Create a task activity to follow up with the account customer in 7 days. 
                    Entity followup = new Entity("task");

                    followup["subject"] = "Send e-mail to the new customer.";
                    followup["description"] =
                        "Follow up with the customer. Check if there are any new issues that need resolution.";
                    followup["scheduledstart"] = DateTime.Now.AddDays(7);
                    followup["scheduledend"] = DateTime.Now.AddDays(7);
                    followup["category"] = localContext.PluginExecutionContext.PrimaryEntityName;

                    followup["regardingobjectid"] = new EntityReference("contact", entity.Id);




                    //<snippetFollowupPlugin4>
                    // Obtain the organization service reference.
                    //</snippetFollowupPlugin4>

                    // Create the task in Microsoft Dynamics CRM.
                    localContext.Trace("FollowupPlthugin: Creating the task activity.");
                    Guid taskid = localContext.OrganizationService.Create(followup);

                    // Assign the task created to #malla (another user when new account is created.)



                           AssignRequest assign = new AssignRequest
                        {
                            Assignee = new EntityReference("systemuser", localContext.PluginExecutionContext.UserId),
                            Target = new EntityReference("task", taskid)
                        };
                        localContext.OrganizationService.Execute(assign);
                 
                }

                //<snippetFollowupPlugin3>
                catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);
                }
            }
        }
    }
}




No comments:

Post a Comment

Note: only a member of this blog may post a comment.