1

私のワークフローでは、マーケティング リストから連絡先を挿入または削除します。入力ブール値パラメーター値に従います。True-> 挿入 -> 連絡先が存在する場合 THEN return -> そうでなければ False -> 削除 -> 存在しない場合 THEN return

この WFA はインシデントで実行されます。

最初に起動すると、うまく機能し、連絡先を削除または追加します。しかし、ブール値を変更して再実行すると、次の例外メッセージが表示されます。

エラーが原因でワークフローが一時停止しました: 未処理の例外: System.NullReferenceException: オブジェクト参照がオブジェクトのインスタンスに設定されていません。ContactToMList.ContactToMList.Execute (CodeActivityContext executionContext) で System.Activities.CodeActivity.InternalExecute (ActivityInstance インスタンス、ActivityExecutor エグゼキューター、BookmarkManager ブックマーク マネージャー) で System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody (ActivityExecutor)

executor、BookmarkManager、bookmarkManager、ロケーション、resultLocation)

コンテキストを取らないようです。

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 = "\r\n"; 
        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);

            }

        }
    }
}
4

1 に答える 1

1

これは、Org Service Proxy と対話するために使用するコードです。UI を介さずにプラグインをトリガーするのに役立ちます。また、プラグインの開発中に破損したデータをクリーンアップするのにも役立ちます。

独自のドメイン、ユーザー名、パスワード、および組織名を入力する必要があります。コメントアウトされたコードは、一連の関連エンティティを設定するために使用していたものです。これについてさらにサポートが必要な場合はお知らせください。

私が持っている場所ではTrainingContext、これを CRM SDK の CrmSvcUtil で生成したコンテキストに置き換えます。

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 = "George.Lucas@Force.net",
            //            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
}
于 2013-03-14T18:37:40.970 に答える