将联系人添加到CRM 2011上的营销列表中

时间:2013-03-12 15:09:03

标签: dynamics-crm-2011 crm

我正在尝试构建一个将联系人添加到营销列表的工作流程。

一切似乎都很好,但是当代码完成解雇时,我会进入营销列表 - >成员联系人不在列表中。

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

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

        [Input("Inserimento")]
        public InArgument<bool> inserimento { get; set; }

        bool action = false;
        private static IOrganizationService myService = null;
        private static Log_Entity log = new Log_Entity(string.Empty, myService);
        protected override void Execute(CodeActivityContext executionContext)
        {
            try
            {
                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);
                myService = service;
                log.WriteLog("");
                // Get the target entity from the context
                Entity target = (Entity)context.InputParameters["Target"];


                Guid contactiId = contact.Get<EntityReference>(executionContext).Id;
                Guid ListId = MList.Get<EntityReference>(executionContext).Id;
                bool insert = inserimento.Get<bool>(executionContext);


                // Prepare DataContext by using AutoGenerated cs file
                XrmDataContext datacontext = new XrmDataContext(service);

                var MyContact = (from c in datacontext.ContactSet where c.ContactId == contactiId select c.Id).ToArray();
                var MyList = (from l in datacontext.ListSet where l.Id == ListId select l).ToList().FirstOrDefault();
                // tutti i membri della lista di marketing
                var members = (from m in datacontext.ListMemberSet where m.ListId.Id == MyList.ListId select m.EntityId.Id).ToArray();




                foreach (Guid id in members)
                    if (MyContact.FirstOrDefault() == id)
                        action = true;



                if (insert && !action)
                {

                    AddListMembersListRequest AddMemberRequest = new AddListMembersListRequest();

                    AddMemberRequest.ListId = ListId;
                    AddMemberRequest.MemberIds = MyContact;
                    // Use AddListMembersListReponse to get information about the request execution 

                    AddListMembersListResponse AddMemberResponse = service.Execute(AddMemberRequest) as AddListMembersListResponse;
                    //service.Update(MyList);

                }
                else if (!insert && action)
                {

                    RemoveMemberListRequest RemoveMemberRequest = new RemoveMemberListRequest();
                    RemoveMemberRequest.ListId = ListId;
                    RemoveMemberRequest.EntityId = MyContact.FirstOrDefault();
                    // Use AddListMembersListReponse to get information about the request execution 
                    RemoveMemberListResponse RemoveMemberResponse = service.Execute(RemoveMemberRequest) as RemoveMemberListResponse;
                   // service.Update(MyList);

                }

            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message);

            }

        }
    }

1 个答案:

答案 0 :(得分:1)

您是否在设置AddMemberRequest.MemberIds之后删除了public InArgument<bool> inserimento { get; set; }的值?

编辑: 好的,我想我这次发现了。你的else if (!insert && action)可能是罪魁祸首。

在这种情况下,您的Workflow活动期望在此Workflow调用的上游定义它。它很可能是静态设置的,并且从未针对Insert和Remove实例进行更改。如果这是真的,那么它基本上是针对Insert案例的硬编码,这使得if (insert && !action)评估为True表示删除,bool action评估为False表示插入。

由于代码适用于Remove,因此假设{{1}}正在运行是合理的;因此,我将首先研究另一个bool变量。

如果我错过了,请告诉我。 (或者,如果我是对的,我不介意绿色复选标记。)