locked
I wan create a Custom Workflow by using C# RRS feed

  • Question

  • hi, I wan create a Custom Workflow by using C#, for send email to all Opportunity, but in the mail only will show 1 receiver.
    I found a code, but its was...send to user/team. are Any one know how can I change it to send to Opportunity please..!!

    Please Help..

    namespace Workflow
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using Microsoft.Xrm.Sdk;
        using Microsoft.Xrm.Sdk.Messages;
        using Microsoft.Xrm.Sdk.Workflow;
        using Microsoft.Xrm.Sdk.Query;
        using System.Activities;
        using System.Web.Services.Protocols;
        using Microsoft.Crm.Sdk.Messages;
        using System.Diagnostics;
    
        public class CustomWorkflow : CodeActivity
        {
            [Input("Team Name")]
            [Default("Case Team")]
            public InArgument<string> CaseTeam { get; set; }
    
            [Output("Date time")]
            public OutArgument<DateTime> TaskDueDate { get; set; }
    
            protected override void Execute(CodeActivityContext executionContext)
            {
                try
                {
                    // Create the tracing service
                    ITracingService tracingService = executionContext.GetExtension<ITracingService>();
    
                    // Create the context
                    IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
    
                    IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
    
                    // Create the Organiztion service
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                    // Get the target entity from the context
                    Entity target = (Entity)context.InputParameters["Target"];
    
                    // Prepare DataContext by using AutoGenerated cs file
                    XrmDataContext datacontext = new XrmDataContext(service);
    
                    // Get the Team Name from the Workflow input parameter
                    string teamName = CaseTeam.Get<string>(executionContext);
    
                    // Get the Team Id from the Team Name
                    var team = (from t in datacontext.TeamSet
                                where t.Name == teamName
                                select new { t.TeamId }).First();
    
                    // Get all the members of the team to send email
                    List<TeamMembership> teamMembers = (from t in datacontext.TeamMembershipSet
                                                        where t.TeamId == team.TeamId
                                                        select t).ToList();
    
                    // Send email to memebers of the team
                    CreateEmail(service, teamMembers, target, context.InitiatingUserId);
    
                    // Set the Task DueDate to 2 days by using Output Parameter
                    TaskDueDate.Set(executionContext, DateTime.Now.AddDays(2));
    
                }
                catch (SoapException ex)
                {
                    // Add the SoapException message in event log
                    EventLog.WriteEntry("code error", "Error occured in " + ex.Detail.InnerText.ToString(), EventLogEntryType.Error);
                }
                catch (Exception exe)
                {
                    // Add the GeneralException message in event log
                    EventLog.WriteEntry("code error", "Error occured in " + exe.InnerException.ToString(), EventLogEntryType.Error);
                }
            }
    
            public static void CreateEmail(IOrganizationService service, List<TeamMembership> teamMembers, Entity caseEntity, Guid loggedinUser)
            {
                // Create Email 
                Email email = new Email();
    
                // Set the from user of the email to Logged in user
                ActivityParty fromParty = new ActivityParty();
                fromParty.PartyId = new EntityReference(SystemUser.EntityLogicalName, loggedinUser);
    
                List<ActivityParty> toPartyList = new List<ActivityParty>();
    
                // Add the Activity party for each member in the team
                foreach (TeamMembership user in teamMembers)
                {
                    ActivityParty toParty = new ActivityParty();
                    toParty.PartyId = new EntityReference(SystemUser.EntityLogicalName, user.SystemUserId.Value);
                    toPartyList.Add(toParty);
                }
    
                // Add the subject and description by concating the case ticket number
                email.Subject = "Case " + caseEntity.Attributes["ticketnumber"].ToString() + " Added. Please review";
                email.Description = "Case " + caseEntity.Attributes["ticketnumber"].ToString() + " Added. Please review";
    
                // Add To and From users to email
                email.To = toPartyList.ToArray();
                email.From = new[] { fromParty };
    
                // Set the case regarding
                email.RegardingObjectId = new EntityReference(Incident.EntityLogicalName, caseEntity.Id);
    
                // Create email
                Guid emailId = service.Create(email);
    
                SendEmailRequest sendRequest = new SendEmailRequest();
                sendRequest.EmailId = emailId;
                sendRequest.TrackingToken = string.Empty;
                sendRequest.IssueSend = true;
    
                // Send the email message.
                service.Execute(sendRequest);
            }
        }
    }
    

    Please help me...

    Thank you!!
    Wednesday, July 17, 2013 2:45 AM

All replies

  • I don't understand your requirement. You want to send an e-mail to the Customer listed on an Opportunity record via a workflow?
    Monday, July 22, 2013 1:48 AM