Place a key word to search

Wednesday, August 1, 2018

How to send a mail on create of record stating that the record is created..

Hi Developers,

Here comes with a new scenario on sending a mail on create of record stating that the record is created.For example I'm  taking an example of on creation of contact mail should send by stating the record is created.the code snippet is as follows:


Explanation : As per sending mail we need to have a From address,To address,Subject ,Body
so first we need to get the from and to address and the we need to crete them accordingly with activity party then by using the system entity email we can send the mail to created record.
please make a note that the post operation used in plugin registration tool.
Hope you understood.

 As per the example
From - User
To - contact record





using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;

namespace Sendemail
{
    /// <summary>
    /// Base class for all plug-in classes.
    /// </summary> 
    public class PluginBase : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new InvalidPluginExecutionException("serviceProvider");
            }

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = (IOrganizationService)servicefactory.CreateOrganizationService(context.UserId);
            ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                if(entity.LogicalName == "contact")
                {
                    tracing.Trace("1");
                 

                    Guid contactid = context.PrimaryEntityId; // Current record
                    tracing.Trace(contactid.ToString());
                     Guid userid = context.UserId;//(Guid)contactrecord.Attributes["userid"];//from current record retrive the owner the name and id
                    tracing.Trace("userid ");                 
                    //#To Details and from details
                        // Create 'From' activity party for the email
                    Entity fromParty = new Entity("activityparty");
                    fromParty["partyid"] = new EntityReference("systemuser", userid);
                    tracing.Trace(fromParty["partyid"].ToString());
                    // Create 'To' activity party for the email
                    Entity toParty = new Entity("activityparty");
                    toParty["partyid"] = new EntityReference("contact", contactid);
                    tracing.Trace(toParty["partyid"].ToString());

                    //#Sending Mail
                    Entity Email = new Entity("email");
                    Email.Attributes["from"] = new Entity[] { fromParty };
                    tracing.Trace("from");
                    Email.Attributes["to"] = new Entity[] { toParty };
                    tracing.Trace("to");
                    Email.Attributes["subject"] = "New Opportunity Requirements";
                    tracing.Trace("subject");
                    Email.Attributes["description"] = "Hi contact is created";
                    tracing.Trace("description");
                      Guid EmailId = service.Create(Email);
                    tracing.Trace("created");
                 
                    SendEmailRequest req = new SendEmailRequest();
                    req.EmailId = EmailId;
                    req.IssueSend = true;
                    req.TrackingToken = "";
                    SendEmailResponse res = (SendEmailResponse)service.Execute(req);



                }
            }
         
        }
    }
}
             




No comments:

Post a Comment