簡體   English   中英

重新打開機會時執行的CRM插件

[英]CRM plugin to execute when opportunity is reopened

我需要為Dynamics CRM 4.0編寫一個插件,該插件在重新打開關閉的機會時執行,以便更改salesstagecode。 我的問題是:

  • 當我向插件注冊一個新步驟時,我應該過濾哪些屬性?
  • 我應該檢查實體上的哪些屬性值?
  • 我該如何查找此實體的值,以便我可以確定插件執行是否應該繼續?

我通常編寫異步工作流程,我編寫插件的經驗仍在開發中,所以我很感激可以提供任何幫助和澄清。

請參閱下面我編寫的插件框架

    public void Execute(IPluginExecutionContext context)
    {
        if (context.InputParameters.Properties.Contains("Target") && context.InputParameters.Properties["Target"] is DynamicEntity)
        {
            ICrmService service = context.CreateCrmService(false);

            DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["Target"];

            if (entity.Name == EntityName.opportunity.ToString())
            {
                if (entity.Properties.Contains(/*What Property Should I Check Here?*/))
                {
                    //And what value should I be looking for in that property?

                }
            }
        }
    }

我想你實際上想要在SetStateDynamicEntity消息上為post階段注冊一個插件。 您不希望此消息有任何過濾屬性。

您的代碼看起來像這樣:

public void Execute(IPluginExecutionContext context)
{
    string state = (string)context.InputParameters["State"];
    if (state == "Open")
    {
        Moniker entityMoniker = (Moniker)context.InputParameters["EntityMoniker"];
        DynamicEntity opp = new DynamicEntity("opportunity");
        opp["opportunityid"] = new Key(entityMoniker.Id);
        opp["salesstagecode"] = new Picklist(/*your value*/);
        context.CreateCrmService(true).Update(opp);
    }
}

您將要在SetStateDynamic消息上設置實體。 這不提供目標,因此您需要拉動EntityMoniker並手動檢索實體,如下所示:

            // If this is a setstate call, we need to manually pull the entity
            if (context.InputParameters.Properties.Contains("EntityMoniker") &&
                    context.InputParameters.Properties["EntityMoniker"] is Moniker)
            {
                Moniker entity = (Moniker)context.InputParameters.Properties["EntityMoniker"];

                // get the entity
                TargetRetrieveDynamic targetRet = new TargetRetrieveDynamic();
                targetRet.EntityId = entity.Id;
                targetRet.EntityName = context.PrimaryEntityName;

                RetrieveRequest retrieveReq = new RetrieveRequest();
                retrieveReq.ColumnSet = new ColumnSet();
                retrieveReq.ColumnSet.AddColumns(new string[]{"opportunityid", "statecode", "statuscode"});
                retrieveReq.Target = targetRet;
                retrieveReq.ReturnDynamicEntities = true;

                RetrieveResponse retrieveRes = this.Service.Execute(retrieveReq) as RetrieveResponse;

                // Set the new entity and the status
                int status = (int)context.InputParameters["Status"];
                dynEntity = (DynamicEntity)retrieveRes.BusinessEntity;                                        
                dynEntity.Properties["statuscode"] = new Status(status);                      
            }

這是我最終得到的。 我會將此標記為正確的答案,但是當我能夠再次投票時,它會給你(Focus和Corey)一個upvote,因為我結合了你們雙方的建議來達成這個解決方案。

    public void Execute(IPluginExecutionContext context)
    {
        if (context.InputParameters.Properties.Contains("Target") &&
            context.InputParameters.Properties["Target"] is DynamicEntity)
        {
            DynamicEntity opp = (DynamicEntity)context.InputParameters["Target"];
            Picklist StageCodePicklist = new Picklist(); (Picklist);opp.Properties["salesstagecode"];
            StageCodePicklist.name = "Advocating - Advanced (90%)";
            StageCodePicklist.Value = 200004;
            opp.Properties["salesstagecode"] = StageCodePicklist;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM