Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

An example to how to create an CRM 2011 email in a plug-in

$
0
0

I need to create a plugin that creates an email. I have a stored proc that gets all of the information for the email I then have a SQL reader that reads that that stored proc. I have not built or created an email in CRM 2011 and so I am uncertian on how to do so. The code for the plugin / email is below:

Code:

namespace AutoSubmitOrderEmail
{
    public class SendEmail : CodeActivity
    {
        #region Globals
        private string GTRINumber =  string.Empty;
        private string OrderIdName = string.Empty;
        private string GPOrderNumber = string.Empty;
        private string QuoteNumber = string.Empty;
        private string CustomerIdName = string.Empty;
        //private string AEId = string.Empty;
        //private string CSEId = string.Empty;
        //private string IAMId = string.Empty;
        private string CustomerPrimaryContact = string.Empty;
        DateTime EstShipDate = new DateTime();
        private string TrackingNumbers = string.Empty;
        private string TrackingType = string.Empty;
        private string MaintenanceNumber = string.Empty;
        private string PurchaseOrderId = string.Empty;
        private string EmailAddress1 = string.Empty;
        private string EmailAddress2 = string.Empty;
        private string SerialNumber = string.Empty;
        private string ConfirmationContact1 = string.Empty;
        private string ConfirmationContact2 = string.Empty;
        private string ConfirmationContact3 = string.Empty;
        private string emailBody = string.Empty;
        private string emailSubject = string.Empty;
        #endregion

        #region Connections
          <SQL Connection info here>
        #endregion

        #region CRM Webservice Settings
           <CRM web service here>
        #endregion

