Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

Incoming mail plugin

$
0
0

I am writing plugin for crm 2013 server. In this I am creating a case for incoming mail. It is running fine , except- I am unable to set regarding field of email(incoming) with new created case. I registered this plugin for pre-operation. Kindly find plugin code:

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;

using System.ServiceModel.Description;

namespace AssignEmailContract

{

   public class EmailContract : IPlugin

   {

       public void Execute(IServiceProvider serviceProvider)

       {

           ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

           IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

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

           {

               Entity entity = (Entity)context.InputParameters["Target"];

               if (entity.LogicalName != "email")

               {

                   return;

               }

               IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

               try

               {

                   if (entity.LogicalName == "email")

                   {

                       string subject = ((string)entity.Attributes["subject"]);

                       string sub = subject.Substring(0, 3);

                       if (entity.Contains("directioncode"))

                       {

                           if ((((Boolean)entity["directioncode"]) == false) && ((sub !="re:") || (sub !="fw:")))

                           {

                               tracingService.Trace("1");

                               EntityCollection M = ((EntityCollection)entity.Attributes["from"]);

                             //  string subject = ((string)entity.Attributes["subject"]);

                              // string syuser = "First name";

                               var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";

                               fetchXml += "<entity name='systemuser'>";

                               fetchXml += "<attribute name='systemuserid' />";

                               fetchXml += "<filter type='and'>";

                               fetchXml += "<condition attribute='firstname' operator='eq' value='First Name' />";

                               fetchXml += "</filter>";

                               fetchXml += "</entity>";

                               fetchXml += "</fetch>";

                               tracingService.Trace("2");

                               var result = service.RetrieveMultiple(new FetchExpression(fetchXml));

                               Guid userid = ((Guid)result.Entities[0].Attributes["systemuserid"]);

                               //string duacc = "Dummy account";

                               var fetchXml1 = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";

                               fetchXml1 += "<entity name='account'>";

                               fetchXml1 += "<attribute name='accountid' />";

                               fetchXml1 += "<filter type='and'>";

                               fetchXml1 += "<condition attribute='name' operator='eq' value='Dummy account' />";

                               //fetchXml += "<condition attribute='statecode' operator='eq' value='Active' />";

                               fetchXml1 += "</filter>";

                               fetchXml1 += "</entity>";

                               fetchXml1 += "</fetch>";

                               var result1 = service.RetrieveMultiple(new FetchExpression(fetchXml1));

                               Guid acc = ((Guid)result1.Entities[0].Attributes["accountid"]);

                               if (M != null && M[0].LogicalName == "activityparty")

                               {

                                   EntityReference N = (EntityReference)M[0]["partyid"];

                                   if (N.LogicalName == "account")

                                   {

                                       Guid accid = N.Id;

                                       //string accid1 = accid.ToString();

                                       Entity incident = new Entity("incident");

                                       incident.Attributes["title"] = subject;

                                       incident.Attributes["customerid"] = new EntityReference("account", accid);

                                       incident.Attributes["ownerid"] = new EntityReference("systemuser", userid);

                                       //incident.Attributes["contractid"] = new EntityReference("contract", contrtid);

                                       // incident.Attributes["contractdetailid"] = new EntityReference("contractdetail", contrtdetailid);

                                       Guid newincidentid = service.Create(incident);

                                       entity.Attributes["regardingobjectid"] = new EntityReference("incident", newincidentid);

                                       service.Update(entity);

                                   }

                                   if (N.LogicalName == "contact")

                                   {

                                       Guid accid = N.Id;

                                       // string accid1 = accid.ToString();

                                       Entity incident = new Entity("incident");

                                       incident.Attributes["title"] = subject;

                                       incident.Attributes["customerid"] = new EntityReference("contact", accid);

                                       incident.Attributes["ownerid"] = new EntityReference("systemuser", userid);

                                       //incident.Attributes["contractid"] = new EntityReference("contract", contrtid);

                                       // incident.Attributes["contractdetailid"] = new EntityReference("contractdetail", contrtdetailid);

                                       Guid newincidentid = service.Create(incident);

                                       entity.Attributes["regardingobjectid"] = new EntityReference("incident", newincidentid);

                                       service.Update(entity);

                                   }

                               }

                           }

                       }

                   }

               }

              catch (Exception ex)

               {

                   tracingService.Trace("MyPlugin: {0}", ex.ToString());

                   throw;

               }

           }

       }

   }

}

Kindly help me


Viewing all articles
Browse latest Browse all 79901

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>