Random Musings

May 30, 2009

TDD on Dynamics CRM Workflow

Filed under: Dynamics CRM — haditeo @ 8:23 pm
Tags: , ,

In the Dynamics CRM 4 custom workflow activity, we can create an instance of CRMService by instantiating a IWorkflowContext object

IWorkflowContext context = ((IContextService)executionContext.GetService(typeof(IContextService))).Context;
ICrmService crmService = context.CreateCrmService();

How to unit test a Dynamics CRM 4 workflow ? since the CrmService object needs to be created with an interface type of ICrmService

Notice here carefully that we need to implement several methods, but in my scenario, i need to use the execute method.

    class MyCrmService :ICrmService
    {

        private CrmService crmService;

        public MyCrmService()
        {
            NetworkCredential nc = new NetworkCredential();
            nc.Domain = "Domain";
            nc.UserName = "administrator";
            nc.Password = "pwd";

            // Set up the CRM Service.
            CrmAuthenticationToken token = new CrmAuthenticationToken();
            token.AuthenticationType = 0;
            token.OrganizationName = "xyz";

            CrmService service = new CrmService();
            service.Url = "http://localhost/mscrmservices/2007/crmservice.asmx";
            service.CrmAuthenticationTokenValue = token;
            service.Credentials = nc;
            this.crmService = service;        
        }


        #region ICrmService Members

        public Guid Create(BusinessEntity entity)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void Delete(string entityName, Guid id)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public object Execute(object request)
        {
            return crmService.Execute((Request)request);
        }

        public string Fetch(string fetchXml)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public BusinessEntity Retrieve(string entityName, Guid id, ColumnSetBase columnSet)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public BusinessEntityCollection RetrieveMultiple(QueryBase query)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void Update(BusinessEntity entity)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    [TestFixture]
    public class Class1
    {
        private MyCrmService crmSvc;
        private MyMetadataService metadataSvc;
        private SendEmailActivity activity;

        [SetUp]
        public void Init()
        {
            this.crmSvc = new MyCrmService();

            this.metadataSvc = new MyMetadataService();

            this.activity = new SendEmailActivity();

        }
     }

The advantages of using the Interface, during the runtime, we can switch a method which requires ICrmService with the actual CrmService itself

May 29, 2009

TDD on DeliverIncomingEmailRequest

Filed under: Dynamics CRM — haditeo @ 7:06 pm
Tags: , ,

There is a scenario where developers would like to convert emails to the related entity let’s say incident.

To process the incoming emails, a plugin needs to be developed that listen to the DeliverIncomingEmail message. Imagine that developers need to setup a vpc which has a valid routable email address and internet connection etc.

Actually in the SDK, we can simulate this by triggering DeliverIncomingEmailRequest CRM service

Developers can use the below code

            DeliverIncomingEmailRequest req = new DeliverIncomingEmailRequest();
            req.From = "from@email.com";
            req.To = "ryan@sample.com";
            req.Subject = "subject" + DateTime.Now.ToLongDateString() + " - " + DateTime.Now.ToLongTimeString();
            req.MessageId = "messageId" + Guid.NewGuid().ToString();
            req.Body = "body" + Guid.NewGuid().ToString();
            req.Cc = "cc@email.com";
            req.Bcc = "bcc@email.com";
            req.ReceivedOn = new CrmDateTime("10/10/2000");
            req.SubmittedBy = "abc";
            req.Importance = "1";


            req.Attachments = new BusinessEntityCollection();
            req.Attachments.EntityName =
            EntityName.activitymimeattachment.ToString();
            
            DeliverIncomingEmailResponse res = (DeliverIncomingEmailResponse)crmService.Execute(req);

Pay attention that one of the recipient must be a valid CRM user with his/her valid email address. In this example, ryan@sample.com is a valid crm user.

This code can be run by using NUnit to make the testing easier.

Blog at WordPress.com.