0

私は CRM を初めて使用し、新しい AutoNumber プラグインを作成しました (実際には既存のプラグインを変更しました)。

このプラグインを CRM 側で動作させるのに問題があります。

プラグインを作成し、CREATE ステップを作成しました。IMAGE の作成と、これをどのように行う必要があるかについて混乱しています。

また、localContext.Trace を使用していますが、この情報を表示する場所がわかりません。

このプラグインを実装するために必要な正確な手順を理解するのを手伝ってくれる人はいますか? 何か間違ったことをしている場合に備えて、ここにコードを含めます。繰り返しますが、これは機能するプラグインであり、変更しただけです。以前の開発者が使用したパターンを踏襲してみました。

参考までに -- 展開プロセスを支援するために CRM Solution Manager ユーティリティを購入しましたが、まだうまくいきません。

お時間をいただきありがとうございます。

これがコードです..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IccPlugin.ProxyClasses;
using IccPlugin.ProxyClasses.ProxyClasses;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace IccPlugin {
    public class ProgramReportAutoNumber : PluginBase {
        private readonly string imageAlias = "ProgramReport";
        private new_programreport preImage { get; set; }
        private new_programreport postImage { get; set; }
        private new_programreport targetEntity { get; set; }

        private readonly string imageAlias_program = "Program";
        private new_program preImage_program { get; set; }
        private new_program postImage_program { get; set; }
        private new_program targetEntity_program { get; set; }

        public ProgramReportAutoNumber(string unsecure, string secure)
            : base(typeof(ProgramReportAutoNumber), unsecure, secure) {
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>((int)CrmPluginStepStage.PreOperation, "Create", "new_programreport", new Action<LocalPluginContext>(Execute)));
            //base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>((int)CrmPluginStepStage.PostOperation, "Update", "new_programreport", new Action<LocalPluginContext>(Execute)));
            //base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>((int)CrmPluginStepStage.PostOperation, "Delete", "new_programreport", new Action<LocalPluginContext>(Execute)));
        }

        protected void Execute(LocalPluginContext localContext) {
            if (localContext == null) {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;

            if (context.PreEntityImages.Contains(imageAlias) && (context.PreEntityImages[imageAlias] is Entity)) {
                preImage = new new_programreport((Entity)context.PreEntityImages[imageAlias]);
            }

            if (context.PostEntityImages.Contains(imageAlias) && (context.PostEntityImages[imageAlias] is Entity)) {
                postImage = new new_programreport((Entity)context.PostEntityImages[imageAlias]);
            }

            if (context.PreEntityImages.Contains(imageAlias_program) && (context.PreEntityImages[imageAlias_program] is Entity)) {
                preImage_program = new new_program((Entity)context.PreEntityImages[imageAlias_program]);
            }

            if (context.PostEntityImages.Contains(imageAlias_program) && (context.PostEntityImages[imageAlias_program] is Entity)) {
                postImage_program = new new_program((Entity)context.PostEntityImages[imageAlias_program]);
            }

            if (context.InputParameters.Contains("Target") && (context.InputParameters["Target"] is Entity)) {
                targetEntity = new new_programreport((Entity)context.InputParameters["Target"]);
            }

            switch (context.MessageName) {
                case "Create":
                    HandleCreate(localContext);
                    break;
                case "Update":
                    HandleUpdate(localContext);
                    break;
                case "Delete":
                    HandleDelete(localContext);
                    break;
                default:
                    throw new ArgumentException("Invalid Message Name: " + context.MessageName);
            }
        }

        private void HandleDelete(LocalPluginContext localContext) {

            localContext.Trace("START - IccPlugin.ProgramReport.AutoNumber.HandleDelete");

            try {
                if (preImage == null) {
                    throw new Exception("IccPlugin.ProgramReport.AutoNumber.HandleDelete: preImage is null, unable to process the delete message.");
                }

                // TODO: Add code here to implement delete message. 
            } catch (Exception ex) {
                localContext.Trace(String.Format("IccPlugin.ProgramReport.AutoNumber.HandleDelete: Exception while processing the delete message, Error Message: {0}", ex.Message), ex);
                throw ex;
            } finally {
                localContext.Trace("END - IccPlugin.ProgramReport.AutoNumber.HandleDelete");
            }
            return;
        }

        private void HandleUpdate(LocalPluginContext localContext) {

            localContext.Trace("START - IccPlugin.ProgramReport.AutoNumber.HandleUpdate");

            if (preImage == null) {
                string msg = "IccPlugin.ProgramReport.AutoNumber.HandleUpdate : The Update step is not registered correctly.  Unable to retrieve the pre-operation image using alias" + imageAlias;
                localContext.Trace(msg);
                throw new Exception(msg);
            }

            if (postImage == null) {
                string msg = "IccPlugin.ProgramReport.AutoNumber.HandleUpdate : The Update step is not registered correctly.  Unable to retrieve the post-operation image using alias" + imageAlias;
                localContext.Trace(msg);
                throw new Exception(msg);
            }

            if (preImage_program == null) {
                string msg = "IccPlugin.Program.AutoNumber.HandleUpdate : The Update step is not registered correctly.  Unable to retrieve the pre-operation image using alias" + imageAlias_program;
                localContext.Trace(msg);
                throw new Exception(msg);
            }

            if (postImage_program == null) {
                string msg = "IccPlugin.Program.AutoNumber.HandleUpdate : The Update step is not registered correctly.  Unable to retrieve the post-operation image using alias" + imageAlias_program;
                localContext.Trace(msg);
                throw new Exception(msg);
            }

            try {
                // TODO: Add code here to implement update message.
            } catch (Exception ex) {
                localContext.Trace(String.Format("IccPlugin.ProgramReport.AutoNumber.HandleUpdate: Exception while processing the update message, Error Message: {0}", ex.Message), ex);
                throw ex;
            } finally {
                localContext.Trace("END - IccPlugin.ProgramReport.AutoNumber.HandleUpdate");
            }
            return;
        }

        private void HandleCreate(LocalPluginContext localContext) {
            localContext.Trace("START - IccPlugin.ProgramReport.AutoNumber.HandleCreate");

            if (targetEntity == null) {
                string msg = "IccPlugin.ProgramReport.AutoNumber.HandleCreate : The Create step is not registered correctly.  Unable to retrieve the target entity using alias Target.";
                localContext.Trace(msg);
                throw new Exception(msg);
            }

            try {
                // if the target entity does not have the new_filenumber attribute set we will set it now.
                if (targetEntity.new_filenumber != null && targetEntity.new_filenumber != "") {
                    // log a warning message and do not change this value.
                    localContext.Trace("The Program Report being created already has a value for File Number, skipping the auto number assignment for this field.");
                } else {
                    SetFileNumber(localContext);
                }

                if (targetEntity.new_name != null && targetEntity.new_name != "") {
                    localContext.Trace("The Program Report being created already has a value for Report Number, skipping the auto number assignment for this field.");
                } else {
                    SetReportNumber(localContext);
                }
            } catch (Exception ex) {
                localContext.Trace(String.Format("IccPlugin.ProgramReport.AutoNumber.HandleCreate: Exception while processing the create message, Error Message: {0}", ex.Message), ex);
                throw ex;
            } finally {
                localContext.Trace("END - IccPlugin.ProgramReport.AutoNumber.HandleCreate");
            }
            return;
        }

        private void SetFileNumber(LocalPluginContext localContext) {
            localContext.Trace("START - IccPlugin.ProgramReport.AutoNumber.SetFileNumber");
            string s_new_filenumberformat = string.Empty;
            string s_new_reportnumberprefix = string.Empty;
            string s_new_filenumbercode = string.Empty;

            try {
                IOrganizationService service = localContext.OrganizationService;
                string fileNumberValue = "";
                emds_autonumbersequence fileNumberSequence = null;

                //  ##################################################################################################
                //  05/02/2013  --  BEP -- Code added for the following change to the auto-number for file numbering
                //  ##################################################################################################
                //      1 - Year/Month/Sequence
                //              [Year]-[Month]-[Sequence] = [Year] is the current year / [Month] is the current month / [Sequence] is a number series for each Year & Month and resets to 1 when the Month changes
                //      2 - Year/PMG/Sequence - PMG 
                //              [Year]-[PMGProductType][Sequence] = [Year] is the current year / [PMGProductType] is 1st letter from the PMG Product Type on the Program Report / [Sequence] is a single number series for this Format
                //      3 - Year/Letter/Sequence - ESL,VAR
                //              [Year]-[FileCode][Sequence] = [Year] is the current year / [FileCode] is from a new field on the Program entity / [Sequence] is a number series for each Format & File Code
                //  ##################################################################################################

                localContext.Trace("Look at the File Number Format to determine which format to use for the Auto-Number, will default to 1 if not set");

                if (targetEntity_program.new_filenumberformat.ToString() != "") {
                    localContext.Trace("A value was set for the new_filenumberformat field, so we will be using this value.");
                    s_new_filenumberformat = targetEntity_program.new_filenumberformat.ToString();
                } else {
                    localContext.Trace("A value was NOT set for the new_filenumberformat field, so we will be using 1 as the default.");
                    s_new_filenumberformat = "1";
                }

                localContext.Trace("File Number Format Being Used = " + s_new_filenumberformat);

                switch (s_new_filenumberformat) {
                    case "1":
                        #region File Format #1
                        fileNumberValue = String.Format("{0}-{1}", DateTime.Now.ToString("yy"), DateTime.Now.ToString("MM"));

                        localContext.Trace("Building QueryExpression to retrieve FileNumber Sequence record.");

                        QueryExpression qeFileNumberSequence_1 = new QueryExpression(BaseProxyClass.GetLogicalName<emds_autonumbersequence>());
                        qeFileNumberSequence_1.ColumnSet = new ColumnSet(true);
                        qeFileNumberSequence_1.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_entitylogicalname, ConditionOperator.Equal, BaseProxyClass.GetLogicalName<new_programreport>());
                        qeFileNumberSequence_1.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_attributelogicalname, ConditionOperator.Equal, new_programreport.Properties.new_filenumber);
                        qeFileNumberSequence_1.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_prefix, ConditionOperator.Equal, fileNumberValue);

                        localContext.Trace("Getting FileNumber sequence record.");

                        List<emds_autonumbersequence> lFileNumberSequences_1 = service.RetrieveProxies<emds_autonumbersequence>(qeFileNumberSequence_1);
                        if (lFileNumberSequences_1 == null || lFileNumberSequences_1.Count == 0) {
                            localContext.Trace("No FileNumber sequence record was returned, creatign a new one.");

                            // no matching sequence records.  Lets start a new sequence index record for this month and year.
                            fileNumberSequence = new emds_autonumbersequence();
                            fileNumberSequence.emds_attributelogicalname = new_programreport.Properties.new_filenumber;
                            fileNumberSequence.emds_entitylogicalname = BaseProxyClass.GetLogicalName<new_programreport>();
                            fileNumberSequence.emds_index = 1;
                            fileNumberSequence.emds_prefix = fileNumberValue;
                            fileNumberSequence.emds_name = String.Format("File Number Sequence For: {0}", fileNumberValue);
                            fileNumberSequence.Create(service);
                        } else {
                            localContext.Trace("A FileNumber sequence record was found, using it.");
                            // a file number sequence record was returned.  Even if there happen to be multiple we are going to just use the first one returned.
                            fileNumberSequence = lFileNumberSequences_1[0];
                        }
                        //  ###############################################################################
                        //  05/02/2013  --  BEP --  Changed the format from "###" to be "##" for seq number
                        //  ###############################################################################
                        fileNumberValue = String.Format("{0}-{1:00}", fileNumberValue, fileNumberSequence.emds_index);
                        fileNumberSequence.emds_index++;
                        fileNumberSequence.Update(service);
                        #endregion
                        break;
                    case "2":
                        #region File Format #2

                        if (targetEntity_program.new_reportnumberprefix != null && targetEntity_program.new_reportnumberprefix != "") {
                            localContext.Trace("A value was set for the new_reportnumberprefix field, so we will be using this value.");
                            s_new_reportnumberprefix = targetEntity_program.new_reportnumberprefix;
                        } else {
                            localContext.Trace("A value was NOT set for the new_reportnumberprefix field, so we will be using P as the default.");
                            s_new_reportnumberprefix = "P";
                        }

                        fileNumberValue = String.Format("{0}-{1}", DateTime.Now.ToString("yy"), s_new_reportnumberprefix);

                        localContext.Trace("Building QueryExpression to retrieve FileNumber Sequence record.");

                        QueryExpression qeFileNumberSequence_2 = new QueryExpression(BaseProxyClass.GetLogicalName<emds_autonumbersequence>());
                        qeFileNumberSequence_2.ColumnSet = new ColumnSet(true);
                        qeFileNumberSequence_2.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_entitylogicalname, ConditionOperator.Equal, BaseProxyClass.GetLogicalName<new_programreport>());
                        qeFileNumberSequence_2.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_attributelogicalname, ConditionOperator.Equal, new_programreport.Properties.new_filenumber);
                        qeFileNumberSequence_2.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_prefix, ConditionOperator.Equal, "PMG");

                        localContext.Trace("Getting FileNumber sequence record.");

                        List<emds_autonumbersequence> lFileNumberSequences_2 = service.RetrieveProxies<emds_autonumbersequence>(qeFileNumberSequence_2);
                        if (lFileNumberSequences_2 == null || lFileNumberSequences_2.Count == 0) {
                            localContext.Trace("No FileNumber sequence record was returned, creatign a new one.");

                            // no matching sequence records.  Lets start a new sequence index record for this month and year.
                            fileNumberSequence = new emds_autonumbersequence();
                            fileNumberSequence.emds_attributelogicalname = new_programreport.Properties.new_filenumber;
                            fileNumberSequence.emds_entitylogicalname = BaseProxyClass.GetLogicalName<new_programreport>();
                            fileNumberSequence.emds_index = 1;
                            fileNumberSequence.emds_prefix = "PMG";
                            fileNumberSequence.emds_name = String.Format("File Number Sequence For: {0}", fileNumberValue);
                            fileNumberSequence.Create(service);
                        } else {
                            localContext.Trace("A FileNumber sequence record was found, using it.");
                            // a file number sequence record was returned.  Even if there happen to be multiple we are going to just use the first one returned.
                            fileNumberSequence = lFileNumberSequences_2[0];
                        }
                        fileNumberValue = String.Format("{0}-{1:0000}", fileNumberValue, fileNumberValue + fileNumberSequence.emds_index.ToString());
                        fileNumberSequence.emds_index++;
                        fileNumberSequence.Update(service);
                        #endregion
                        break;
                    case "3":
                        #region File Format #3

                        if (targetEntity_program.new_filenumbercode != null && targetEntity_program.new_filenumbercode != "") {
                            localContext.Trace("A value was set for the new_filenumbercode field, so we will be using this value.");
                            s_new_filenumbercode = targetEntity_program.new_filenumbercode;
                        } else {
                            localContext.Trace("A value was NOT set for the new_filenumbercode field, so we will be using L as the default.");
                            s_new_filenumbercode = "l";
                        }

                        fileNumberValue = String.Format("{0}-{1}", DateTime.Now.ToString("yy"), s_new_filenumbercode);

                        localContext.Trace("Building QueryExpression to retrieve FileNumber Sequence record.");

                        QueryExpression qeFileNumberSequence_3 = new QueryExpression(BaseProxyClass.GetLogicalName<emds_autonumbersequence>());
                        qeFileNumberSequence_3.ColumnSet = new ColumnSet(true);
                        qeFileNumberSequence_3.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_entitylogicalname, ConditionOperator.Equal, BaseProxyClass.GetLogicalName<new_programreport>());
                        qeFileNumberSequence_3.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_attributelogicalname, ConditionOperator.Equal, new_programreport.Properties.new_filenumber);
                        qeFileNumberSequence_3.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_prefix, ConditionOperator.Equal, fileNumberValue);

                        localContext.Trace("Getting FileNumber sequence record.");

                        List<emds_autonumbersequence> lFileNumberSequences_3 = service.RetrieveProxies<emds_autonumbersequence>(qeFileNumberSequence_3);
                        if (lFileNumberSequences_3 == null || lFileNumberSequences_3.Count == 0) {
                            localContext.Trace("No FileNumber sequence record was returned, creatign a new one.");

                            // no matching sequence records.  Lets start a new sequence index record for this month and year.
                            fileNumberSequence = new emds_autonumbersequence();
                            fileNumberSequence.emds_attributelogicalname = new_programreport.Properties.new_filenumber;
                            fileNumberSequence.emds_entitylogicalname = BaseProxyClass.GetLogicalName<new_programreport>();
                            fileNumberSequence.emds_index = 1;
                            fileNumberSequence.emds_prefix = fileNumberValue;
                            fileNumberSequence.emds_name = String.Format("File Number Sequence For: {0}", fileNumberValue);
                            fileNumberSequence.Create(service);
                        } else {
                            localContext.Trace("A FileNumber sequence record was found, using it.");
                            // a file number sequence record was returned.  Even if there happen to be multiple we are going to just use the first one returned.
                            fileNumberSequence = lFileNumberSequences_3[0];
                        }
                        fileNumberValue = String.Format("{0}-{1:0000}", fileNumberValue, fileNumberValue + fileNumberSequence.emds_index.ToString());
                        fileNumberSequence.emds_index++;
                        fileNumberSequence.Update(service);
                        #endregion
                        break;
                    default:
                        break;
                }

                targetEntity.new_filenumber = fileNumberValue;
            } catch (Exception ex) {
                localContext.Trace(String.Format("IccPlugin.ProgramReport.AutoNumber.SetFileNumber: Exception while setting the File Number value, Error Message: {0}", ex.Message), ex);
                throw ex;
            } finally {
                localContext.Trace("END - IccPlugin.ProgramReport.AutoNumber.SetFileNumber");
            }
        }

        private void SetReportNumber(LocalPluginContext localContext) {
            localContext.Trace("START - IccPlugin.ProgramReport.AutoNumber.SetReportNumber");
            string s_new_reportnumberprefix = string.Empty;

            try {
                IOrganizationService service = localContext.OrganizationService;
                string reportNumberValue = "";
                emds_autonumbersequence reportNumberSequence = null;

                //  ##################################################################################################
                //  05/02/2013  --  BEP -- Code added for the following change to the auto-number for file numbering
                //  ##################################################################################################
                //  Currently the plugin uses the GP Class Id as the prefix for the Report Number.  
                //  It now needs to use the Report Number Prefix field. 
                //  ##################################################################################################

                if (targetEntity_program.new_reportnumberprefix != null && targetEntity_program.new_reportnumberprefix != "") {
                    localContext.Trace("A value was set for the new_reportnumberprefix field, so we will be using this value.");
                    s_new_reportnumberprefix = targetEntity_program.new_reportnumberprefix;
                } else {
                    localContext.Trace("A value was NOT set for the new_reportnumberprefix field, so we will be using P as the default.");
                    s_new_reportnumberprefix = "P";
                }

                localContext.Trace("Building QueryExpression to retrieve parent new_program record.");

                //  #################################################################################
                //  05/02/2013  --  BEP --  The above code replaces the need to pull the GP Class ID
                //  #################################################################################
                //new_program program = targetEntity.new_programid.RetrieveProxy<new_program>(service, new ColumnSet(true));
                // going to assume that we were able to get the parent program record.  If not an exception will be thrown.
                // could add a check here and throw our own detailed exception if needed.

                reportNumberValue = String.Format("{0}", s_new_reportnumberprefix);  // using Trim just to be safe.

                // now lets get the sequence record for this Report Number Prefix
                QueryExpression qeReportNumberSequence = new QueryExpression(BaseProxyClass.GetLogicalName<emds_autonumbersequence>());
                qeReportNumberSequence.ColumnSet = new ColumnSet(true);
                qeReportNumberSequence.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_entitylogicalname, ConditionOperator.Equal, BaseProxyClass.GetLogicalName<new_programreport>());
                qeReportNumberSequence.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_attributelogicalname, ConditionOperator.Equal, new_programreport.Properties.new_name);
                qeReportNumberSequence.Criteria.AddCondition(emds_autonumbersequence.Properties.emds_prefix, ConditionOperator.Equal, reportNumberValue);

                localContext.Trace("Getting Report Number sequence record.");

                List<emds_autonumbersequence> lReportNumberSequences = service.RetrieveProxies<emds_autonumbersequence>(qeReportNumberSequence);
                if (lReportNumberSequences == null || lReportNumberSequences.Count == 0) {
                    localContext.Trace("No Report Number sequence record was returned, creatign a new one.");

                    // no matching sequence records.  Lets start a new sequence index record for this month and year.
                    reportNumberSequence = new emds_autonumbersequence();
                    reportNumberSequence.emds_attributelogicalname = new_programreport.Properties.new_name;
                    reportNumberSequence.emds_entitylogicalname = BaseProxyClass.GetLogicalName<new_programreport>();
                    reportNumberSequence.emds_index = 1;
                    reportNumberSequence.emds_prefix = reportNumberValue;
                    reportNumberSequence.emds_name = String.Format("Report Number Sequence For Report Number Prefix: {0}", reportNumberValue);
                    reportNumberSequence.Create(service);
                } else {
                    localContext.Trace("A Report Number sequence record was found, using it.");
                    // a file number sequence record was returned.  Even if there happen to be multiple we are going to just use the first one returned.
                    reportNumberSequence = lReportNumberSequences[0];
                }

                reportNumberValue = String.Format("{0}-{1}", reportNumberValue, reportNumberSequence.emds_index);
                reportNumberSequence.emds_index++;
                reportNumberSequence.Update(service);

                targetEntity.new_name = reportNumberValue;
            } catch (Exception ex) {
                localContext.Trace(String.Format("IccPlugin.ProgramReport.AutoNumber.SetReportNumber: Exception while setting the File Number value, Error Message: {0}", ex.Message), ex);
                throw ex;
            } finally {
                localContext.Trace("END - IccPlugin.ProgramReport.AutoNumber.SetReportNumber");
            }
        }
    }
}
4

1 に答える 1