Workflow 如何以编程方式在CRM 2011中添加Memebres后更新营销列表

Workflow 如何以编程方式在CRM 2011中添加Memebres后更新营销列表,workflow,dynamics-crm-2011,dynamics-crm,Workflow,Dynamics Crm 2011,Dynamics Crm,我创建了一个将成员插入营销列表的工作流。但是当这个过程完成时,我注意到marketing list members计数器总是=N-1 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Workflow; usin

我创建了一个将成员插入营销列表的工作流。但是当这个过程完成时,我注意到marketing list members计数器总是=N-1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;

using System.IO;

namespace ContactToMarketingList
{
    public class ContactToMList : CodeActivity
    {
        [RequiredArgument]
        [Input("Contatto")]
        [ReferenceTarget("contact")]
        public InArgument<EntityReference> Contact { get; set; }

        [RequiredArgument]
        [Input("Marketing List")]
        [ReferenceTarget("list")]
        public InArgument<EntityReference> MList { get; set; }

        [RequiredArgument]
        [Input("Inserimento")]
        public InArgument<bool> Inserimento { get; set; }
        private static string _separatore = "\r\n";
        private static Log_Entity log = null; 


        protected override void Execute(CodeActivityContext executionContext)
        {
            //Entity myList = new Entity();
            //myList.LogicalName = "list"; 
            string _logs = string.Empty; 
            //string fileName = "c:\\temp\\" + DateTime.Now.ToString("HH:mm:ss.ffff");
            //if (!File.Exists(fileName))
            //    File.WriteAllText(fileName, string.Empty); 

            IExecutionContext context = executionContext.GetExtension<IExecutionContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            ITracingService tracer = executionContext.GetExtension<ITracingService>(); 

            if(log== null)
            log = new Log_Entity(service); 

            try
            {
                EntityReference contactiId = Contact.Get<EntityReference>(executionContext);
                log.addLogs(ref _logs, "ContactiId:::" + contactiId.Id.ToString()); 
                EntityReference listId = MList.Get<EntityReference>(executionContext);
                log.addLogs(ref _logs, "ListId:::" + listId.Id.ToString());
                Boolean inserimento = Inserimento.Get(executionContext);
                log.addLogs(ref _logs, "Boolean:::" + inserimento.ToString().ToUpper());
                XrmDataContext datacontext = new XrmDataContext(service);


                var members = (from m in datacontext.ListMemberSet where m.ListId.Id == MList.Get<EntityReference>(executionContext).Id select m.EntityId.Id).ToArray();

                Boolean _action = false;
                foreach (Guid id in members)
                    if (Contact.Get<EntityReference>(executionContext).Id == id)
                        _action = true;

                log.addLogs(ref _logs, "L'utente è già nella lista?" + _action.ToString().ToUpper()); 

                if (Inserimento.Get(executionContext) && !_action)
                {
                    log.addLogs(ref _logs, "Add to marketing List - Inizio"); 
                   // File.AppendAllText(fileName, "Inserimento.Get(executionContext) && !_action"); 
                    AddMemberListRequest AddMemberRequest = new AddMemberListRequest();

                    AddMemberRequest.ListId = MList.Get<EntityReference>(executionContext).Id;
                    AddMemberRequest.EntityId = Contact.Get<EntityReference>(executionContext).Id;
                    AddMemberListResponse AddMemberResponse = service.Execute(AddMemberRequest) as AddMemberListResponse;
                    log.addLogs(ref _logs, "Add to marketing List - Fine"); 

                }
                else if (!Inserimento.Get(executionContext) && _action)
                {
                    log.addLogs(ref _logs, "Remove from  marketing List - Inizio"); 

                    RemoveMemberListRequest RemoveMemberRequest = new RemoveMemberListRequest();
                    RemoveMemberRequest.ListId = MList.Get<EntityReference>(executionContext).Id;
                    RemoveMemberRequest.EntityId = Contact.Get<EntityReference>(executionContext).Id;                   
                    RemoveMemberListResponse RemoveMemberResponse = service.Execute(RemoveMemberRequest) as RemoveMemberListResponse;
                    log.addLogs(ref _logs, "Remove from  marketing List - Fine"); 

                }
                else
                {
                    log.addLogs(ref _logs, Inserimento.Get(executionContext) == true ? "L'utente è già presente nella Lista di Marketing." + _separatore : "L'utente non è presente nella Lista di Marketing." + _separatore); 
                }

                log.WriteLog( _logs);
                var MyList = (from l in datacontext.ListSet where l.Id == listId.Id select l).ToList().FirstOrDefault();
                service.Update(MyList); 

            }
            catch(Exception ex)
            {
                if (log == null)               
                    new Log_Entity(service).WriteLog( _logs, ex.Message); 
                else                
                    log.WriteLog( _logs, ex.Message);               
            } 

        }

    }
}
在我的日志中,每次调用Service.Update时,我都会发现以下消息:

EntityState必须设置为null、为创建消息创建或为更新消息更改


如果你觉得合适的话,在post操作或者甚至异步时注册插件