Answered by:
CRM 2011 - Custom Workflow activity to retrieve teamMembers as an Output parameter

Question
-
Hi All,
Could anybody please help me when a record is created custom workflow activity that takes TEAM an input parameter in the first step and passes an output parameter value as all TEAM MEMBERS of that TEAM to another step in the workflow ?
So that I can configure that output onto TO field in sending emails to all the team members.
I have tried to create the following workflow activity that should get the team members of a team:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Activities; using System.Web.Services.Protocols; using System.Diagnostics; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk.Query; using Microsoft.Crm.Sdk.Messages; using Xrm; namespace Custom.Workflow.Activities { public class GetListofTeamMembers : CodeActivity { [Input("To: Team")] [ReferenceTarget(Team.EntityLogicalName)] public InArgument<EntityReference> LookupToTeam { get; set; } [Output("To:TeamMembers")] public OutArgument<EntityCollection> TeamMembers { 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 = LookupToTeam.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(); } 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); } }}}
- Edited by Vamsi Duggineni Thursday, January 16, 2014 7:02 PM
Thursday, January 16, 2014 6:53 PM
Answers
-
You won't be able to make this work exactly as designed, as a custom workflow activity cannot return an EntityCollection - i.e. you won't be able to have an OutputParameter that contains more than one user.
I'd change the design so that the custom workflow activity additionally takes an Email as an InputParameter, and have the custom workflow activity code add the team members to the To property of the email. If you want the email to be sent automatically, you'll also need a custom workflow activty to send it, or send it from this custom workflow activty
You've also got an issue with your code. You seem to be trying to use the Target InputParameter - this is used in plugins, but doesn't apply to custom workflow activities. You need to define Input Parameters using the InputAttribute
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
- Marked as answer by Vamsi Duggineni Friday, January 17, 2014 12:20 PM
Friday, January 17, 2014 8:20 AMModerator
All replies
-
Hello,
Unfortunately you can't use partylist attribute as input/out parameter so I will suggest you to use plugin for this requirement instead of workflow. you can create relationship between team and your custom entity, and send email using workflow only, but you can write a pre create plugin which will check for regarding, if it is related to your custom entity and has team lookup field, you can add team members in To field to send email.
HTH
Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.Friday, January 17, 2014 8:14 AMModerator -
You won't be able to make this work exactly as designed, as a custom workflow activity cannot return an EntityCollection - i.e. you won't be able to have an OutputParameter that contains more than one user.
I'd change the design so that the custom workflow activity additionally takes an Email as an InputParameter, and have the custom workflow activity code add the team members to the To property of the email. If you want the email to be sent automatically, you'll also need a custom workflow activty to send it, or send it from this custom workflow activty
You've also got an issue with your code. You seem to be trying to use the Target InputParameter - this is used in plugins, but doesn't apply to custom workflow activities. You need to define Input Parameters using the InputAttribute
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
- Marked as answer by Vamsi Duggineni Friday, January 17, 2014 12:20 PM
Friday, January 17, 2014 8:20 AMModerator -
Thanks - I got that.Friday, January 17, 2014 12:20 PM