        protected override void Execute(CodeActivityContext executionContext)
        {
            try
            {
                //Create the IWorkflowContext and the IOrganizationService for communication with CRM
                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                //Get the quote id of the quote entity
                EntityReference quote = myQuote.Get<EntityReference>(executionContext);
                EntityReference GTRI_purchaseorder = purchaseOrderReference.Get<EntityReference>(executionContext);

                Guid QuoteId = new Guid();
                QuoteId = myQuote.Get(executionContext).Id;

                Guid OrderId = new Guid();
                OrderId = purchaseOrderReference.Get(executionContext).Id;

                Guid AEId = new Guid();
                AEId = myQuote.Get(executionContext).Id;

                Guid CSEId = new Guid();
                CSEId = myQuote.Get(executionContext).Id;

                Guid IAMId = new Guid();
                IAMId = myQuote.Get(executionContext).Id;

                try
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        try
                        {
                            using (SqlConnection oConn = new SqlConnection(connectionString))
                            {
                                cmd.Connection = oConn;
                                cmd.CommandType = CommandType.StoredProcedure;
                                cmd.CommandText = "SubmitOrder.Send_Automatic_Email_First";
                                cmd.Parameters.AddWithValue("@GTRI_OrderId", OrderId);
                                oConn.Open();

                                using (SqlDataReader reader = cmd.ExecuteReader())
                                {
                                    if (reader != null)
                                    {
                                        int intCounter = 0;
                                        while (reader.Read())
                                        {
                                            if (intCounter == 0)
                                            {

                                                if (reader["GTRI_name"].ToString() == null)
                                                {
                                                    GTRINumber = "";
                                                }
                                                else
                                                {
                                                    GTRINumber = reader["GTRI_name"].ToString();
                                                }

                                                if (reader["GTRI_OrderIdName"].ToString() == null)
                                                {
                                                    OrderIdName = "";
                                                }
                                                else
                                                {
                                                    OrderIdName = reader["GTRI_OrderIdName"].ToString();
                                                }

                                                if (reader["GTRI_GPOrderNumber"].ToString() == null)
                                                {
                                                    GPOrderNumber = "";
                                                }
                                                else
                                                {
                                                    GPOrderNumber = reader["GTRI_GPOrderNumber"].ToString();
                                                }

                                                if (reader["QuoteNumber"].ToString() == null)
                                                {
                                                    QuoteNumber = "";
                                                }
                                                else
                                                {
                                                    QuoteNumber = reader["QuoteNumber"].ToString();
                                                }

                                                if (reader["CustomerIdName"].ToString() == null)
                                                {
                                                    CustomerIdName = "";
                                                }
                                                else
                                                {
                                                    CustomerIdName = reader["CustomerIdName"].ToString();
                                                }

                                                if (reader["GTRI_CustomerPrimaryContactId"].ToString() == null)
                                                {
                                                    CustomerPrimaryContact = "";
                                                }
                                                else
                                                {
                                                    CustomerPrimaryContact = reader["GTRI_CustomerPrimaryContactId"].ToString();
                                                }

                                                if (reader["Gtri_EstShipDate"].ToString() == null)
                                                {
                                                    EstShipDate = Convert.ToDateTime(reader[null].ToString());
                                                }
                                                else
                                                {
                                                    EstShipDate = Convert.ToDateTime(reader["Gtri_EstShipDate"].ToString());
                                                }

                                                if (reader["Gtri_SerialNumber"].ToString() == null)
                                                {
                                                    SerialNumber = "";
                                                }
                                                else
                                                {
                                                    SerialNumber = reader["Gtri_SerialNumber"].ToString();
                                                }

                                                if (reader["Gtri_TrackingNumbers"].ToString() == null)
                                                {
                                                    TrackingNumbers = "";
                                                }
                                                else
                                                {
                                                    TrackingNumbers = reader["Gtri_TrackingNumbers"].ToString();
                                                }

                                                if (reader["Value"].ToString() == null)
                                                {
                                                    TrackingType = "";
                                                }
                                                else
                                                {
                                                    TrackingType = reader["Value"].ToString();
                                                }

                                                if (reader["gtri_maintenancenumber"].ToString() == null)
                                                {
                                                    MaintenanceNumber = "";
                                                }
                                                else
                                                {
                                                    MaintenanceNumber = reader["gtri_maintenancenumber"].ToString();
                                                }

                                                if (reader["GTRI_purchaseorderid"].ToString() == null)
                                                {
                                                    PurchaseOrderId = "";
                                                }
                                                else
                                                {
                                                    PurchaseOrderId = reader["GTRI_purchaseorderid"].ToString();
                                                }

                                                if (reader["EmailAddress1"].ToString() == null)
                                                {
                                                    EmailAddress1 = "";
                                                }
                                                else
                                                {
                                                    EmailAddress1 = reader["EmailAddress1"].ToString();
                                                }

                                                if (reader["EmailAddress2"].ToString() == null)
                                                {
                                                    EmailAddress2 = "";
                                                }
                                                else
                                                {
                                                    EmailAddress1 = reader["EmailAddress2"].ToString();
                                                }

                                                if (reader["gtri_ConfirmationContact1"].ToString() == null)
                                                {
                                                    ConfirmationContact1 = "";
                                                }
                                                else
                                                {
                                                    ConfirmationContact1 = reader["gtri_ConfirmationContact1"].ToString();
                                                }

                                                if (reader["gtri_ConfirmationContact2"].ToString() == null)
                                                {
                                                    ConfirmationContact2 = "";
                                                }
                                                else
                                                {
                                                    ConfirmationContact2 = reader["gtri_ConfirmationContact2"].ToString();
                                                }

                                                if (reader["gtri_ConfirmationContact3"].ToString() == null)
                                                {
                                                    ConfirmationContact3 = "";
                                                }
                                                else
                                                {
                                                    ConfirmationContact3 = reader["gtri_ConfirmationContact3"].ToString();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }
                catch (Exception ex)
                {
                }

                emailSubject = emailSubject + "<br>Your parts have been Ordered.</br>";

                //Build From: email
                Entity fromParty = new Entity("activityparty");
                fromParty["partyid"] = new EntityReference("quote", IAMId);

                //Buid To: email
                Guid toPartyEmailAdddres1 = Guid.Empty;
                toPartyEmailAdddres1 = new Guid(EmailAddress1);

                Guid toPartyEmailAddress2 = Guid.Empty;
                toPartyEmailAddress2 = new Guid(EmailAddress2);

                Guid toPartyEmailConfirmationContact1 = Guid.Empty;
                toPartyEmailConfirmationContact1 = new Guid(ConfirmationContact1);

                Guid toPartyEmailConfirmationContact2 = Guid.Empty;
                toPartyEmailConfirmationContact2 = new Guid(ConfirmationContact2);

                Guid toPartyEmailConfirmationContact3 = Guid.Empty;
                toPartyEmailConfirmationContact3 = new Guid(ConfirmationContact3);

                Entity toParty = new Entity("activityparty");
                toParty["partyid1"] = new EntityReference("contact", toPartyEmailAdddres1);
                toParty["partyid2"] = new EntityReference("contact", toPartyEmailAddress2);
                toParty["partyid3"] = new EntityReference("quote", toPartyEmailConfirmationContact1);
                toParty["partyid4"] = new EntityReference("quote", toPartyEmailConfirmationContact2);
                toParty["partyid5"] = new EntityReference("quote", toPartyEmailConfirmationContact3);

                //Build CC: email
                Entity ccParty = new Entity("activityparty");
                ccParty["partyid1"] = new EntityReference("quote", AEId);
                ccParty["partyid2"] = new EntityReference("quote", CSEId);

                //Build body: email
                
                //Build Email: email
                Entity Email = new Entity("email");
                Email.Attributes["from"] = new Entity[] { fromParty };
                Email.Attributes["to"] = new Entity[] { toParty };
                Email.Attributes["cc"] = new Entity[] { ccParty };
                Email.Attributes["subject"] = "Your parts have been ordered.";
                Email.Attributes["regardingobjectid"] = new EntityReference("quote", QuoteId);
                Email.Attributes["description"] = emailSubject;
                Email.Attributes["ownerid"] = new EntityReference("quote", IAMId);
                Guid emailID = service.Create(Email);

                //Send email
                SendEmailRequest reqSendEmail = new SendEmailRequest();
                reqSendEmail.EmailId = emailID; //ID of created mail
                reqSendEmail.TrackingToken = "";
                reqSendEmail.IssueSend = true;

                SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);
            }
            catch(Exception ex)
            {
            }
        }
        #region CRMInput
        // Initialize the input parameter
        [Input("Purchase Order")]
        [ReferenceTarget("GTRI_purchaseorder")]
        public InArgument<EntityReference> purchaseOrderReference { get; set; }

        [Input("Quote")]
        [ReferenceTarget("quote")]
        public InArgument<EntityReference> myQuote { get; set; }
        #endregion
    }
}

Please help! Time is of the issue!

Thank you in advance!


newjeep


Viewing all articles
Browse latest Browse all 8223

Trending Articles



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