2013-03-12 74 views
1

我正在嘗試構建將聯繫人添加到營銷列表的工作流程。將聯繫人添加到CRM2011上的營銷列表

一切似乎都很好,但是當代碼完成解僱後,我進入市場營銷名單 - >會員聯繫人不在列表中。

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

      } 

     } 
    } 
+0

這段代碼可以用更多的Linq語句進行改進......但是,它也看起來像是調試非常簡單。你有沒有看過代碼,看看'service.Execute('被調用了嗎?' – 2013-03-12 15:31:39

+0

'是否可以移除,但是不要添加任何賬號? – user1200566 2013-03-12 16:10:56

+0

如果你對回覆感到滿意,請檢查它是一個答案,否則,請告訴缺少的東西。 – 2013-03-12 19:04:51

回答

1

是不是在設置後刪除了AddMemberRequest.MemberIds的值?

編輯: 好吧,我想我這次發現了它。您的public InArgument<bool> inserimento { get; set; }可能是罪魁禍首。

在這種情況下,您的工作流程活動期望在調用此工作流程的上游定義該工作流程活動。對於插入和移除實例,它很可能是靜態設置並且永遠不會更改。如果這是真的,那麼對於插入案例來說,它實質上是硬編碼的,這使else if (!insert && action)評估爲移除True,並且if (insert && !action)評估爲插入的False。

由於代碼確實適用於刪除,因此假設bool action正在工作是合理的;因此,我會先看看另一個bool變量。

讓我知道我是否錯過了它。 (或者如果我是對的,我不會介意綠色的複選標記。)

+0

不!對不起,我的錯誤複製/粘貼!這是好的代碼。:) – user1200566 2013-03-12 15:24:30