1

Pre_operation の "Send" メッセージで "Email" エンティティに crm2011 プラグインを書いています。私がやりたいことは、電子メールエンティティの「送信」ボタンをクリックしたときに、送信する前に必要なチェックを行うことです。チェックが正しくない場合は、メールの送信を防止および停止し、「アラート メッセージ」を表示して、2 番目のプラグインを停止します (このプラグインは、メールを送信し、関連付けられたエンティティを作成して「ケース」を変換します)。そのプラグインの提案を教えてください。pre-validation ステージまたは Pre_operation 状態を使用する必要がありますか? また、false を返してプラグインを停止するにはどうすればよいですか。

  public void Execute(IServiceProvider serviceProvider)
    {
        try
        {
            string message = null;
            _serviceProvider = serviceProvider;
            _context = (IPluginExecutionContext)
                                         serviceProvider.GetService(typeof(IPluginExecutionContext));

            _serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            _currentUser = _context.UserId;
            message = _context.MessageName.ToLower();
            if (message == "send")
            {
                if (_context.InputParameters != null && _context.InputParameters.Contains("EmailId"))
                {
                    object objEmailId = _context.InputParameters["EmailId"];
                    if (objEmailId != null)
                    {
                        _emailId = new Guid(objEmailId.ToString());
                        FindEmailInfo();
                        if (_email != null)
                        {
                            if (_email.Attributes.Contains("description") && _email.Attributes["description"] != null)//Email descritpion is not null
                            {
                                string emaildescription = StripHTML();

                                //Find KB Article prefix no in system config entity
                                serviceguideprefix = "ServiceGuidesPrefix";
                                QueryByAttribute query = new QueryByAttribute("ppp_systemconfig");
                                query.ColumnSet = new ColumnSet(true);
                                query.AddAttributeValue(sysconfig_name, serviceguideprefix);
                                EntityCollection sysconfig = _service.RetrieveMultiple(query);
                                if (sysconfig.Entities.Count > 0)
                                {
                                    Entity e = sysconfig.Entities[0];
                                    if (e.Attributes.Contains("ppp_value"))
                                    {
                                        ppp_value = e.Attributes["ppp_value"].ToString();
                                    }
                                }
                                if (ppp_value != null && ppp_value != string.Empty)
                                {
                                    //var matches = Regex.Matches(emaildescription, @"KBA-\d*-\w*").Cast<Match>().ToArray();
                                    var matches = Regex.Matches(emaildescription, ppp_value + @"-\d*-\w*").Cast<Match>().ToArray();
                                    //ReadKBNo(emaildescription);
                                    foreach (Match kbnumber in matches)
                                    {
                                        EntityCollection kbarticlecol = FindKBArticleIds(kbnumber.ToString());
                                        if (kbarticlecol.Entities.Count > 0)
                                        {
                                            Entity kbariticle = kbarticlecol.Entities[0];
                                            if (kbariticle.Attributes.Contains("mom_internalkm"))
                                            {
                                                bool internalserviceguide = (bool)kbariticle.Attributes["mom_internalkm"];
                                                if (internalserviceguide) found = true;
                                                else found = false;
                                            }
                                            else found = false;
                                        }
                                    }
                                }
                                if (found)
                                {
                                    //-----
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new InvalidPluginExecutionException(ex.Message, ex);
        }
    }
4

2 に答える 2

4

プラグインを停止するのは簡単です。InvalidPluginException をスローするだけです。指定したメッセージは、アラート ウィンドウでユーザーに表示されます。送信前にこれを行う必要があります。この場合、検証前か操作前かは問題ではないと思います。

編集:

はい、コードで例外が発生していなくても、InvalidPluginException をスローする必要があります。これは私たちが通常行うことではないことを認めますが、それが機能する方法です。Msdn には詳細があります: http://msdn.microsoft.com/en-us/library/gg334685.aspx

たとえば、コードは次のようになります。

public void Execute(IServiceProvider serviceProvider)
{
    try    
    {
        //This is where we validate the email send
        if(emailIsOkay)
        {
            //Do something
        }
        else if(emailIsNotOkay)
        {
            //Throw and exception that will stop the plugin and the message will be shown to the user (if its synchronous)
            throw new InvalidPluginExecutionException("Hello user, your email is not correct!!");
        }
    }
    catch (InvalidPluginExecutionException invalid)
    {
        //We dont to catch exception for InvalidPluginExecution, so just throw them on
        throw; 
    }
    catch (Exception ex)
    {
        //This exception catches if something goes wrong in the code, or some other process.
        throw new InvalidPluginExecutionException(ex.Message, ex);
    }
}
于 2012-08-06T06:47:41.010 に答える