首先我们需要确定windows workflow foundation 已经安装.

创建一个dynamics CRM workflow (四) - Development of Custom Workflows 随笔 第1张

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 

 

创建一个dynamics CRM workflow (四) - Development of Custom Workflows 随笔 第2张

 

创建之后先移除MyCustomWorkflows 里面的 Activity.xaml

从packages\Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool.9.0.2.12\tools 路径中添加以下两个reference

创建一个dynamics CRM workflow (四) - Development of Custom Workflows 随笔 第3张

 

复制下面的代码到我们新建的GetTaxWorkflow.cs

 

因为我们在CRM里面定义的custom entity是键值对的形式出现, 所以我们需要input值和output值.

我们有以下几个方式获取数据. 这里我们使用Query By Attribute

1. UsingRetrieve (必须获得GUID)

2. QueryExpression (可以实现复杂的逻辑)

3. Query By Attribute (简化的QueryExpression)

4. FatchXML

5. LINQ

    public class GetTaxWorkflow : CodeActivity
    {
        [Input("Key")]
        public InArgument<string> Key { get; set; }

        [Output("Tax")]
        public OutArgument<string> Tax { get; set; }

        protected override void Execute(CodeActivityContext executionContext)
        {
            //Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();

            //Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            var key = Key.Get(executionContext);

            // Get data from Configuraton Entity
            // Call organization web service

            QueryByAttribute query = new QueryByAttribute("contoso_configuration");
            query.ColumnSet = new ColumnSet(new string[] { "contoso_value" });
            query.AddAttributeValue("contoso_name", key);
            EntityCollection collection = service.RetrieveMultiple(query);

            if (collection.Entities.Count != 1)
            {
                tracingService.Trace("Something is wrong with configuration");

            }

            Entity config = collection.Entities.FirstOrDefault();

            tracingService.Trace(config.Attributes["contoso_value"].ToString());
            Tax.Set(executionContext, config.Attributes["contoso_value"].ToString());
        }
    }

 

记得注册这个assembly

创建一个dynamics CRM workflow (四) - Development of Custom Workflows 随笔 第4张

 

然后让我们build一下项目. 我们的workflow dll就会在debug中

创建一个dynamics CRM workflow (四) - Development of Custom Workflows 随笔 第5张

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