, , ,

Hi All,

Yet another very commonly used plugin is creation of Case from an Incoming Email. This can be very easily done with the Workflows as well but when the conditions are complicated, we prefer the use of plugins.

In my scenario, the requirement was bit twisted. If the Incoming Email was from an Account holding an Active Contract then a Case was suppose to be automatically created along with the details of the Active Contract and its Line Item and assigned to the Project manager of that associated Contract.

To begin with I’ll create a new Class Library Project adding CRM and .Net References as explained in my previous post and sign it with a Key.

  • Firstly we need to fetch the ‘From’ field from the incoming Email so that we can get the associated Entity (Account\Contact\Lead) related to that mail-id.

  • The ‘From’ field in the Email is actually an ‘activityparty'(EntityCollection) with ‘partyid'(EntityReference) containing the reference to the related Entity with specific GUID to which the mail-id is actually associated.

In my scenario if the related entity is an account then I’ll proceed further with that particular Account Id.

  • Using that Account Id, I can now fetch the Active Contracts related to those Accounts using the Fetchxml in Service.RetrieveMultiple Functionality (IOrganizationService).

  • Once we have the associated Contracts, similarly we can have the Contract Line related to those Contracts.

Contract Lines can be New or Existing so we have to write the FetchXml using both the filter type ‘and’ & ‘or’ accordingly.

  • Since I have all the required fields now so proceed further with creating the Case.

The Final Code will look something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

namespace AssignEmailContract
    public class EmailContract:IPlugin
        IOrganizationService service;
        public void Execute(IServiceProvider serviceProvider)
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

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

                    if (entity.LogicalName == "email")
                        if (entity.Contains("directioncode"))
                            if (((Boolean)entity["directioncode"]) == false)
                                EntityCollection M = ((EntityCollection)entity.Attributes["from"]);
                                string subject = ((string)entity.Attributes["subject"]);
                                //string mails = ((string)entity.Attributes["sender"]);

                                if (M != null && M[0].LogicalName == "activityparty")
                                    EntityReference N = (EntityReference)M[0]["partyid"];

                                    if (N.LogicalName == "account")
                                        Guid accid = N.Id;
                                        Guid contrtid;
                                        Guid contrtdetailid;
                                        string accid1 = accid.ToString();

                                        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
                                        fetchXml += "<entity name='contract'>";
                                        fetchXml += "<attribute name='contractid' />";
                                        fetchXml += "<filter type='and'>";
                                        fetchXml += "<condition attribute='accountid' operator='eq' value='" + accid1 + "' />";
                                        fetchXml += "<condition attribute='statecode' operator='eq' value='Active' />";
                                        fetchXml += "</filter>";
                                        fetchXml += "</entity>";
                                        fetchXml += "</fetch>";

                                        var result = service.RetrieveMultiple(new FetchExpression(fetchXml));
                                        if (result.Entities.Count > 0)
                                            contrtid = ((Guid)result.Entities[0].Attributes["contractid"]);
                                            string contrtid1 = contrtid.ToString();

                                            var fetchXml1 = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
                                            fetchXml1 += "<entity name='contractdetail'>";
                                            fetchXml1 += "<attribute name='contractdetailid' />";
                                            fetchXml1 += "<filter type='and'>";
                                            fetchXml1 += "<condition attribute='contractid' operator='eq' value='" + contrtid1 + "' />";
                                            fetchXml1 += "<filter type='or'>";
                                            fetchXml1 += "<condition attribute='statecode' operator='eq' value='Existing' />";
                                            fetchXml1 += "<condition attribute='statecode' operator='eq' value='Renewed' />";
                                            fetchXml1 += "</filter>";
                                            fetchXml1 += "</filter>";
                                            fetchXml1 += "</entity>";
                                            fetchXml1 += "</fetch>";

                                            var result1 = service.RetrieveMultiple(new FetchExpression(fetchXml1));
                                            if (result1.Entities.Count > 0)
                                                contrtdetailid = ((Guid)result1.Entities[0].Attributes["contractdetailid"]);
                                                Entity contract5 = service.Retrieve("contract", contrtid, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
                                                Guid billcust = ((EntityReference)contract5.Attributes["billingcustomerid"]).Id;
                                                Guid projectmgrid = ((EntityReference)contract5.Attributes["new_projectmanagerid"]).Id;

                                                Entity incident = new Entity("incident");
                                                incident.Attributes["title"] = subject;
                                                incident.Attributes["customerid"] = new EntityReference("account", accid);
                                                incident.Attributes["ownerid"] = new EntityReference("systemuser", projectmgrid);
                                                incident.Attributes["contractid"] = new EntityReference("contract", contrtid);
                                                incident.Attributes["contractdetailid"] = new EntityReference("contractdetail", contrtdetailid);
                                                Guid newincidentid = service.Create(incident);








                catch (Exception ex)
                    throw new InvalidPluginExecutionException("An error occured in the Plugin.", ex);




Thanks !!!