1

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

一切似乎都很好,但是当代码完成触发时,我转到营销列表-> 成员,联系人不在列表中。

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);

            }

        }
    }
4

1 回答 1

1

你不是在设置后删除你的价值观AddMemberRequest.MemberIds吗?

编辑:好的,我想我这次找到了。你public InArgument<bool> inserimento { get; set; }很可能是罪魁祸首。

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

由于代码确实适用于 Remove,因此可以合理地假设它bool action正在运行;因此,我将从查看另一个 bool 变量开始。

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

于 2013-03-12T15:22:13.407 回答