This project is read-only.

Create an Asynchronous Plugin Integration Test

In this example, we explain how to test the plugin "RunIdentityCheck". This asynchronous plugin assigns a score ("xdftidentityscore") to the corresponding contact after a Identity Check_ record has been created. So our test is going to verify that an score is generated and assigned to the same contact.

To create this test, just follow the next steps:
  • Right click on the folder Plugins of the Integration Test project
  • Add new Item
  • Dynamics CRM => "Async Plugin Integration Test"



RunIdentityCheck.cs code:
namespace Xrm.Solutions.FrameworkSample.Plugins
    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Xrm.Solutions.FrameworkSample.Common;
    using Xrm.Solutions.FrameworkSample.Common.Entities;

    /// <summary>
    /// Plugin to generate a score for the related contact.
    /// </summary>    
    public class RunIdentityCheck: Plugin
        public RunIdentityCheck()
            : base(typeof(RunIdentityCheck))
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", "xdft_identitycheck", new Action<LocalPluginContext>(ExecuteRunIdentityCheck)));

        protected void ExecuteRunIdentityCheck(LocalPluginContext localContext)
            Entity identityCheck = GetIdentityCheck(localContext);

            string firstname = identityCheck.Contains("xdft_firstname") ? identityCheck["xdft_firstname"].ToString() : null;
            string lastname = identityCheck.Contains("xdft_lastname") ? identityCheck["xdft_lastname"].ToString() : null;
            DateTime dob = identityCheck.Contains("xdft_dateofbirth") ? (DateTime)identityCheck["xdft_dateofbirth"] : DateTime.MaxValue;
            string address = identityCheck.Contains("xdft_address") ? identityCheck["xdft_address"].ToString() : null;

            IdentityCheckService service = new IdentityCheckService();
            int score = service.RunCheck(firstname,

            localContext.TracingService.Trace("Score has been generated. Value: {0}", score);

            Entity updatedContact = new Entity("contact");
            updatedContact.Id = ((EntityReference)identityCheck["xdft_contactid"]).Id;
            updatedContact["xdft_identityscore"] = score;


        private Entity GetIdentityCheck(LocalPluginContext localContext)
            if (localContext == null)
                throw new ArgumentNullException("localContext");
            if (!localContext.PluginExecutionContext.InputParameters.Contains("Target"))
                throw new ArgumentNullException("Target");

            Entity identityCheckEntity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
                throw new ArgumentNullException("Contact", "The current Identity Check record doesn't have an associated contact");
            localContext.TracingService.Trace("Identity Check is ready to be used");

            return identityCheckEntity;

TestRunIdentityCheck.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Xrm.Framework.Test.Integration;
using Xrm.Solutions.FrameworkSample.Common.Entities;

namespace Xrm.Solutions.FrameworkSample.XrmIntegrationTests.Plugins
    public class TestRunIdentityCheck : AsyncPluginIntegrationTest
        #region Instance Variables

        private Contact contact;


        #region Setup

        protected override OrganizationRequest SetupTriggerRequest()
            contact = CreateTestContact();
            xdft_identitycheck identityCheck = new xdft_identitycheck();
            identityCheck.xdft_FirstName = contact.FirstName;
            identityCheck.xdft_LastName = contact.LastName;
            identityCheck.xdft_DateOfBirth = contact.BirthDate;
            identityCheck.xdft_Address = contact.Address1_Line1 + " " + contact.Address1_PostalCode;
            identityCheck.xdft_ContactId = contact.ToEntityReference();

            CreateRequest createRequest = new CreateRequest();
            createRequest.Target = identityCheck;

            return createRequest;

        private Contact CreateTestContact()
            Contact contact = new Contact();

            contact.FirstName = DateTime.Now.ToString();
            contact.LastName = "Test";
            contact.BirthDate = DateTime.Now;
            contact.Address1_Line1 = "Soho ";
            contact.Address1_PostalCode = "CW8 0ES";
            contact.Id = this.OrganizationService.Create(contact);

            return contact;


        #region Test

        public void RunTestRunIdentityCheck()


        #region Verify

        protected override void VerifyStatus()

            Assert.AreEqual(Status, AsyncStatus.Pass);

            Contact updatedContact = this.OrganizationService.Retrieve("contact", contact.Id, new ColumnSet(new string[]{ "xdft_identityscore" })).ToEntity<Contact>();

            Assert.IsTrue(updatedContact.xdft_IdentityScore > 0 && updatedContact.xdft_IdentityScore <= 100);


        #region Clean Up

        protected override void CleanUp()

            Guid identityCheckID = (Guid) this.TriggerResponse.Results["id"];

            this.OrganizationService.Delete("xdft_identitycheck", identityCheckID);

            this.OrganizationService.Delete("contact", contact.Id);


In this test, you should notice the property AsyncOperationName hasn't been overwritten, as we did in the example Create an Asynchronous Workflow Integration Test. When this happens, the default System Job name to be used will be the name of the test class removing the string "Test". So, if the name of the test is TestRunIdentityCheck, the system job to be checked is RunIdentityCheck, which matches the name of the plugin and how it will be listed in the System Jobs list as a System Event operation type.

Last edited Jun 13, 2014 at 11:11 AM by rtebar, version 4


No comments yet.