using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.Crm;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Workflow.Activities;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
namespace cel_WorkflowActivities
{
public sealed class runRelatedWorkflow : CodeActivity
{
[RequiredArgument]
[Input("Workflow To Execute")]
[ReferenceTarget("workflow")]
public InArgument<EntityReference> workflowId { get; set; }
[RequiredArgument]
[Input("Related Entity Logical Name")]
public InArgument<string> entityName { get; set; }
[RequiredArgument]
[Input("Attribute Logical Name")]
public InArgument<string> attributeName { get; set; }
protected override void Execute(CodeActivityContext executionContext)
{
StringBuilder sb = new StringBuilder("starting; ");
try
{
Guid wFlowId = ((EntityReference)executionContext.GetValue(this.workflowId)).Id;
string eName = executionContext.GetValue(this.entityName);
string fName = executionContext.GetValue(this.attributeName);
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
QueryByAttribute q = new QueryByAttribute(eName);
q.Attributes.AddRange(fName, "statecode");
q.Values.AddRange(context.PrimaryEntityId.ToString(), "Active");
EntityCollection retrieved = service.RetrieveMultiple(q);
sb.Append(string.Format("Retrieved {0} records; ", retrieved.Entities.Count.ToString()));
foreach (Entity c in retrieved.Entities)
{
ExecuteWorkflowRequest exec = new ExecuteWorkflowRequest();
exec.EntityId = c.Id;
exec.WorkflowId = wFlowId;
sb.Append(string.Format("Executing workflow on record with id {0}; ", c.Id.ToString()));
service.Execute(exec);
}
sb.Append("done");
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("An error occurred running related workflows: " + ex.Message);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.Crm;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Workflow.Activities;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
namespace cel_WorkflowActivities
{
public sealed class runRelatedWorkflow : CodeActivity
{
[RequiredArgument]
[Input("Workflow To Execute")]
[ReferenceTarget("workflow")]
public InArgument<EntityReference> workflowId { get; set; }
[RequiredArgument]
[Input("Related Entity Logical Name")]
public InArgument<string> entityName { get; set; }
[RequiredArgument]
[Input("Attribute Logical Name")]
public InArgument<string> attributeName { get; set; }
protected override void Execute(CodeActivityContext executionContext)
{
StringBuilder sb = new StringBuilder("starting; ");
try
{
Guid wFlowId = ((EntityReference)executionContext.GetValue(this.workflowId)).Id;
string eName = executionContext.GetValue(this.entityName);
string fName = executionContext.GetValue(this.attributeName);
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
QueryByAttribute q = new QueryByAttribute(eName);
q.Attributes.AddRange(fName, "statecode");
q.Values.AddRange(context.PrimaryEntityId.ToString(), "Active");
EntityCollection retrieved = service.RetrieveMultiple(q);
sb.Append(string.Format("Retrieved {0} records; ", retrieved.Entities.Count.ToString()));
foreach (Entity c in retrieved.Entities)
{
ExecuteWorkflowRequest exec = new ExecuteWorkflowRequest();
exec.EntityId = c.Id;
exec.WorkflowId = wFlowId;
sb.Append(string.Format("Executing workflow on record with id {0}; ", c.Id.ToString()));
service.Execute(exec);
}
sb.Append("done");
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("An error occurred running related workflows: " + ex.Message);
}
}
}
}
No comments:
Post a Comment