locked
Workflow Activity RRS feed

  • General discussion

  • Hi All,

    I wrote a custom workflow, which I am triggering on status and Processstage change. My requirement is when we add 1 or more users  to the sales team grid of opportunity entity , then whenever a status is changed from or stage is changing then a email should go all the users in the sales grid. please find the code and help me. The below code is sending email only to the last added user but it should send all the users added. please change the code which I wrote and achieve my goal.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Crm.Sdk.Messages;
    using System.Activities;
    using Microsoft.Xrm.Sdk.Workflow;

     

    namespace Email
    {
        public class EmailActivity: CodeActivity
        {
            protected override void Execute(CodeActivityContext context)
            {
                IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
                Entity entity = service.Retrieve(workflowContext.PrimaryEntityName, workflowContext.PrimaryEntityId, new ColumnSet(true));
                if (entity!=null)
                {
                    string text = String.Format("<a xxx</a>");
                    Guid owerid = entity.GetAttributeValue<EntityReference>("ownerid").Id;
                    string opportunityOwner = entity.GetAttributeValue<EntityReference>("ownerid").Name;
                    string status = entity.FormattedValues["statuscode"];

                    Guid CurrentUserid = workflowContext.UserId;
                    List<Guid> currentteamid = new List<Guid>();
                    currentteamid = getTeamDetails(service, CurrentUserid);


                    List<Guid> ConnectteamId1 = new List<Guid>();
                    ConnectteamId1 = GetConnectionDetails(service, entity.Id);
                    Guid opportunityOwnerId = entity.GetAttributeValue<EntityReference>("ownerid").Id;

                    for (int i = 0; i < ConnectteamId1.Count; i++)
                    {
                        if (ConnectteamId1[i] == currentteamid[0])
                        {
                            Entity email = new Entity("email");
                            Entity fromParty = new Entity("activityparty");
                            Entity toParty = new Entity("activityparty");
                            Entity ccParty = new Entity("activityparty");

                            Guid userId = getUserId(service, entity.Id);
                            toParty["partyid"] = new EntityReference("systemuser", userId);
                            fromParty["partyid"] = new EntityReference("systemuser", workflowContext.UserId);

                            ccParty["partyid"] = new EntityReference("systemuser", owerid);
                            email["from"] = new Entity[] { fromParty };
                            email["to"] = new Entity[] { toParty };
                            email["cc"] = new Entity[] { ccParty };
                            var Topic = getOpportunityName(service, entity.Id);
                            email["subject"] = Topic + " " + "Progressed";
                            email["description"] ="Dear Team," + "<br/><br/>" + Topic + "   " + "<b>has moved from “Process stage to Process stage” and status is </b>" + "  " + status + " " + text + "<br/> <br/> <br/> <br/> <br/> " + "Regards," + "<br/> <br/>" + opportunityOwner;
                            email["regardingobjectid"] = new EntityReference("opportunity", entity.Id);

     

                            Guid emailId = service.Create(email);
                            SendEmailRequest sendEmailreq = new SendEmailRequest
                            {
                                EmailId = emailId,
                                TrackingToken = "",
                                IssueSend = true
                            };
                            SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);

                        }

                    }

                }
            }

            public List<Guid> GetConnectionDetails(IOrganizationService service, Guid id)
            {
                string name1 = "";
                List<Guid> teamid1 = new List<Guid>();
                QueryExpression query = new QueryExpression("connection");
                query.ColumnSet.AddColumn("record2id");
                query.Criteria.AddCondition("record1id", ConditionOperator.Equal, id);
                EntityCollection result = service.RetrieveMultiple(query);
                foreach (var item in result.Entities)
                {
                    var name = item.GetAttributeValue<EntityReference>("record2id");
                    if (name.LogicalName == "systemuser")
                    {
                        name1 = name.Name;
                        var userid = name.Id;

                        teamid1 = getTeamDetails(service, userid);
                    }
                }
                return teamid1;
            }
            public List<Guid> getTeamDetails(IOrganizationService service, Guid id)
            {
                List<Guid> list = new List<Guid>();
                string name1 = "";
                Guid Teamid = Guid.Empty;
                QueryExpression query1 = new QueryExpression("team");
                query1.ColumnSet.AddColumn("name");
                query1.Criteria.FilterOperator = LogicalOperator.And;
                // query1.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, id);
                query1.AddLink("teammembership", "teamid", "teamid").AddLink("systemuser", "systemuserid", "systemuserid").LinkCriteria.AddCondition("systemuserid", ConditionOperator.Equal, id);
                EntityCollection collection = service.RetrieveMultiple(query1);
                foreach (var item1 in collection.Entities)
                {
                    name1 = (item1.Attributes["name"]).ToString();
                    list.Add(new Guid((item1.Id).ToString()));

                }
                return list;

            }
            public string getOpportunityName(IOrganizationService service, Guid id)
            {
                string Topic = "";
                QueryExpression exp = new QueryExpression("opportunity");
                exp.ColumnSet.AddColumn("name");
                exp.Criteria.AddCondition("opportunityid", ConditionOperator.Equal, id);
                EntityCollection collection = service.RetrieveMultiple(exp);
                foreach (var item in collection.Entities)
                {
                    Topic = item.Attributes["name"].ToString();

                }
                return Topic;


            }
            public Guid getUserId(IOrganizationService service, Guid id)
            {
                Guid userid = Guid.Empty;
                QueryExpression query = new QueryExpression("connection");
                query.ColumnSet.AddColumn("record2id");
                query.Criteria.AddCondition("record1id", ConditionOperator.Equal, id);
                EntityCollection result = service.RetrieveMultiple(query);
                foreach (var item in result.Entities)
                {
                    var name2 = item.GetAttributeValue<EntityReference>("record2id");
                    if (name2.LogicalName == "systemuser")
                    {

                        userid = name2.Id;

                    }

                }
                return userid;
                }
            }
        }

    please help , very urgent

     
    Sunday, August 17, 2014 9:45 AM