Email send Via Workflow/ Plugin in d365

 public class SendEmailList

        {
            public string ResourceName { get; set; }
            public string RACFName { get; set; }
            public string WONumber { get; set; }
            public DateTime? StartDate { get; set; }
            public DateTime? EndDate { get; set; }
            public string EmailId { get; set; }
            public string CreatedBy { get; set; }
            public string UserName { get; set; }
            public Guid Id { get; set; }

 

        }

 

public void SendEmail(List<SendEmailList> emailList, IOrganizationService service)
        {
            if (emailList != null && emailList.Count > 0)
            {
                foreach (var item in emailList.GroupBy(w => w.EmailId))
                {
                    var filter = emailList.Where(w => w.EmailId == item.Key).ToList();
                    string table = "<table><tr><td>Resource  </td><td>Facility  </td><td>Work Order</td><td>Start Date</td><td>End Date</td></tr>{0}</table>";
                    string body = string.Empty;
                    foreach (var loop in filter)
                    {
                        body += "<tr><td>" + loop.ResourceName + "</td><td>" + loop.RACFName + "</td><td>" + loop.WONumber + "</td><td>" + ConvertUTCTimeToUserLocal(loop.StartDate.Value, service) + "</td><td>" + ConvertUTCTimeToUserLocal(loop.EndDate.Value, service) + "</td></tr>";
                    }

 

                    string description = string.Format(table, body);

 

                    // We will send the email from this current user
                    Entity fromActivityParty = new Entity("activityparty");
                    Entity toActivityParty = new Entity("activityparty");
                    fromActivityParty["partyid"] = new EntityReference("queue", new Guid("ea08a814-91b1-ed11-83fe-00224815753b"));
                    toActivityParty["partyid"] = new EntityReference("systemuser", item.FirstOrDefault().Id);
                    Entity email = new Entity("email");
                    email["from"] = new Entity[] { fromActivityParty };
                    email["to"] = new Entity[] { toActivityParty };
                    //    email["regardingobjectid"] = new EntityReference("contact", contactId);
                    email["subject"] = "Remove Restricted RACF Booking";
                    email["description"] = TemplateReplaceValue(description, item.FirstOrDefault().UserName);
                    email["directioncode"] = true;
                    Guid emailId = service.Create(email);

 

                    // Use the SendEmail message to send an e-mail message.
                    SendEmailRequest sendEmailRequest = new SendEmailRequest
                    {
                        EmailId = emailId,
                        TrackingToken = "",
                        IssueSend = true
                    };
                    SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailRequest);
                }

 

            }

 

 

        }

 

Comments

Popular posts from this blog

Power Automate compose vs variable

Update record when checkbox checked using JavaScript and save d365

Power pages custom button add in gried