protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (localContext == null) { throw new ArgumentNullException(nameof(localContext)); } EntityReference emailToSend = EmailToSend.Get(context); EntityReference recipientBusinessUnit = RecipientBusinessUnit.Get(context); bool includeChildren = IncludeChildren.Get(context); bool sendEmail = SendEmail.Get(context); List <Entity> ccList = new List <Entity>(); Entity email = RetrieveEmail(localContext.OrganizationService, emailToSend.Id); if (email == null) { UsersAdded.Set(context, 0); return; } //Add any pre-defined recipients specified to the array foreach (Entity activityParty in email.GetAttributeValue <EntityCollection>("cc").Entities) { ccList.Add(activityParty); } EntityCollection buUsers = GetBuUsers(localContext.OrganizationService, recipientBusinessUnit.Id); ccList = ProcessUsers(buUsers, ccList); if (includeChildren) { ccList = DrillDownBu(localContext.OrganizationService, recipientBusinessUnit.Id, ccList); } //Update the email email["cc"] = ccList.ToArray(); localContext.OrganizationService.Update(email); //Send if (sendEmail) { SendEmailRequest request = new SendEmailRequest { EmailId = emailToSend.Id, TrackingToken = string.Empty, IssueSend = true }; localContext.OrganizationService.Execute(request); } UsersAdded.Set(context, ccList.Count); }
protected override void Execute(CodeActivityContext executionContext) { ITracingService tracer = executionContext.GetExtension <ITracingService>(); IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { EntityReference emailToSend = EmailToSend.Get(executionContext); EntityReference recipientBusinessUnit = RecipientBusinessUnit.Get(executionContext); bool includeChildren = IncludeChildren.Get(executionContext); bool sendEmail = SendEmail.Get(executionContext); List <Entity> ccList = new List <Entity>(); Entity email = RetrieveEmail(service, emailToSend.Id); if (email == null) { UsersAdded.Set(executionContext, 0); return; } //Add any pre-defined recipients specified to the array foreach (Entity activityParty in email.GetAttributeValue <EntityCollection>("cc").Entities) { ccList.Add(activityParty); } EntityCollection buUsers = GetBuUsers(service, recipientBusinessUnit.Id); ccList = ProcessUsers(buUsers, ccList); if (includeChildren) { ccList = DrillDownBu(service, recipientBusinessUnit.Id, ccList); } //Update the email email["cc"] = ccList.ToArray(); service.Update(email); //Send if (sendEmail) { SendEmailRequest request = new SendEmailRequest { EmailId = emailToSend.Id, TrackingToken = string.Empty, IssueSend = true }; service.Execute(request); } UsersAdded.Set(executionContext, ccList.Count); } catch (Exception ex) { tracer.Trace("Exception: {0}", ex.ToString()); } }