WorkFlow Context System.NullReferenceException
在我的工作流程中插入或删除营销列表中的联系人。根据输入的布尔参数值。
真-> 插入
-> 如果联系人存在
然后返回
-> 其他
错误 -> 删除
-> 如果不存在
然后返回
此 WFA 在事件上运行。
在第一次触发时,它运行良好,删除或添加联系人。但是我重新运行更改布尔值,我得到以下异常消息:
Workflow paused due to error: Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance
of an object. at
ContactToMList.ContactToMList.Execute(CodeActivityContext
executionContext) at
System.Activities.CodeActivity.InternalExecute(ActivityInstance
instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at
System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutorexecutor, BookmarkManager bookmarkManager, Location resultLocation)
似乎没有考虑上下文。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | 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 ContactToMList; namespace ContactToMList { 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 string _logs = string.Empty; private static IOrganizationService myService = null; private static string _separataore ="\ \ "; 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 // var target = context.InputParameters["EntityId"]; 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(); if (MyList.MemberType == 2) { if (MyList.Type.Value == false) { 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); } } else { tracingService.Trace("Stai cercando di inserire un contatto in una lista di Marketing che non è statica."); _logs +="Stai cercando di inserire un contatto in una lista di Marketing che non è statica." + _separataore; return; } } else { tracingService.Trace("Stai cercando di inserire un enittà diversa da un contatto."); _logs +="Stai cercando di inserire un enittà diversa da un contatto." + _separataore; return; } } catch (Exception ex) { log.WriteLog(ex.Message); } } } } |
这是我用来与我的组织服务代理交互的一些代码。我发现它有助于在不通过 UI 的情况下触发插件。它还有助于清理我在开发插件时损坏的数据。
您需要填写自己的域、用户名、密码和组织名称。注释掉的代码是我用来填充一系列相关实体的东西。如果您需要更多帮助,请告诉我。
在我有
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | CrmConnection connection = CrmConnection.Parse("Url=http://localhost/trainingorg1; Domain=; Username=; Password=;"); OrganizationService service = new OrganizationService(connection); string template ="http://localhost/main.aspx?etn={0}&pagetype=entityrecord&id=%7B{1}%7D"; using (TrainingContext svc = new TrainingContext(service)) { svc.MergeOption = Microsoft.Xrm.Sdk.Client.MergeOption.NoTracking; training_customer customer = svc.training_customerSet.FirstOrDefault(c => c.training_name =="George.Lucas"); customer.training_SocialSecurityNumber ="123-45-6789"; svc.Update(customer); #region interaction activity //try //{ // training_customer customer = new training_customer // { // EmailAddress ="[email protected]", // training_name ="George.Lucas", // training_UniqueCustomerIdentifier ="George.Lucas" // }; // customer.Id = svc.Create(customer); // training_interactiontypemaster typeMaster = new training_interactiontypemaster // { // training_Description ="Indicates an Interaction initiated because a Customer needed to update an Address", // training_InteractionTypeValue ="Address Update" // }; // typeMaster.Id = svc.Create(typeMaster); // training_interaction interaction = new training_interaction // { // training_name ="Command Prompt", // training_Customer = customer.ToEntityReference(), // training_Description = typeMaster.training_Description, // training_InteractionType = typeMaster.ToEntityReference() // }; // interaction.Id = svc.Create(interaction); // EntityReference reference = interaction.ToEntityReference(); // customer.training_Interaction = reference; // svc.Update(customer); // training_interactionactivity activity = new training_interactionactivity // { // training_ActivityName ="Customer Update", // training_Description ="Updating Customer:" + customer.training_name, // training_Interaction = reference, // training_Entity = string.Format(template, customer.LogicalName, customer.Id) // }; // activity[customer.LogicalName] = customer.ToEntityReference(); // svc.Create(activity); // training_address address = new training_address // { // training_City ="City", // training_Country ="Country", // training_County ="County", // training_Interaction = reference, // training_Street1 ="Street 1", // training_Street2 ="Street 2" // }; // address.Id = svc.Create(address); // training_interactionactivity activityTwo = new training_interactionactivity // { // training_Address = address.ToEntityReference(), // training_ActivityName ="Address Update", // training_Description ="Updating the Address for Customer:" + customer.training_name, // training_Interaction = reference, // training_Entity = string.Format(template, address.LogicalName, address.Id) // }; // //activityTwo[address.LogicalName] = new CrmEntityReference(training_address.EntityLogicalName, addressId); // svc.Create(activityTwo); //} //catch (Exception e) //{ // Console.WriteLine(e.ToString()); //} #endregion } |