获取相关的实体statecode值以用作检查

时间:2015-04-27 20:43:59

标签: c# plugins entity dynamics-crm dynamics-crm-2013

我需要能够仅在相关商机有statecode 1

的条件下执行我的代码

在我的代码中,我可以使用Microsoft Dynamics SDK提供的GenerateSalesOrderFromOpportunityRequest类在创建商机关闭活动时创建新的销售订单。

此方法的缺点是,当机会以赢(opportunityclose)或丢失(1)关闭时,系统会创建2活动。此外,opportunityclose活动中没有任何属性表明它是赢还是输。所以找到它的唯一方法是从相关的机会中获取该属性。

在我的代码中,我能够从相关商机中获取其他属性,例如name,但我无法获得statecode其他0的任何其他值}}

这是我的代码:

Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

            if (postImageEntity.LogicalName == "opportunityclose" && postImageEntity.Attributes.Contains("opportunityid") && postImageEntity.Attributes["opportunityid"] != null)
            {
                // Create an entity reference for the related opportunity to get the id for the GenerateSalesOrderFromOpportunityRequest class
                EntityReference entityRef = (EntityReference)postImageEntity.Attributes["opportunityid"];

                // Retrieve the opportunity that the closed opportunity activity was created for.
                Entity RelatedEntityRef = service.Retrieve("opportunity", entityRef.Id, new ColumnSet( new String[] {"statecode","statuscode", "name"}));

                OptionSetValue StateCode = (OptionSetValue)RelatedEntityRef.Attributes["statecode"];

                OptionSetValue StatusCode = (OptionSetValue)RelatedEntityRef.Attributes["statuscode"];

                string OppName = (string)RelatedEntityRef.Attributes["name"];

                if (entityRef.LogicalName == "opportunity" && StateCode.Value == 1)
                {
                    try
                    {
                        GenerateSalesOrderFromOpportunityRequest req = new GenerateSalesOrderFromOpportunityRequest();
                        req.OpportunityId = entityRef.Id;
                        req.ColumnSet = new ColumnSet(true);
                        GenerateSalesOrderFromOpportunityResponse resp = (GenerateSalesOrderFromOpportunityResponse)service.Execute(req);
                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                    }
                }
            }

回顾:为了实现这一点,我只需要能够获得与statecode相关的opportunity的实际opportunityclose值。目前,即使我知道商机的州代码为0,我也只能获得1

其他信息:

  • 这适用于Microsoft Dynamics Online 2013/2015(适用于两者)
  • 使用SKD v6.1.1
  • 插件有效,但会触发机会是赢还是输。 (不是故意的)

1 个答案:

答案 0 :(得分:0)

您无法查看查看状态是赢还是输的机会。我

如果需要,您可以检索OpportunityClose并更改状态 https://msdn.microsoft.com/en-us/library/gg334301.aspx

我认为它将机会关闭设置为1,因为您正在执行GenerateSalesOrderFromOpportunityRequest,只有在您赢得机会时才会这样做(例如,您不会失去机会)。