In the I have created a video if you want to watch me go through the process of creating the plugin. Before I start with this plugin tutorial, to understand it you will need to have gone through my previous tutorials I have three CRM videos and two blog posts
Setting up Developer Toolkit for Microsoft Dynamics CRM
blog – Setting up visual studio with the developer toolkit
video – CRM 2013 – Create a simple plugin in CRM 2013 using the CRM Development Toolkit
blog – CRM 2013 – simple update plugin
CRM 2013 – Simple Plugin – Redeploying, improving and updating
So I will assume you have got CRM development toolkit setup and ready to rock, so now we can just focus on creating a new plugin to run on the create of an account record In this plugin we are going to create a plugin which is triggered when a new account is created.
Here is a video going through the steps
below features the steps and the code
The plugin will fire after the new account is created so it will be a post plugin message.
The plugin will then create a follow up task with a due date in two weeks time.
Open up your Visual Studio project.
Go to the CRM Explorer
click on Account right click and choose Create Plug-in
For the Create Plugin Details, Account is already set but you want to
change Message to Create
Pipeline stage to Post-Operation
This will Create a file called PostAccountCreate.cs For this plugin I have split up the default plugin code and the code which does the creating of the task.
// <copyright file="PostAccountCreate.cs" company="Microsoft"> // Copyright (c) 2014 All Rights Reserved // </copyright> // <author>Microsoft</author> // <date>4/26/2014 10:42:26 PM</date> // <summary>Implements the PostAccountCreate Plugin.</summary> // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.1 // </auto-generated> namespace HoskCRMDev2013.Plugins { using System; using System.ServiceModel; using HoskCRMDev2013.Plugins.Plugin_Code; using Microsoft.Xrm.Sdk; /// <summary> /// PostAccountCreate Plugin. /// </summary> public class PostAccountCreate: Plugin { /// <summary> /// Initializes a new instance of the <see cref="PostAccountCreate"/> class. /// </summary> public PostAccountCreate() : base(typeof(PostAccountCreate)) { base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", "account", new Action<LocalPluginContext>(ExecutePostAccountCreate))); // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination. // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change. } /// <summary> /// Executes the plug-in. /// </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 CRM 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 void ExecutePostAccountCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } // TODO: Implement your custom Plug-in business logic. // Obtain the execution context from the service provider. IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; // The InputParameters 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 parmameters. Entity entity = (Entity)context.InputParameters["Target"]; if (entity.LogicalName == "account") { if (context.MessageName.ToUpper() == "CREATE") { AccountCreateFollowUp accountCreateFollowUp = new AccountCreateFollowUp(); accountCreateFollowUp.CreateAccountTask(service, entity); } } } } } }
The code above checks to see if the entity is of type account and if the message is a CREATE message. This is in case you had the same plugin being fired on lots of different account messages (e.g. create, update, status change). We aren’t really using it in this plugin but I thought it would be interesting to mention it.
The code which creates the task is called here
AccountCreateFollowUp accountCreateFollowUp = new AccountCreateFollowUp();
accountCreateFollowUp.CreateAccountTask(service, entity);
Below you can see the class which creates the followup task
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; namespace HoskCRMDev2013.Plugins.Plugin_Code { public class AccountCreateFollowUp { /// <summary> /// Code to create account task for new accounts /// </summary> /// <param name="service">crm service</param> /// <param name="accountEntity">entity of the newly created account</param> public void CreateAccountTask(IOrganizationService service, Entity accountEntity) { try { //create new task for account set in 2 weeks in the future Entity contactAccountTask = new Entity("task"); contactAccountTask["subject"] = "Check new account is happy"; contactAccountTask["description"] = "Make contact with new customer. See if they are happy with service and resolve any issues."; contactAccountTask["scheduledstart"] = DateTime.Now.AddDays(14); contactAccountTask["scheduledend"] = DateTime.Now.AddDays(14); EntityReference entRef = new EntityReference("account", accountEntity.Id); contactAccountTask["regardingobjectid"] = entRef; // Create the task and this should be linked to the new account record service.Create(contactAccountTask); } catch (FaultException ex) { throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex); } } } }
Now right click on CRMPackage
click build
Click Deploy
Getting an issue that account with that id does not exists.
LikeLike